fix: Ollama Empty Text Streaming Issue #1294
Open
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.
Fix Ollama Empty TextPart Streaming
Description
This PR fixes an issue where streaming responses would stop prematurely when encountering empty
TextPart
responses from the Ollama model (served through OpenAI compatible API). The stream was incorrectly ending early when Ollama returned empty text parts before generating tool calls or additional content.Changes
stream_to_final
function inagent.py
to be more robust:has_content()
checkTesting
Related Issue
Fixes #1292
Additional Notes
This issue primarily affected Ollama model responses, but the fix enhances streaming robustness across all providers by ensuring only meaningful content triggers a final result.
There is a slight increase in token counting observed in tests after this fix. This is expected and proper behavior - previously, empty text chunks would cause premature stream termination, resulting in incomplete token counting. The fix ensures the complete stream is processed correctly, resulting in more accurate token counts and proper chunking of the streamed response.