API / stream-text-from-grok-api

API

Stream text from the Grok API

Stream text from the Grok API

Turn on "stream": true so tokens arrive over SSE as the model generates them. Every text-output model supports streaming. Image-output models do not. Prefer a client SDK to parse the event stream; raw chunks follow the chat.completion.chunk shape with delta.content until data: [DONE]. Get a key at console.x.ai.

Python (OpenAI SDK)

Point the OpenAI client at https://api.x.ai/v1 and pass stream=True. For reasoning models, raise the request timeout (for example 3600 seconds) so the client does not close early.

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("XAI_API_KEY"),
    base_url="https://api.x.ai/v1",
    timeout=3600.0,
)

stream = client.chat.completions.create(
    model="grok-4.6",
    messages=[
        {"role": "user", "content": "Write a short poem about streaming tokens."}
    ],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content
    if delta:
        print(delta, end="", flush=True)
print()

The xAI SDK exposes the same flow as chat.stream() on a chat created with client.chat.create(...). The AI SDK uses streamText with xai.responses('grok-4.6').

curl

curl https://api.x.ai/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -m 3600 \
  -N \
  -d '{
  "model": "grok-4.6",
  "messages": [
    {"role": "user", "content": "Write a short poem about streaming tokens."}
  ],
  "stream": true
}'

-m 3600 keeps the HTTP client open for long reasoning runs. -N disables curl buffering so events print as they arrive.

Pitfalls

  • Leaving the default short timeout on reasoning models — the stream can close before the final tokens.
  • Calling stream on an image-output model — text streaming is unsupported there.
  • Parsing SSE by hand without handling data: [DONE] and empty deltas — use the SDK unless you need raw events.