spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

Fix OllamaChatModel dropping thinking field in multi-turn conversation history

Open suryateja-g13 opened this issue 6 months ago • 1 comments

Closes #6016 (Ollama path).

Problem

OllamaChatModel stores the thinking field from model responses in ChatGenerationMetadata (via builder.metadata("thinking", thinking)), but when an AssistantMessage is replayed as conversation history in a follow-up request, only AssistantMessage.properties is accessible — ChatGenerationMetadata is not available at that point. As a result, thinking was always null when building the outbound OllamaApi.Message, silently breaking multi-turn conversations with Qwen3 and other thinking models.

Fix

Extract thinking from the Ollama response at both receive sites (blocking and streaming) and store it in AssistantMessage.properties under the same "thinking" key, in addition to the existing ChatGenerationMetadata storage. The createRequest() ASSISTANT branch already reads that key from AssistantMessage.getMetadata() and passes it to Message.Builder.thinking().

Test plan

  • [ ] Unit test: build an AssistantMessage with thinking in properties, call ollamaChatRequest(), verify the outbound OllamaApi.Message carries thinking
  • [ ] Existing OllamaChatModelTests pass
  • [ ] Streaming path: same round-trip test using the streaming code path

suryateja-g13 avatar May 17 '26 21:05 suryateja-g13

The failing CI check ('MCP common and annotations integration tests') is caused by a known flaky test: WebClientStreamableHttpTransportErrorHandlingIT.testSessionRecoveryAfter404 (race condition, tracked in #5563). This failure is unrelated to the Ollama changes in this PR, which only touch OllamaChatModel.java and its test. The 'Build branch' check passes cleanly.

suryateja-g13 avatar May 19 '26 02:05 suryateja-g13