From ae7946402ac0c98c877db144065019e2ba243cd2 Mon Sep 17 00:00:00 2001 From: RCD <90105158+Finesssee@users.noreply.github.com> Date: Fri, 30 Jan 2026 17:19:56 +0700 Subject: [PATCH] fix(translator): validate complete JSON before using SetRaw for tool output Tool outputs like "[1,2,3]\n\n[Process exited with code 0]" were being partially parsed by gjson.Parse() which would extract just "[1,2,3]" and lose the trailing text. This caused invalid JSON in the Gemini request. Fix: Use gjson.Valid() to verify the ENTIRE string is valid JSON before treating it as raw JSON. If validation fails, treat as a string with proper escaping via sjson.Set(). Co-Authored-By: Claude (gemini-claude-opus-4-5-thinking) --- .../openai/responses/gemini_openai-responses_request.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 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 6decd8ee..952a056a 100644 --- a/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go +++ b/internal/translator/gemini/openai/responses/gemini_openai-responses_request.go @@ -293,8 +293,11 @@ func ConvertOpenAIResponsesRequestToGemini(modelName string, inputRawJSON []byte // Set the raw JSON output directly (preserves string encoding) if outputRaw != "" && outputRaw != "null" { - output := gjson.Parse(outputRaw) - if output.Type == gjson.JSON { + // Only treat as JSON if the ENTIRE string is valid JSON. + // gjson.Parse will parse partial JSON (e.g., "[1,2,3]\n\nmore text" parses as [1,2,3]), + // so we use gjson.Valid to verify the complete string is valid JSON. + if gjson.Valid(outputRaw) { + output := gjson.Parse(outputRaw) functionResponse, _ = sjson.SetRaw(functionResponse, "functionResponse.response.result", output.Raw) } else { functionResponse, _ = sjson.Set(functionResponse, "functionResponse.response.result", outputRaw)