> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pipellm.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 流式输出

> 边生成边接收对话 token。

在 Chat Completions 上设置 `stream: true`。PipeLLM 返回与 OpenAI 相同形状的 [SSE](https://html.spec.whatwg.org/multipage/server-sent-events.html)。

<CodeGroup>
  ```python Python theme={"dark"}
  import os
  from openai import OpenAI

  client = OpenAI(
      api_key=os.environ["PIPELLM_API_KEY"],
      base_url="https://api.pipellm.ai/v1",
  )

  stream = client.chat.completions.create(
      model="gpt-5",
      stream=True,
      messages=[{"role": "user", "content": "从 1 数到 5。"}],
  )

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

  ```typescript TypeScript theme={"dark"}
  import OpenAI from "openai";

  const client = new OpenAI({
    apiKey: process.env.PIPELLM_API_KEY,
    baseURL: "https://api.pipellm.ai/v1",
  });

  const stream = await client.chat.completions.create({
    model: "gpt-5",
    stream: true,
    messages: [{ role: "user", content: "从 1 数到 5。" }],
  });

  for await (const chunk of stream) {
    const delta = chunk.choices[0]?.delta?.content;
    if (delta) process.stdout.write(delta);
  }
  ```

  ```bash cURL theme={"dark"}
  curl https://api.pipellm.ai/v1/chat/completions \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $PIPELLM_API_KEY" \
    -N \
    -d '{
      "model": "gpt-5",
      "stream": true,
      "messages": [{"role": "user", "content": "从 1 数到 5。"}]
    }'
  ```
</CodeGroup>

Anthropic 和 Gemini 原生路由在打开各自协议的流式开关后同样支持流式（Messages 的 `stream: true`，或 Gemini 的 `:streamGenerateContent`）。视频生成是异步的，**不会**走流式，请轮询 [`GET /v2/videos/{id}`](/zh/api-reference/video/get)。

非流式响应结构见 [Chat Completions](/zh/api-reference/openai/chat-completions)。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.