Fix silent data loss in function_call arguments/output non-string JSON parsing
build / build (push) Successful in 2m29s
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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user