Skip to content

fix: handle Responses tool input variants#2914

Open
StarryKira wants to merge 1 commit into
Wei-Shaw:mainfrom
StarryKira:fix/responses-tool-input-rawmessage
Open

fix: handle Responses tool input variants#2914
StarryKira wants to merge 1 commit into
Wei-Shaw:mainfrom
StarryKira:fix/responses-tool-input-rawmessage

Conversation

@StarryKira
Copy link
Copy Markdown
Contributor

Summary: Allow Responses function_call arguments to be JSON objects or stringified JSON, normalize function_call_output strings and output_text arrays for Anthropic and ChatCompletions bridges, and preserve existing string wire format when generating Responses input. Tests: go test ./internal/pkg/apicompat; go test ./...

Copilot AI review requested due to automatic review settings May 30, 2026 17:25
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR improves API-compat handling for function_call / function_call_output items by treating tool arguments and outputs as raw JSON so the bridge can accept either native JSON objects/arrays or JSON-encoded strings.

Changes:

  • Change ResponsesInputItem.Arguments and ResponsesInputItem.Output from string to json.RawMessage.
  • Add normalization helpers to coerce tool arguments/outputs into formats expected by Chat Completions and Anthropic request shapes.
  • Update conversion code and tests to cover object-arguments and array-output cases.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
backend/internal/pkg/apicompat/types.go Switch tool argument/output fields to json.RawMessage to accept richer JSON wire formats.
backend/internal/pkg/apicompat/responses_to_anthropic_request.go Use normalization helpers when building Anthropic tool blocks.
backend/internal/pkg/apicompat/responses_input_normalization.go Add normalization/validation helpers for tool args and outputs.
backend/internal/pkg/apicompat/chatcompletions_to_responses.go Wrap Chat → Responses tool args/outputs as JSON strings via json.RawMessage.
backend/internal/pkg/apicompat/chatcompletions_responses_bridge.go Normalize Responses → Chat tool args/outputs.
backend/internal/pkg/apicompat/test_helpers_test.go Add helper to assert json.RawMessage contains a JSON string.
backend/internal/pkg/apicompat/chatcompletions_responses_test.go Update assertions for raw-string output.
backend/internal/pkg/apicompat/chatcompletions_responses_bridge_test.go Add tests for object-args + array-output and string-args + string-output.
backend/internal/pkg/apicompat/anthropic_to_responses.go Ensure tool output/args remain string-on-wire while stored as RawMessage.
backend/internal/pkg/apicompat/anthropic_responses_test.go Update + extend tests for new tool args/output representations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

CallID: tc.ID,
Name: tc.Function.Name,
Arguments: args,
Arguments: jsonStringRawMessage(args),
Type: "function_call_output",
CallID: m.ToolCallID,
Output: output,
Output: jsonStringRawMessage(output),
Type: "function_call_output",
CallID: m.Name,
Output: output,
Output: jsonStringRawMessage(output),
Comment on lines +9 to +12
func jsonStringRawMessage(s string) json.RawMessage {
raw, _ := json.Marshal(s)
return raw
}
Comment on lines +233 to +239
CallID string `json:"call_id,omitempty"`
Name string `json:"name,omitempty"`
Arguments json.RawMessage `json:"arguments,omitempty"`
ID string `json:"id,omitempty"`

// type=function_call_output
Output string `json:"output,omitempty"`
Output json.RawMessage `json:"output,omitempty"`
Comment on lines +38 to +56
func responsesToolArgumentsForChat(raw json.RawMessage) string {
raw = bytesTrimSpace(raw)
if len(raw) == 0 || string(raw) == "null" {
return "{}"
}
if rawIsJSONObject(raw) {
return string(compactRawJSON(raw))
}

var s string
if err := json.Unmarshal(raw, &s); err != nil {
return "{}"
}
s = strings.TrimSpace(s)
if s == "" {
return "{}"
}
return s
}
return raw
}

func responsesToolArgumentsForAnthropic(raw json.RawMessage) json.RawMessage {
return s
}

func responsesToolOutputText(raw json.RawMessage) string {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants