From d475aaba962c3361fa8d757ba6428eddacea27ce Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Tue, 24 Mar 2026 01:00:57 +0800 Subject: [PATCH] Fixed: #2274 fix(translator): omit null content fields in Codex OpenAI tool call responses --- .../chat-completions/codex_openai_response.go | 2 +- .../codex_openai_response_test.go | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/internal/translator/codex/openai/chat-completions/codex_openai_response.go b/internal/translator/codex/openai/chat-completions/codex_openai_response.go index 94367e50..ab728a24 100644 --- a/internal/translator/codex/openai/chat-completions/codex_openai_response.go +++ b/internal/translator/codex/openai/chat-completions/codex_openai_response.go @@ -60,7 +60,7 @@ func ConvertCodexResponseToOpenAI(_ context.Context, modelName string, originalR rawJSON = bytes.TrimSpace(rawJSON[5:]) // Initialize the OpenAI SSE template. - template := []byte(`{"id":"","object":"chat.completion.chunk","created":12345,"model":"model","choices":[{"index":0,"delta":{"role":null,"content":null,"reasoning_content":null,"tool_calls":null},"finish_reason":null,"native_finish_reason":null}]}`) + template := []byte(`{"id":"","object":"chat.completion.chunk","created":12345,"model":"model","choices":[{"index":0,"delta":{},"finish_reason":null,"native_finish_reason":null}]}`) rootResult := gjson.ParseBytes(rawJSON) diff --git a/internal/translator/codex/openai/chat-completions/codex_openai_response_test.go b/internal/translator/codex/openai/chat-completions/codex_openai_response_test.go index 06e917d3..534884c2 100644 --- a/internal/translator/codex/openai/chat-completions/codex_openai_response_test.go +++ b/internal/translator/codex/openai/chat-completions/codex_openai_response_test.go @@ -45,3 +45,48 @@ func TestConvertCodexResponseToOpenAI_FirstChunkUsesRequestModelName(t *testing. t.Fatalf("expected model %q, got %q", modelName, gotModel) } } + +func TestConvertCodexResponseToOpenAI_ToolCallChunkOmitsNullContentFields(t *testing.T) { + ctx := context.Background() + var param any + + out := ConvertCodexResponseToOpenAI(ctx, "gpt-5.4", nil, nil, []byte(`data: {"type":"response.output_item.added","item":{"type":"function_call","call_id":"call_123","name":"websearch"}}`), ¶m) + if len(out) != 1 { + t.Fatalf("expected 1 chunk, got %d", len(out)) + } + + if gjson.GetBytes(out[0], "choices.0.delta.content").Exists() { + t.Fatalf("expected content to be omitted, got %s", string(out[0])) + } + if gjson.GetBytes(out[0], "choices.0.delta.reasoning_content").Exists() { + t.Fatalf("expected reasoning_content to be omitted, got %s", string(out[0])) + } + if !gjson.GetBytes(out[0], "choices.0.delta.tool_calls").Exists() { + t.Fatalf("expected tool_calls to exist, got %s", string(out[0])) + } +} + +func TestConvertCodexResponseToOpenAI_ToolCallArgumentsDeltaOmitsNullContentFields(t *testing.T) { + ctx := context.Background() + var param any + + out := ConvertCodexResponseToOpenAI(ctx, "gpt-5.4", nil, nil, []byte(`data: {"type":"response.output_item.added","item":{"type":"function_call","call_id":"call_123","name":"websearch"}}`), ¶m) + if len(out) != 1 { + t.Fatalf("expected tool call announcement chunk, got %d", len(out)) + } + + out = ConvertCodexResponseToOpenAI(ctx, "gpt-5.4", nil, nil, []byte(`data: {"type":"response.function_call_arguments.delta","delta":"{\"query\":\"OpenAI\"}"}`), ¶m) + if len(out) != 1 { + t.Fatalf("expected 1 chunk, got %d", len(out)) + } + + if gjson.GetBytes(out[0], "choices.0.delta.content").Exists() { + t.Fatalf("expected content to be omitted, got %s", string(out[0])) + } + if gjson.GetBytes(out[0], "choices.0.delta.reasoning_content").Exists() { + t.Fatalf("expected reasoning_content to be omitted, got %s", string(out[0])) + } + if !gjson.GetBytes(out[0], "choices.0.delta.tool_calls.0.function.arguments").Exists() { + t.Fatalf("expected tool call arguments delta to exist, got %s", string(out[0])) + } +}