From f988c47feca522fb46df094734cc608071f782ae Mon Sep 17 00:00:00 2001 From: m1saka Date: Sun, 23 Aug 2026 14:15:01 +0800 Subject: [PATCH] Fix silent data loss in function_call arguments/output non-string JSON parsing 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. --- internal/openai/responses.go | 61 +++++++++++++++++++--- internal/openai/responses_test.go | 84 +++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 6 deletions(-) diff --git a/internal/openai/responses.go b/internal/openai/responses.go index a2a22a7..d9745a0 100644 --- a/internal/openai/responses.go +++ b/internal/openai/responses.go @@ -3,6 +3,7 @@ package openai import ( "encoding/json" "errors" + "strings" ) // --- Responses API request parsing & conversion --- @@ -194,9 +195,7 @@ func responsesInputToMessages(input json.RawMessage) ([]map[string]any, error) { inner["name"] = name } if v, ok := item["arguments"]; ok { - var args string - _ = json.Unmarshal(v, &args) - inner["arguments"] = args + inner["arguments"] = rawJSONToString(v) } if v, ok := item["call_id"]; ok { var id string @@ -215,9 +214,7 @@ func responsesInputToMessages(input json.RawMessage) ([]map[string]any, error) { msg["tool_call_id"] = id } if v, ok := item["output"]; ok { - var out string - _ = json.Unmarshal(v, &out) - msg["content"] = out + msg["content"] = outputToString(v) } messages = append(messages, msg) default: @@ -279,6 +276,58 @@ func convertContentParts(raw json.RawMessage) any { return result } +// rawJSONToString converts a json.RawMessage to a string. If the value is +// already a JSON string, it is used directly. Any other JSON value (object, +// array, number, bool) is re-encoded as a JSON string so it can populate +// fields that require a string, such as tool_calls[].function.arguments. +// This prevents silent data loss when a field arrives as a non-string type. +func rawJSONToString(v json.RawMessage) string { + var s string + if json.Unmarshal(v, &s) == nil { + return s + } + var anyValue any + if json.Unmarshal(v, &anyValue) == nil { + if b, err := json.Marshal(anyValue); err == nil { + return string(b) + } + } + return string(v) // last resort: raw bytes +} + +// outputToString converts a function_call_output "output" value to a string +// for the Chat Completions tool message content. The output may be: +// - a plain string (used directly) +// - an array of content parts (text extracted and concatenated) +// - any other JSON value (re-encoded as a JSON string) +func outputToString(v json.RawMessage) string { + var s string + if json.Unmarshal(v, &s) == nil { + return s + } + // Try array of content parts — extract text from each part. + var parts []map[string]any + if json.Unmarshal(v, &parts) == nil { + var sb strings.Builder + for _, p := range parts { + if t, _ := p["text"].(string); t != "" { + sb.WriteString(t) + } + } + if sb.Len() > 0 { + return sb.String() + } + } + // Fallback: re-encode as a JSON string. + var anyValue any + if json.Unmarshal(v, &anyValue) == nil { + if b, err := json.Marshal(anyValue); err == nil { + return string(b) + } + } + return string(v) +} + // translateResponsesTools converts tools from the Responses API flat format // to the Chat Completions nested {type:"function",function:{…}} format. func translateResponsesTools(raw json.RawMessage) (json.RawMessage, error) { diff --git a/internal/openai/responses_test.go b/internal/openai/responses_test.go index 7b7d69f..2c46cdb 100644 --- a/internal/openai/responses_test.go +++ b/internal/openai/responses_test.go @@ -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,