Repository navigation
client: skip blank lines in streaming NDJSON responses - #784
Open
AP24110011695 wants to merge 1 commit into
Open
AP24110011695 wants to merge 1 commit into
AP24110011695 wants to merge 1 commit into
Conversation
A blank line anywhere in the streamed body (from proxies, load
balancers, or compatible servers that re-chunk or pad responses)
crashed the parser with a raw json.JSONDecodeError instead of yielding
responses: iter_lines/aiter_lines yield empty strings for blank lines
and json.loads('') raises.
Skip empty lines in both the sync and async streaming paths, so
generate(..., stream=True) and chat(..., stream=True) tolerate
non-ollama response formatting. Regression tests cover both paths with
a blank line between events.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
A blank line anywhere in a streamed response crashes the parser with a raw
json.JSONDecodeError: Expecting value: line 1 column 1 (char 0)instead of yielding responses.httpx'siter_lines()/aiter_lines()yield empty strings for blank lines, andjson.loads('')raises — found by pointing the client at a response body containing\n\nbetween events (reproduced standalone and in tests).The official server writes clean NDJSON, but this client is pointed at many Ollama-compatible backends and proxies (LiteLLM, LM Studio, gateways) that re-chunk or pad streams; any blank line there aborts
generate(..., stream=True)/chat(..., stream=True)mid-iteration.Fix
Skip empty lines in both streaming paths (
Client._requestandAsyncClient._request). Non-blank parsing,errorhandling and yielded types are untouched.Tests
Two regression tests in
tests/test_client.py(one per path), each feeding a body with a blank line between events over a real local HTTP server:test_client_generate_stream_skips_blank_linestest_async_client_chat_stream_skips_blank_linesBoth fail on
main(35d9667) withjson.JSONDecodeErrorand pass with this change. Full suite: no new failures (the Image/create_blobfailures present locally fail identically on cleanmainon this Windows machine and are unrelated).