Repository navigation
fix(spring-ai): preserve reasoning content across model turns #1913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ | |
| import java.net.URI; | ||
| import java.net.URL; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
|
|
@@ -93,6 +94,8 @@ public class MessageConverter { | |
| */ | ||
| private static final String THOUGHT_SIGNATURES_KEY = "thoughtSignatures"; | ||
|
|
||
| private static final String REASONING_CONTENT_KEY = "reasoningContent"; | ||
|
|
||
| /** Metadata key the Spring AI Google GenAI provider uses to flag a generation as thought text. */ | ||
| private static final String IS_THOUGHT_KEY = "isThought"; | ||
|
|
||
|
|
@@ -574,6 +577,7 @@ private ImmutableList<Message> handleUserContent(Content content) { | |
|
|
||
| private AssistantMessage handleAssistantContent(Content content) { | ||
| StringBuilder textBuilder = new StringBuilder(); | ||
| StringBuilder reasoning = new StringBuilder(); | ||
| List<AssistantMessage.ToolCall> toolCalls = new ArrayList<>(); | ||
| List<byte[]> thoughtSignatures = new ArrayList<>(); | ||
| List<Media> mediaList = new ArrayList<>(); | ||
|
|
@@ -582,6 +586,8 @@ private AssistantMessage handleAssistantContent(Content content) { | |
| if (part.text().isPresent()) { | ||
| if (!part.thought().orElse(false)) { | ||
| textBuilder.append(part.text().get()); | ||
| } else { | ||
| reasoning.append(part.text().get()); | ||
| } | ||
| } else if (part.functionCall().isPresent()) { | ||
| FunctionCall functionCall = part.functionCall().get(); | ||
|
|
@@ -600,11 +606,16 @@ private AssistantMessage handleAssistantContent(Content content) { | |
| } | ||
|
|
||
| String text = textBuilder.toString(); | ||
| if (toolCalls.isEmpty() && mediaList.isEmpty()) { | ||
| Map<String, Object> properties = new LinkedHashMap<>(); | ||
| if (!thoughtSignatures.isEmpty()) { | ||
| properties.put(THOUGHT_SIGNATURES_KEY, thoughtSignatures); | ||
| } | ||
| if (!reasoning.isEmpty()) { | ||
| properties.put(REASONING_CONTENT_KEY, reasoning.toString()); | ||
| } | ||
| if (toolCalls.isEmpty() && mediaList.isEmpty() && properties.isEmpty()) { | ||
| return new AssistantMessage(text); | ||
| } | ||
| Map<String, Object> properties = | ||
| thoughtSignatures.isEmpty() ? Map.of() : Map.of(THOUGHT_SIGNATURES_KEY, thoughtSignatures); | ||
| return AssistantMessage.builder() | ||
| .content(text) | ||
| .properties(properties) | ||
|
|
@@ -767,9 +778,13 @@ private FinishReason toAdkFinishReason(String finishReason) { | |
|
|
||
| private Content convertAssistantMessageToContent(AssistantMessage assistantMessage) { | ||
| List<Part> parts = new ArrayList<>(); | ||
| Map<String, Object> metadata = assistantMessage.getMetadata(); | ||
| Object reasoning = metadata == null ? null : metadata.get(REASONING_CONTENT_KEY); | ||
| if (reasoning instanceof String reasoningText && !reasoningText.isEmpty()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BREAKING (HIGH): Streaming Spring AI OpenAI through the public Repro: Run a real
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 5f31a1d: StreamingResponseAggregator now excludes thought parts from answer aggregation. Added coverage in its existing test class through the public converter, and extended the real OpenAiChatModel SSE fixture to exercise toLlmResponse(chunk, true) plus the public aggregator. The previous PR reproduces the repeated-reasoning answer; the fix preserves answer text and tool calls. |
||
| parts.add(Part.builder().text(reasoningText).thought(true).build()); | ||
| } | ||
|
|
||
| if (assistantMessage.getText() != null && !assistantMessage.getText().isEmpty()) { | ||
| Map<String, Object> metadata = assistantMessage.getMetadata(); | ||
| boolean isThought = | ||
| metadata != null | ||
| && (Boolean.TRUE.equals(metadata.get(IS_THOUGHT_KEY)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DESIGN (MEDIUM): Every later request now sends
reasoning_contenton every earlier assistant turn, with or without tools. It also sendsreasoning_contentto providers that returnedreasoning, because Spring AI reads both intoreasoningContentand always writesreasoning_contentback (OpenAiChatModel.java:680). DeepSeek only needs it when the request includes tools. For example, a second turn without tools changes from{"role":"assistant","content":"Hi."}to{"role":"assistant","content":"Hi.","reasoning_content":"Greet back"}.ADK Python sends it for every non-Anthropic provider, lite_llm.py:1951) and state it in the description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adopted the plain-thought replay behavior and documented it explicitly in 5f31a1d: later requests restore reasoning on earlier assistant turns both with and without tools. The description also calls out Spring AI's outgoing reasoning_content field when the provider originally returned reasoning. The converter round-trip test covers both tool/no-tool variants.