Skip to content

Ollama Docs, Revised

Streaming

Task page. 7 changes, 3 questions for the owner. Audit: improved. Original source at commit 318ee28.

Revised

Streaming lets you render text as the model produces it.1

Streaming is on by default through the REST API, but off by default in the SDKs.2

To turn on streaming in the SDKs,3 set the stream parameter to True.?1

Key streaming concepts?2

  • 4Chatting: Stream partial assistant messages. Each chunk includes the content so you can render messages as they arrive.
  • Thinking: Thinking-capable models emit a thinking field alongside regular content in each chunk. Detect this field in streaming chunks to show or hide reasoning traces before the final answer arrives.
  • Tool calling: Watch for streamed tool_calls in each chunk, run5 the requested tool, and append tool outputs back into the conversation.

Handle streamed chunks6

Note: Accumulate the partial fields to keep the conversation history. This matters most for tool calling, where you must pass the thinking, the model's tool call, and the result of that call back to the model in the next request.7?3

Python

from ollama import chat

stream = chat(
  model='qwen3',
  messages=[{'role': 'user', 'content': 'What is 17 × 23?'}],
  stream=True,
)

in_thinking = False
content = ''
thinking = ''
for chunk in stream:
  if chunk.message.thinking:
    if not in_thinking:
      in_thinking = True
      print('Thinking:\n', end='', flush=True)
    print(chunk.message.thinking, end='', flush=True)
    # accumulate the partial thinking 
    thinking += chunk.message.thinking
  elif chunk.message.content:
    if in_thinking:
      in_thinking = False
      print('\n\nAnswer:\n', end='', flush=True)
    print(chunk.message.content, end='', flush=True)
    # accumulate the partial content
    content += chunk.message.content

  # append the accumulated fields to the messages for the next request
  new_messages = [{ role: 'assistant', thinking: thinking, content: content }]

JavaScript

import ollama from 'ollama'

async function main() {
  const stream = await ollama.chat({
    model: 'qwen3',
    messages: [{ role: 'user', content: 'What is 17 × 23?' }],
    stream: true,
  })

  let inThinking = false
  let content = ''
  let thinking = ''

  for await (const chunk of stream) {
    if (chunk.message.thinking) {
      if (!inThinking) {
        inThinking = true
        process.stdout.write('Thinking:\n')
      }
      process.stdout.write(chunk.message.thinking)
      // accumulate the partial thinking
      thinking += chunk.message.thinking
    } else if (chunk.message.content) {
      if (inThinking) {
        inThinking = false
        process.stdout.write('\n\nAnswer:\n')
      }
      process.stdout.write(chunk.message.content)
      // accumulate the partial content
      content += chunk.message.content
    }
  }

  // append the accumulated fields to the messages for the next request
  new_messages = [{ role: 'assistant', thinking: thinking, content: content }]
}

main().catch(console.error)

Notes on this page

  1. 1 Wording Audit: improved Uses "lets you" and the active voice.

    Was: Streaming allows you to render text as it is produced by the model.

    LetsYou PronounContractions prin-plain-words

    Auditor: “Lets you” is more direct than “allows you to,” with the same meaning.

  2. 2 Wording Audit: improved Says "on" and "off" instead of "enabled" and "disabled".

    Was: Streaming is enabled by default through the REST API, but disabled by default in the SDKs.

    TurnOn

    Auditor: “On” and “off” state the same defaults in plainer words.

  3. 3 Wording Audit: improved Uses "turn on" instead of "enable".

    Was: To enable streaming in the SDKs,

    TurnOn

    Auditor: “Turn on” is a plainer equivalent of “enable,” and the instruction is unchanged.

  4. 4 Structure Audit: improved The three concepts have no order, so the numbered list became bullets.

    Was: 1. Chatting: ... 1. Thinking: ... 1. Tool calling:

    list-numbered-for-sequence

    Auditor: Bullets fit these unordered concepts better than numbered items.

  5. 5 Wording Audit: improved Uses "run" instead of "execute".

    Was: execute

    Run code-run-not-execute

    Auditor: “Run” is plainer than “execute” and preserves the instruction.

  6. 6 Wording Audit: improved Starts the heading with a verb, as a task heading does.

    Was: ## Handling streamed chunks

    proc-imperative

    Auditor: “Handle streamed chunks” makes the heading more action-oriented while still describing its section.

  7. 7 Wording Audit: improved Leads with the action, drops the passive and the wordy phrasing, and says who passes the results back.

    Was: It is necessary to accumulate the partial fields in order to maintain the history of the conversation. This is particularly important for tool calling where the thinking, tool call from the model, and the executed tool result must be passed back to the model in the next request.

    PronounContractions prin-lead-with-the-answer Run

    Auditor: The note leads with the action and explains more directly what to pass back and when.

