Skip to content

client: skip blank lines in streaming NDJSON responses - #784

Open
AP24110011695 wants to merge 1 commit into
ollama:mainfrom
AP24110011695:fix/stream-blank-lines
Open

AP24110011695 wants to merge 1 commit into
ollama:mainfrom
AP24110011695:fix/stream-blank-lines

Conversation

@AP24110011695

@AP24110011695 AP24110011695 commented Nov 1, 2026 •

Copy link
Copy Markdown

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's iter_lines() / aiter_lines() yield empty strings for blank lines, and json.loads('') raises — found by pointing the client at a response body containing \n\n between 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._request and AsyncClient._request). Non-blank parsing, error handling 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_lines
  • test_async_client_chat_stream_skips_blank_lines

Both fail on main (35d9667) with json.JSONDecodeError and pass with this change. Full suite: no new failures (the Image/create_blob failures present locally fail identically on clean main on this Windows machine and are unrelated).

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

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.

1 participant