fix: prevent duplicate reasoning-end events in Ollama stream #903
+8
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes an issue where the Ollama stream handler would repeatedly emit
ThinkingCompleteEvent(mapping toreasoning-end) for every chunk after a reasoning block had finished.The Problem:
The
Streamhandler checksif ($this->state->hasThinkingStarted())to determine if it should emit aThinkingCompleteEvent. However, theStreamStateclass lacked a method to resetthinkingStartedtofalse. As a result, once asking for reasoning began, the flag remainedtruefor the duration of the stream, causingThinkingCompleteEventto be yielded for every subsequent data chunk that didn't contain a thinking delta.The Fix:
markThinkingCompleted()toStreamStateto allow resetting thethinkingStartedflag.Providers\Ollama\Handlers\Streamto call$this->state->markThinkingCompleted()immediately before yieldingThinkingCompleteEvent.This ensures the event is emitted exactly once per reasoning block, preventing frontend errors (such as
AI_UIMessageStreamError: Received reasoning-end for missing reasoning part) caused by duplicate end events.Breaking Changes
None. This is a bug fix that adds a method to
StreamStateand corrects internal state management logic.