模型能力
Streaming
所有具备文本输出能力的模型(Chat、Image Understanding 等)都支持 streaming output。具备图像输出能力的模型(Image Generation)不支持该功能。
Streaming output 使用 Server-Sent Events(SSE),使服务器可以在 event stream 中返回增量内容。
Streaming response 可以提供实时反馈,并允许文本在生成时立即显示,从而增强用户交互体验。
要启用 streaming,必须在请求中设置 "stream": true。
import os
from xai_sdk import Client
from xai_sdk.chat import user, system
client = Client(
api_key=os.getenv('XAI_API_KEY'),
timeout=3600, # Override default timeout with longer timeout for reasoning models
)
chat = client.chat.create(model="grok-4.5")
chat.append(
system("You are Grok, a helpful and maximally truthful AI built by xAI."),
)
chat.append(
user("Explain how neural networks learn in two sentences.")
)
for response, chunk in chat.stream():
print(chunk.content, end="", flush=True) # Each chunk's content
print(response.content, end="", flush=True) # The response object auto-accumulates the chunks
print(response.content) # The full response你将获得类似下面的 event stream:
data: {
"id":"<completion_id>","object":"chat.completion.chunk","created":<creation_time>,
"model":"grok-4.5",
"choices":[{"index":0,"delta":{"content":"Ah","role":"assistant"}}],
"usage":{"prompt_tokens":41,"completion_tokens":1,"total_tokens":42,
"prompt_tokens_details":{"text_tokens":41,"audio_tokens":0,"image_tokens":0,"cached_tokens":0}},
"system_fingerprint":"fp_xxxxxxxxxx"
}
data: {
"id":"<completion_id>","object":"chat.completion.chunk","created":<creation_time>,
"model":"grok-4.5",
"choices":[{"index":0,"delta":{"content":",","role":"assistant"}}],
"usage":{"prompt_tokens":41,"completion_tokens":2,"total_tokens":43,
"prompt_tokens_details":{"text_tokens":41,"audio_tokens":0,"image_tokens":0,"cached_tokens":0}},
"system_fingerprint":"fp_xxxxxxxxxx"
}
data: [DONE]建议使用客户端 SDK 解析 event stream。
Python/Javascript 中的 streaming response 示例:
Neural 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.