Questions for the owner

  • ?1 The JavaScript example sets stream: true, but this sentence gives True; should the sentence name both spellings?

    About: “To enable streaming in the SDKs, set the stream parameter to True.”

  • ?2 This page opens with an overview of three concepts and then shows code; should the overview move to a separate concept page?

    About: “## Key streaming concepts”

  • ?3 The last line of the Python example writes { role: 'assistant', ... } with unquoted keys, and the JavaScript example assigns new_messages without declaring it; are both intended?

    About: “must be passed back to the model in the next request.”

Original

Streaming allows you to render text as it is produced by the model.

Streaming is enabled by default through the REST API, but disabled by default in the SDKs.

To enable streaming in the SDKs, set the stream parameter to True.

Key streaming concepts

  1. Chatting: Stream partial assistant messages. Each chunk includes the content so you can render messages as they arrive.
  2. Thinking: Thinking-capable models emit a thinking field alongside regular content in each chunk. Detect this field in streaming chunks to show or hide reasoning traces before the final answer arrives.
  3. Tool calling: Watch for streamed tool_calls in each chunk, execute the requested tool, and append tool outputs back into the conversation.

Handling streamed chunks

Note: It is necessary to accumulate the partial fields in order to maintain the history of the conversation. This is particularly important for tool calling where the thinking, tool call from the model, and the executed tool result must be passed back to the model in the next request.

Python

from ollama import chat

stream = chat(
  model='qwen3',
  messages=[{'role': 'user', 'content': 'What is 17 × 23?'}],
  stream=True,
)

in_thinking = False
content = ''
thinking = ''
for chunk in stream:
  if chunk.message.thinking:
    if not in_thinking:
      in_thinking = True
      print('Thinking:\n', end='', flush=True)
    print(chunk.message.thinking, end='', flush=True)
    # accumulate the partial thinking 
    thinking += chunk.message.thinking
  elif chunk.message.content:
    if in_thinking:
      in_thinking = False
      print('\n\nAnswer:\n', end='', flush=True)
    print(chunk.message.content, end='', flush=True)
    # accumulate the partial content
    content += chunk.message.content

  # append the accumulated fields to the messages for the next request
  new_messages = [{ role: 'assistant', thinking: thinking, content: content }]

JavaScript

import ollama from 'ollama'

async function main() {
  const stream = await ollama.chat({
    model: 'qwen3',
    messages: [{ role: 'user', content: 'What is 17 × 23?' }],
    stream: true,
  })

  let inThinking = false
  let content = ''
  let thinking = ''

  for await (const chunk of stream) {
    if (chunk.message.thinking) {
      if (!inThinking) {
        inThinking = true
        process.stdout.write('Thinking:\n')
      }
      process.stdout.write(chunk.message.thinking)
      // accumulate the partial thinking
      thinking += chunk.message.thinking
    } else if (chunk.message.content) {
      if (inThinking) {
        inThinking = false
        process.stdout.write('\n\nAnswer:\n')
      }
      process.stdout.write(chunk.message.content)
      // accumulate the partial content
      content += chunk.message.content
    }
  }

  // append the accumulated fields to the messages for the next request
  new_messages = [{ role: 'assistant', thinking: thinking, content: content }]
}

main().catch(console.error)

What Changed

Reworded the intro and the note to the standard's plain words and active voice, and changed the numbered list of concepts to bullets because the order doesn't matter. The wording check findings are cleared. Three questions ask the owner about the stream setting in JavaScript, the page's mix of overview and steps, and the message shape in the code.

Content type: task. The page shows how to stream a chat response and handle the chunks, which is one job for a reader who is ready to build it.

Automated check, original: 0 errors, 2 warnings, 4 suggestions. Revised: 0 errors, 0 warnings, 0 suggestions.

Independent audit (GPT-6): improved. The revision makes the prose more direct and the concept list easier to scan without changing its meaning. It preserves the code and link targets, and its questions flag unresolved issues for the documentation owner.

What the revision missed:

  • The page ends with code but no next action for readers, although the standard says to end with an action when the reader has something to do.
  • The examples contain apparent code issues: the Python example uses unquoted dictionary keys, and the JavaScript example assigns to undeclared new_messages. The revision appropriately raises these for the owner, but they remain unresolved in the page.

Structure (1)

  • 4 The three concepts have no order, so the numbered list became bullets. improved

Wording (6)

  • 1 Uses "lets you" and the active voice. improved
  • 2 Says "on" and "off" instead of "enabled" and "disabled". improved
  • 3 Uses "turn on" instead of "enable". improved
  • 5 Uses "run" instead of "execute". improved
  • 6 Starts the heading with a verb, as a task heading does. improved
  • 7 Leads with the action, drops the passive and the wordy phrasing, and says who passes the results back. improved