Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ollama/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ def inner():
raise ResponseError(e.response.text, e.response.status_code) from None

for line in r.iter_lines():
if not line:
# Proxies and compatible servers can pad or re-chunk the
# NDJSON stream with blank lines.
continue
part = json.loads(line)
if err := part.get('error'):
raise ResponseError(err)
Expand Down Expand Up @@ -813,6 +817,10 @@ async def inner():
raise ResponseError(e.response.text, e.response.status_code) from None

async for line in r.aiter_lines():
if not line:
# Proxies and compatible servers can pad or re-chunk the
# NDJSON stream with blank lines.
continue
part = json.loads(line)
if err := part.get('error'):
raise ResponseError(err)
Expand Down
45 changes: 45 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1527,3 +1527,48 @@ def test_client_chat_with_think_level(httpserver: HTTPServer):
assert response['model'] == 'qwen3.8:27b'
assert response['message']['content'] == 'Hi there.'
assert response['message']['thinking'] == 'Thinking deeply...'


def test_client_generate_stream_skips_blank_lines(httpserver: HTTPServer):
"Blank lines in an NDJSON stream (proxies, keep-alive padding) must not crash the parser."

httpserver.expect_ordered_request(
'/api/generate',
method='POST',
json={
'model': 'dummy',
'prompt': 'Why is the sky blue?',
'stream': True,
},
).respond_with_data(
json.dumps({'model': 'dummy', 'response': 'Because '}) + '\n\n' + json.dumps({'model': 'dummy', 'response': 'it is.', 'done': True}) + '\n',
content_type='application/x-ndjson',
)

client = Client(httpserver.url_for('/'))
response = client.generate('dummy', 'Why is the sky blue?', stream=True)

assert [part.response for part in response] == ['Because ', 'it is.']


async def test_async_client_chat_stream_skips_blank_lines(httpserver: HTTPServer):
"Blank lines in an NDJSON stream must not crash the async parser either."

httpserver.expect_ordered_request(
'/api/chat',
method='POST',
json={
'model': 'dummy',
'messages': [{'role': 'user', 'content': 'Why is the sky blue?'}],
'tools': [],
'stream': True,
},
).respond_with_data(
json.dumps({'model': 'dummy', 'message': {'role': 'assistant', 'content': 'Because '}}) + '\n\n' + json.dumps({'model': 'dummy', 'message': {'role': 'assistant', 'content': 'it is.'}, 'done': True}) + '\n',
content_type='application/x-ndjson',
)

client = AsyncClient(httpserver.url_for('/'))
response = await client.chat('dummy', messages=[{'role': 'user', 'content': 'Why is the sky blue?'}], stream=True)

assert [part.message.content async for part in response] == ['Because ', 'it is.']