> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-sdk-testing-latest.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# ストリーミング応答を有効にする

> Serverless Inference でストリーミング出力を有効にすると、モデル応答を段階的に受け取れます。

`stream` オプションを `true` に設定すると、モデルの応答がチャンクのストリームとして段階的に返されるため、応答全体を待たずに、到着した結果から表示できます。これは、モデルが出力の生成に時間を要する場合に役立ちます。

ストリーミング出力は、すべての hosted モデルでサポートされます。[reasoning models](./reasoning) ではストリーミングを推奨します。ストリーミングでないリクエストでは、モデルが出力を開始するまでに時間がかかるとタイムアウトする可能性があるためです。

以下の例では、chat completion リクエストでストリーミングを有効にします。

<Tabs>
  <Tab title="Python">
    ```python theme={null}
    import openai

    client = openai.OpenAI(
        base_url='https://api.inference.wandb.ai/v1',
        api_key="[YOUR-API-KEY]",  # https://wandb.ai/settings でAPIキーを作成します
    )

    stream = client.chat.completions.create(
        model="openai/gpt-oss-120b",
        messages=[
            {"role": "user", "content": "Tell me a rambling joke"}
        ],
        stream=True,
    )

    for chunk in stream:
        if chunk.choices:
            print(chunk.choices[0].delta.content or "", end="", flush=True)
        else:
            print(chunk) # CompletionUsage object を表示します
    ```
  </Tab>

  <Tab title="Bash">
    ```bash theme={null}
    curl https://api.inference.wandb.ai/v1/chat/completions \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer [YOUR-API-KEY]" \
      -d '{
        "model": "openai/gpt-oss-120b",
        "messages": [
          { "role": "user", "content": "Tell me a rambling joke" }
        ],
        "stream": true
      }'
    ```
  </Tab>
</Tabs>
