How to stream Ollama API responses

Ollama streams several API responses as newline-delimited JSON so callers can display tokens as they arrive. Streaming lowers perceived latency for long generations, but the client must parse multiple JSON objects instead of one response body.

The stream ends when a chunk contains done set to true. For short scripts, disable streaming with stream: false; for chat interfaces, handle both partial content chunks and the final completion chunk.

Keep streaming examples small while validating the parser. Large prompts make it harder to tell whether a bug is in the model output, network buffering, or client-side JSON handling.

Steps to stream Ollama API responses:

  1. Send a default streaming generate request.
    $ curl http://localhost:11434/api/generate -d '{
      "model": "gpt-oss:20b",
      "prompt": "Return the word OK."
    }'
    {"model":"gpt-oss:20b","response":"O","done":false}
    {"model":"gpt-oss:20b","response":"K","done":false}
    {"model":"gpt-oss:20b","done":true}
  2. Parse each line as its own JSON object.
    $ python3 - <<'PY'
    import json
    for line in ['{"response":"O","done":false}', '{"response":"K","done":false}', '{"done":true}']:
        chunk = json.loads(line)
        if chunk.get('response'):
            print(chunk['response'], end='')
    print()
    PY
    OK
  3. Disable streaming when a single JSON response is easier to process.
    $ curl -s http://localhost:11434/api/generate -d '{
      "model": "gpt-oss:20b",
      "prompt": "Return OK.",
      "stream": false
    }'
    {"response":"OK","done":true}
  4. Handle chat streaming from message.content chunks.
    $ curl http://localhost:11434/api/chat -d '{"model":"gpt-oss:20b","messages":[{"role":"user","content":"Return OK."}]}'
    {"message":{"content":"O"},"done":false}
    {"message":{"content":"K"},"done":false}
    {"done":true}
  5. Verify the client waits until the final done chunk arrives.
    $ python3 - <<'PY'
    last = {"done": True, "done_reason": "stop"}
    print(last["done"])
    PY
    True