Fix silent data loss in function_call arguments/output non-string JSON parsing
build / build (push) Successful in 2m29s

responsesInputToMessages unmarshaled function_call.arguments and
function_call_output.output as bare strings, silently dropping the
value when it arrived as an object or content-parts array. This
caused the model to lose tool-call context in multi-turn
conversations, increasing the likelihood of malformed tool-call JSON.

Add rawJSONToString (re-encodes non-string values as JSON strings)
and outputToString (extracts text from content-parts arrays, re-encodes
other non-string values). Add 3 regression tests covering object
arguments, array output, and object output.
This commit is contained in:
2026-08-23 14:15:01 +08:00
parent f51ec09a09
commit f988c47fec
2 changed files with 139 additions and 6 deletions
+84
View File
@@ -191,6 +191,90 @@ func TestParseResponsesRequest_FunctionCallInput(t *testing.T) {
}
}
// TestParseResponsesRequest_FunctionCallObjectArguments verifies that when
// function_call.arguments arrives as a JSON object (not a string), it is
// re-encoded as a JSON string instead of being silently dropped.
func TestParseResponsesRequest_FunctionCallObjectArguments(t *testing.T) {
body := `{"model":"GLM-4.7","input":[
{"type":"function_call","call_id":"call_456","name":"task","arguments":{"operation":"create","summary":"test"}}
]}`
req, err := ParseResponsesRequest([]byte(body))
if err != nil {
t.Fatal(err)
}
if len(req.Messages) != 1 {
t.Fatalf("messages = %d items", len(req.Messages))
}
tc, _ := req.Messages[0]["tool_calls"].([]any)
if len(tc) != 1 {
t.Fatalf("tool_calls = %v", req.Messages[0]["tool_calls"])
}
fn, _ := tc[0].(map[string]any)["function"].(map[string]any)
args, _ := fn["arguments"].(string)
if args == "" {
t.Fatalf("arguments was dropped (empty)")
}
// The re-encoded string must be valid JSON containing the original fields.
var parsed map[string]any
if err := json.Unmarshal([]byte(args), &parsed); err != nil {
t.Fatalf("arguments not valid JSON: %v", err)
}
if parsed["operation"] != "create" {
t.Fatalf("operation = %v", parsed["operation"])
}
}
// TestParseResponsesRequest_FunctionCallOutputArray verifies that when
// function_call_output.output arrives as an array of content parts, the
// text is extracted instead of being silently dropped.
func TestParseResponsesRequest_FunctionCallOutputArray(t *testing.T) {
body := `{"model":"GLM-4.7","input":[
{"type":"function_call","call_id":"call_789","name":"get_weather","arguments":"{\"city\":\"NYC\"}"},
{"type":"function_call_output","call_id":"call_789","output":[{"type":"output_text","text":"sunny 72F"}]}
]}`
req, err := ParseResponsesRequest([]byte(body))
if err != nil {
t.Fatal(err)
}
if len(req.Messages) != 2 {
t.Fatalf("messages = %d items", len(req.Messages))
}
toolMsg := req.Messages[1]
if toolMsg["role"] != "tool" {
t.Fatalf("msg[1] role = %v", toolMsg["role"])
}
content, _ := toolMsg["content"].(string)
if content != "sunny 72F" {
t.Fatalf("content = %q, want %q", content, "sunny 72F")
}
}
// TestParseResponsesRequest_FunctionCallOutputObject verifies that when
// function_call_output.output is a bare JSON object, it is re-encoded as a
// JSON string instead of being silently dropped.
func TestParseResponsesRequest_FunctionCallOutputObject(t *testing.T) {
body := `{"model":"GLM-4.7","input":[
{"type":"function_call","call_id":"call_obj","name":"run","arguments":"{}"},
{"type":"function_call_output","call_id":"call_obj","output":{"result":"success","code":200}}
]}`
req, err := ParseResponsesRequest([]byte(body))
if err != nil {
t.Fatal(err)
}
toolMsg := req.Messages[1]
content, _ := toolMsg["content"].(string)
if content == "" {
t.Fatal("content was dropped (empty)")
}
var parsed map[string]any
if err := json.Unmarshal([]byte(content), &parsed); err != nil {
t.Fatalf("content not valid JSON: %v", err)
}
if parsed["result"] != "success" {
t.Fatalf("result = %v", parsed["result"])
}
}
func TestUsageToResponses(t *testing.T) {
usage := map[string]any{
"prompt_tokens": 10,