流式响应
Chat Completions 流式请求
Section titled “Chat Completions 流式请求”设置 stream: true 后,接口以 SSE 格式返回增量事件。
curl -N "${BASE_URL}/v1/chat/completions" \ -H "Authorization: Bearer ${API_KEY}" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4.1", "messages": [ { "role": "user", "content": "连续输出三条排查建议" } ], "stream": true, "stream_options": { "include_usage": true } }'SSE 事件形状
Section titled “SSE 事件形状”data: {"id":"chatcmpl_xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"第一"}}]}
data: {"id":"chatcmpl_xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"条建议"}}]}
data: {"id":"chatcmpl_xxx","object":"chat.completion.chunk","choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":20,"completion_tokens":30,"total_tokens":50}}
data: [DONE]客户端处理建议
Section titled “客户端处理建议”- 按 SSE 的
data:行逐条解析 - 收到
[DONE]后关闭本次流 - 如果需要在客户端展示用量,传入
stream_options.include_usage=true - 客户端断开后,本次请求可能仍会完成计费
Python SDK 示例
Section titled “Python SDK 示例”stream = client.chat.completions.create( model="gpt-4.1", messages=[{"role": "user", "content": "连续输出三条排查建议"}], stream=True, stream_options={"include_usage": True},)
for chunk in stream: if chunk.choices and chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="")