Gemini tool loop fails on tool results that start with `[` but are not JSON
Description
JsonWrappingToolResponseContentAdapter passes through unchanged any tool result that starts with { or [. Gemini needs valid JSON in a tool response, and GoogleGenAiChatModel.parseJsonToMap parses every tool result. When a tool returns plain text that only happens to start with a bracket, for example [Submission accepted] The total has been written to output/total.json., nothing wraps it. The request then fails before it is sent:
java.lang.RuntimeException: Failed to parse JSON: [Submission accepted] ...
at org.springframework.ai.google.genai.GoogleGenAiChatModel.parseJsonToMap(GoogleGenAiChatModel.java:376)
at org.springframework.ai.google.genai.GoogleGenAiChatModel.messageToGeminiParts(GoogleGenAiChatModel.java:319)
...
Caused by: tools.jackson.core.exc.StreamReadException: Unrecognized token 'Submission'
The error is deterministic, but the retry template treats it as transient. Each attempt fails the same way, with backoffs of up to 180 s, so the tool loop hangs for many minutes before it gives up.
Steps to reproduce
- Use a Google GenAI (Gemini) model in a tool loop.
- Have a tool return text that starts with
[and is not JSON, for exampleTool.Result.text("[Done] 3 files written"). - Make the loop call the model again after that tool result.
Plain text that starts with a bracket should be wrapped like any other plain text, but is passed through as is.
Proposed fix
In JsonWrappingToolResponseContentAdapter.adapt, keep the { / [ check as a fast path, but pass the content through only when it actually parses as JSON. Otherwise, wrap it. The class KDoc already lists {not json} as a known gap; this closes it.
Branch with the fix and tests: fix/tool-response-adapter-bracket-text.