Skip to content

fix(spring-boot): keep the finish reason when usage arrives in the last chunk - #2276

Open
elgamal-ahmed wants to merge 1 commit into
JetBrains:developfrom
elgamal-ahmed:gh-2109
Open

elgamal-ahmed wants to merge 1 commit into
JetBrains:developfrom
elgamal-ahmed:gh-2109

Conversation

@elgamal-ahmed

Copy link
Copy Markdown

Keeps the finish reason and the token usage when Spring AI reports them in separate stream chunks, so the terminal End frame carries both. Closes #2109

executeStreaming held a single ChatResponse reference that every collected chunk overwrote, then read both values off it in the finally block. With stream-usage enabled the last chunk carries the usage and an empty results list, so the finish reason from the chunk before it was lost and the End frame reported null; with the option off the symptom inverts and the token counts stay at zero. Each value is now kept as it arrives, the same shape PR #1404 used for the OpenAI-like clients. The one judgement call is what a chunk reporting neither looks like: ChatResponseMetadata.getUsage() is never null but reports zeros, and Mistral AI and DeepSeek pad every streamed chunk with an empty finish reason, so an empty reason is skipped and a zero usage only fills in while nothing has been counted yet, which keeps the End frame for a stream that reports no usage at all exactly as it is today. koog-spring-ai-v2 carried the same code and gets the same fix.

Tests: four per module, covering the usage-only terminal chunk, usage followed by a chunk that reports none, a padded empty finish reason after a real one, and the unchanged zero counts when nothing reports usage. The first three fail on develop without the production change.

closes KG-857

…st chunk

SpringAiLLMClient.executeStreaming held a single ChatResponse reference
that every collected chunk overwrote, then read the finish reason and the
token usage off it once the stream had ended. With Spring AI's
stream-usage option the terminal chunk carries the usage and an empty
results list, so the finish reason from the chunk before it was dropped
and the End frame reported null.

Keep each value as it arrives instead, the way the OpenAI-like clients
already do. A chunk that reports neither carries an empty finish reason
and zero token counts rather than nulls, and Mistral AI and DeepSeek pad
every streamed chunk that way, so neither is allowed to replace what an
earlier chunk reported. koog-spring-ai-v2 carried the same code and gets
the same fix.

Closes JetBrains#2109

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SpringAiLLMClient.executeStreaming drops finishReason with stream-usage enabled

1 participant