From 346b663079687fb5142776c7be9ad357997f066f Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Thu, 27 Nov 2025 09:40:00 +0800 Subject: [PATCH] **fix(translator): handle non-JSON output gracefully in function call outputs** - Updated handling of `output` in `gemini_openai-responses_request.go` to use `.Str` instead of `.Raw` when parsing non-JSON string outputs. - Added checks to distinguish between JSON and non-JSON `output` types for accurate `functionResponse` construction. --- .../responses/gemini_openai-responses_request.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go b/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go index b7d9b193..e0cd60e5 100644 --- a/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go +++ b/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go @@ -187,7 +187,7 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte // Handle function call outputs - convert to function message with functionResponse callID := item.Get("call_id").String() // Use .Raw to preserve the JSON encoding (includes quotes for strings) - outputRaw := item.Get("output").Raw + outputRaw := item.Get("output").Str functionContent := `{"role":"function","parts":[]}` functionResponse := `{"functionResponse":{"name":"","response":{}}}` @@ -212,13 +212,16 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte // Set the raw JSON output directly (preserves string encoding) if outputRaw != "" && outputRaw != "null" { - functionResponse, _ = sjson.SetRaw(functionResponse, "functionResponse.response.result", outputRaw) + output := gjson.Parse(outputRaw) + if output.Type == gjson.JSON { + functionResponse, _ = sjson.SetRaw(functionResponse, "functionResponse.response.result", output.Raw) + } else { + functionResponse, _ = sjson.Set(functionResponse, "functionResponse.response.result", outputRaw) + } } - functionContent, _ = sjson.SetRaw(functionContent, "parts.-1", functionResponse) out, _ = sjson.SetRaw(out, "contents.-1", functionContent) } - return true }) } else if input.Exists() && input.Type == gjson.String {