Merged upstreamCorrectnessMerged Jun 5, 2026
Avoid duplicate chat memory in tool prompts
Fixed Spring AI chat-memory behavior so tool-call continuation prompts do not repeat stored user, assistant, and tool messages.
spring-projects/spring-ai · #6228
Problem
With chat memory enabled, a prompt that already contained prior tool-call context could be advised with the same memory messages again. In the reported Spring AI M8 reproducer, that duplication caused the model to call the same tool repeatedly and could also create repeated blank chat-memory rows.
Approach
Added detection for memory messages already present inside the current prompt before prepending chat memory. The implementation covers both full-history prompts and windowed-memory prompts while continuing to store the latest tool response.
Impact and scope
- Improves reliability for Spring AI applications that combine chat memory with tool calling, a core path for production agent workflows.
- Prevents repeated tool execution caused by duplicated conversation context, reducing runaway calls and noisy memory growth.
- Strengthens Spring AI 2.0.0-RC1 behavior before release-candidate hardening by covering both full-history and windowed-memory cases.
Validation
- Added a regression test proving existing memory in the prompt is not duplicated before tool-response continuation.
- Added a windowed-memory regression covering retained recent messages and the latest tool response.
- Verified MessageChatMemoryAdvisorTests and the spring-ai-client-chat module test suite from the PR workflow.