fix: handle Responses tool input variants#2914
Open
StarryKira wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
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.ArgumentsandResponsesInputItem.Outputfromstringtojson.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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ./...