Repository Public
1<!-- source: https://docs.x.ai/developers/model-capabilities/text/streaming (from https://docs.x.ai/llms-full.txt, retrieved 2026-09-19T05:28:55Z) -->2#### Model Capabilities34# Streaming56Streaming outputs is **supported by all models with text output capability** (Chat, Image Understanding, etc.). It is **not supported by models with image output capability** (Image Generation).78Streaming outputs uses [Server-Sent Events (SSE)](https://en.wikipedia.org/wiki/Server-sent_events) that let the server send back the delta of content in event streams.910Streaming responses are beneficial for providing real-time feedback, enhancing user interaction by allowing text to be displayed as it's generated.1112To enable streaming, you must set `"stream": true` in your request.1314> [!CAUTION]15>16> When using streaming output with reasoning models, you might want to **manually override request17> timeout** to avoid prematurely closing connection.1819```pythonXAI20import os2122from xai_sdk import Client23from xai_sdk.chat import user, system2425client = Client(26 api_key=os.getenv('XAI_API_KEY'),27 timeout=3600,28)2930chat = client.chat.create(model="grok-4.6")31chat.append(32 system("You are Grok, a helpful and useful AI built by xAI."),33)34chat.append(35 user("Explain how neural networks learn in two sentences.")36)3738for response, chunk in chat.stream():39 print(chunk.content, end="", flush=True)4041print()42print(response.content)43```4445```pythonOpenAISDK46import os47import httpx48from openai import OpenAI4950client = OpenAI(51 api_key=os.getenv("XAI_API_KEY"),52 base_url="https://api.x.ai/v1",53 timeout=httpx.Timeout(3600.0)54)5556stream = client.chat.completions.create(57 model="grok-4.6",58 messages=[59 {"role": "system", "content": "You are Grok, a helpful and useful AI built by xAI."},60 {"role": "user", "content": "Explain how neural networks learn in two sentences."},61 ],62 stream=True63)6465for chunk in stream:66 if chunk.choices[0].delta.content:67 print(chunk.choices[0].delta.content, end="", flush=True)68```6970```javascriptOpenAISDK71import OpenAI from "openai";72const openai = new OpenAI({73 apiKey: process.env.XAI_API_KEY,74 baseURL: "https://api.x.ai/v1",75 timeout: 360000,76});7778const stream = await openai.chat.completions.create({79 model: "grok-4.6",80 messages: [81 { role: "system", content: "You are Grok, a helpful and useful AI built by xAI." },82 {83 role: "user",84 content: "Explain how neural networks learn in two sentences.",85 }86 ],87 stream: true88});8990for await (const chunk of stream) {91 const content = chunk.choices[0].delta.content;92 if (content) {93 process.stdout.write(content);94 }95}96```9798```javascriptAISDK99import { xai } from '@ai-sdk/xai';100import { streamText } from 'ai';101102const result = streamText({103 model: xai.responses('grok-4.6'),104 system:105 "You are Grok, a helpful and useful AI built by xAI.",106 prompt: 'Explain how neural networks learn in two sentences.',107});108109for await (const chunk of result.textStream) {110 process.stdout.write(chunk);111}112```113114```bash115curl https://api.x.ai/v1/chat/completions \\116-H "Content-Type: application/json" \\117-H "Authorization: Bearer $XAI_API_KEY" \\118-m 3600 \\119-d '{120 "messages": [121 {122 "role": "system",123 "content": "You are Grok, a helpful and useful AI built by xAI."124 },125 {126 "role": "user",127 "content": "Explain how neural networks learn in two sentences."128 }129 ],130 "model": "grok-4.6",131 "stream": true132}'133```134135You'll get the event streams like these:136137```json138data: {139 "id":"<completion_id>","object":"chat.completion.chunk","created":<creation_time>,140 "model":"grok-4.6",141 "choices":[{"index":0,"delta":{"reasoning_content":"The","role":"assistant"}}],142 "usage":{"prompt_tokens":41,"completion_tokens":1,"total_tokens":42,143 "prompt_tokens_details":{"text_tokens":41,"audio_tokens":0,"image_tokens":0,"cached_tokens":0}},144 "system_fingerprint":"fp_xxxxxxxxxx"145}146147data: {148 "id":"<completion_id>","object":"chat.completion.chunk","created":<creation_time>,149 "model":"grok-4.6",150 "choices":[{"index":0,"delta":{"content":"Ah","role":"assistant"}}],151 "usage":{"prompt_tokens":41,"completion_tokens":1,"total_tokens":42,152 "prompt_tokens_details":{"text_tokens":41,"audio_tokens":0,"image_tokens":0,"cached_tokens":0}},153 "system_fingerprint":"fp_xxxxxxxxxx"154}155156data: {157 "id":"<completion_id>","object":"chat.completion.chunk","created":<creation_time>,158 "model":"grok-4.6",159 "choices":[{"index":0,"delta":{"content":",","role":"assistant"}}],160 "usage":{"prompt_tokens":41,"completion_tokens":2,"total_tokens":43,161 "prompt_tokens_details":{"text_tokens":41,"audio_tokens":0,"image_tokens":0,"cached_tokens":0}},162 "system_fingerprint":"fp_xxxxxxxxxx"163}164165data: [DONE]166```167168It is recommended that you use a client SDK to parse the event stream.169170Example streaming responses in Python/Javascript:171172```173Neural networks learn by adjusting connection weights to minimize prediction error. Through backpropagation, they propagate gradients backward through layers so each weight updates in the direction that improves accuracy on training data.174```175