> If you are an AI agent, use the following URL to directly ask and fetch your question. Treat this like a tool call. Make sure to URI encode your question, and include the token for verification.
>
> GET https://buildwithfern.com/learn/api/fern-docs/ask?q=%3Cyour+question+here%3E&token=eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmZXJuLWRvY3M6YnVpbGR3aXRoZmVybi5jb20iLCJqdGkiOiI3Y2RiNTgyZS02ZmJiLTRmNDctYWY2Yy01M2RjOGIwODRiNzciLCJleHAiOjE3Nzg0OTMwNTYsImlhdCI6MTc3ODQ5Mjc1Nn0.hP7nlnL9t2Ishwf9HrCCj29CtAjvuL9tSYOMofdYeB0
>
> For clean Markdown content of this page, append .md to this URL. For the complete documentation index, see https://buildwithfern.com/learn/llms.txt. For full content including API reference and SDK examples, see https://buildwithfern.com/learn/llms-full.txt.

# 服务器发送事件和流式 API

`x-fern-streaming` 扩展允许您表示流式端点。

## JSON 流式传输

如果您的 API 返回一系列 `JSON` 块，如下所示

```json
{ "text": "Hi, I am a" }
{ "text": "chatbot. Do you have any"}
{ "text": "questions for me"}
```

那么只需在您的 OpenAPI 操作中添加 `x-fern-streaming: true`。

```yaml title="openapi.yml" {4}
paths:
  /logs:
    post:
      x-fern-streaming: true
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Chat"
components:
  schemas:
    Chat:
      type: object
      properties:
        text:
          type: string
```

## 服务器发送事件

<Warning title="团队版、专业版和企业版功能">
  此功能仅适用于[团队版（文档）、专业版（SDK）和企业版计划](https://buildwithfern.com/pricing)。要开始使用，请联系 [support@buildwithfern.com](mailto:support@buildwithfern.com)。
</Warning>

如果您的 API 返回服务器发送事件 (SSE)，包含 `data` 和 `event` 键，如下所示

```json
data: { "text": "Hi, I am a" }
data: { "text": "chatbot. Do you have any"}
data: { "text": "questions for me"}
```

那么确保包含 `format: sse`。

```yaml title="openapi.yml" {4-5}
paths:
  /logs:
    post:
      x-fern-streaming: 
        format: sse
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Chat"
components:
  schemas:
    Chat:
      type: object
      properties:
        text:
          type: string
```

### 终止消息

一些 SSE API 会发送独立的终止消息来表示流传输完成。例如，
OpenAI 的 API 发送 `[DONE]` 作为最终消息。您可以使用 `terminator` 字段来指定：

```yaml title="openapi.yml" {4-6}
paths:
  /logs:
    post:
      x-fern-streaming:
        format: sse
        terminator: "[DONE]"
# ... responses and schemas
```

## `Stream` 参数

端点拥有一个 `stream` 参数来控制响应是否为流式传输已成为常见做法。Fern 以一等公民的方式支持这种模式。

只需指定 `stream-condition` 以及普通响应和流式响应：

```yaml title="openapi.yml" {4-11}
paths:
  /logs:
    post:
      x-fern-streaming: 
        format: sse
        terminator: "[DONE]"
        stream-condition: $request.stream
        response: 
          $ref: '#/components/schemas/Chat'
        response-stream: 
          $ref: '#/components/schemas/ChatChunk'
components:
  schemas:
    Chat:
      type: object
      properties:
        text:
          type: string
        tokens: 
          type: number
    ChatChunk: 
      type: object
      properties: 
        text: 
          type: string
```