From bb58d8103e45d7634379c1beb51c57ee160e7ded Mon Sep 17 00:00:00 2001 From: William Drake Date: Fri, 20 Nov 2026 09:15:45 -0700 Subject: [PATCH] fix(apicompat): accept JSON object arguments in ResponsesInputItem --- .../responses_input_arguments_test.go | 67 +++++++++++++++++++ backend/internal/pkg/apicompat/types.go | 9 ++- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 backend/internal/pkg/apicompat/responses_input_arguments_test.go diff --git a/backend/internal/pkg/apicompat/responses_input_arguments_test.go b/backend/internal/pkg/apicompat/responses_input_arguments_test.go new file mode 100644 index 0000000000..2af2cd5f63 --- /dev/null +++ b/backend/internal/pkg/apicompat/responses_input_arguments_test.go @@ -0,0 +1,67 @@ +package apicompat + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestResponsesToAnthropicRequest_AllowsToolSearchCallObjectArgumentsWithoutTools(t *testing.T) { + // Compaction / follow-up requests often omit `tools`, so AdaptResponsesClientTools + // does not lower historical tool_search_call items whose `arguments` is a JSON object. + req := &ResponsesRequest{ + Model: "claude-opus-5-5", + Input: json.RawMessage(`[ + {"type":"message","role":"user","content":[{"type":"input_text","text":"find git tools"}]}, + {"type":"tool_search_call","id":"tsc_1","call_id":"call_ts1","execution":"client","status":"completed","arguments":{"query":"git","limit":5}}, + {"type":"tool_search_output","id":"tso_1","call_id":"call_ts1","execution":"client","status":"completed","tools":[{"type":"function","name":"git_status","parameters":{"type":"object"}}]}, + {"type":"message","role":"user","content":[{"type":"input_text","text":"continue"}]} + ]`), + } + + got, err := ResponsesToAnthropicRequest(req) + require.NoError(t, err) + require.NotEmpty(t, got.Messages) + // Both user turns survive (merged into one user message because tool_search items are skipped). + require.Len(t, got.Messages, 1) + require.Equal(t, "user", got.Messages[0].Role) + require.Contains(t, string(got.Messages[0].Content), "find git tools") + require.Contains(t, string(got.Messages[0].Content), "continue") +} + +func TestResponsesToAnthropicRequest_AllowsFunctionCallObjectArguments(t *testing.T) { + req := &ResponsesRequest{ + Model: "claude-sonnet-5-5", + Input: json.RawMessage(`[ + {"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]}, + {"type":"function_call","call_id":"call_obj","name":"lookup","arguments":{"city":"Phoenix","limit":3}}, + {"type":"function_call_output","call_id":"call_obj","output":"sunny"} + ]`), + } + + got, err := ResponsesToAnthropicRequest(req) + require.NoError(t, err) + require.Len(t, got.Messages, 3) + + var blocks []AnthropicContentBlock + require.NoError(t, json.Unmarshal(got.Messages[1].Content, &blocks)) + require.Len(t, blocks, 1) + require.Equal(t, "tool_use", blocks[0].Type) + require.Equal(t, "lookup", blocks[0].Name) + require.JSONEq(t, `{"city":"Phoenix","limit":3}`, string(blocks[0].Input)) +} + +func TestResponsesInputItem_UnmarshalArgumentsStringAndNull(t *testing.T) { + var strItem ResponsesInputItem + require.NoError(t, json.Unmarshal([]byte(`{"type":"function_call","call_id":"c1","name":"f","arguments":"{\"a\":1}"}`), &strItem)) + require.Equal(t, `{"a":1}`, strItem.Arguments) + + var objItem ResponsesInputItem + require.NoError(t, json.Unmarshal([]byte(`{"type":"tool_search_call","call_id":"c2","arguments":{"query":"x"}}`), &objItem)) + require.JSONEq(t, `{"query":"x"}`, objItem.Arguments) + + var nullItem ResponsesInputItem + require.NoError(t, json.Unmarshal([]byte(`{"type":"tool_search_call","call_id":"c3","arguments":null}`), &nullItem)) + require.Equal(t, "", nullItem.Arguments) +} diff --git a/backend/internal/pkg/apicompat/types.go b/backend/internal/pkg/apicompat/types.go index b1bb7a2b8b..12c1dee5c4 100644 --- a/backend/internal/pkg/apicompat/types.go +++ b/backend/internal/pkg/apicompat/types.go @@ -402,7 +402,8 @@ func (i *ResponsesInputItem) UnmarshalJSON(data []byte) error { type alias ResponsesInputItem var wire struct { *alias - Output json.RawMessage `json:"output"` + Arguments json.RawMessage `json:"arguments"` + Output json.RawMessage `json:"output"` } *i = ResponsesInputItem{} @@ -411,6 +412,12 @@ func (i *ResponsesInputItem) UnmarshalJSON(data []byte) error { return err } + if args := bytes.TrimSpace(wire.Arguments); len(args) > 0 && !bytes.Equal(args, []byte("null")) { + if err := json.Unmarshal(args, &i.Arguments); err != nil { + i.Arguments = string(args) + } + } + output := bytes.TrimSpace(wire.Output) if len(output) == 0 || bytes.Equal(output, []byte("null")) { return nil