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.
$ 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}
$ 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
$ curl -s http://localhost:11434/api/generate -d '{ "model": "gpt-oss:20b", "prompt": "Return OK.", "stream": false }' {"response":"OK","done":true}
$ 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}
$ python3 - <<'PY'
last = {"done": True, "done_reason": "stop"}
print(last["done"])
PY
True