New Tool Radar

Local AI

How to Read an Ollama Streaming Reply Line by Line

Call Ollama's API with streaming on and read the newline-delimited JSON objects as the reply arrives, instead of waiting for the whole answer.

Schematic diagram with four steps: call the API with streaming on, read each JSON line as it arrives, join the response chunks in order, then read the final object for timing stats.

Ollama’s generate and chat endpoints stream by default: the reply arrives as a series of JSON objects, one per line, and the final object carries the statistics. Reading the stream line by line is how chat apps show text appearing as it is generated instead of waiting for the whole answer.

Call with streaming on and parse each line

Send the request without setting "stream": false — streaming is the default. Each line of the response body is one JSON object with a response field holding the next chunk of text and a done field that is false until the last object. Accumulate the response fields in order to build the full answer, and read eval_count and eval_duration from the final object: tokens per second is eval_count / eval_duration * 10^9, because durations are in nanoseconds.

curl -N http://localhost:11434/api/generate -d '{
  "model": "llama3.2",
  "prompt": "Why is the sky blue?"
}'
# each line is JSON: {"response": "The", "done": false} ... {"done": true, "eval_count": 259, "eval_duration": 4232710000}

If you want one reply instead of a stream, send "stream": false and the response is a single JSON object whose response field holds the whole answer. The context field in the final object is an encoding of the conversation you can send in the next request to keep a short conversational memory — though the docs mark it deprecated in favour of sending the message history to /api/chat.

Try it: four checks

Check What you should see If you do not see it
Stream flows Lines of JSON arrive before the answer is complete Do not set stream to false
Chunks join Concatenating response fields gives the full answer Keep the lines in arrival order
Done seen A final object arrives with done true Wait for it before closing the connection
Speed computed eval_count / eval_duration gives tokens per second Durations are nanoseconds, multiply by 10^9

Expected result. We checked the Ollama API reference, which says streaming endpoints return a series of JSON objects, that stream can be disabled with stream false, that durations are in nanoseconds, and that tokens per second is eval_count / eval_duration * 10^9. We did not call the API on this machine.

Limits

This guide covers reading the stream only. The deprecated context field is not a substitute for proper message history in multi-turn chats. We did not test any streaming call ourselves.

Sources

See all