<!-- Generated from public website sources; do not edit this file. -->

# Streaming responses
Display a response progressively instead of waiting for the complete answer.

## Before you start
Complete [your first API call](https://deepseeksub.com/docs/quickstart/index.md), install the Python `openai` package and keep the same environment variables. Your provider must support streaming Chat Completions.

## Read chunks with Python
Enable `stream=True`. A streaming response supplies incremental `delta` objects rather than one complete message. Some chunks contain no text, so check the content before printing it.

```python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["CODING_PLAN_API_KEY"],
    base_url=os.environ["CODING_PLAN_BASE_URL"],
)
with client.chat.completions.create(
    model=os.environ["CODING_PLAN_MODEL"],
    messages=[{"role": "user", "content": "Explain recursion briefly."}],
    stream=True,
) as stream:
    for chunk in stream:
        if not chunk.choices:
            continue
        text = chunk.choices[0].delta.content
        if text:
            print(text, end="", flush=True)
print()
```

## Understand the stream
Chat Completions streaming uses server-sent events. Let the SDK decode them; a network packet is not necessarily one event. Text may arrive in small fragments, and a final event may have an empty text delta.

Reasoning models may also return reasoning fields. This example displays the answer content only. Consult your provider's response schema before handling additional fields.

## Handle interruptions
Keep a visible distinction between an answer that finished and a partial answer from a dropped connection. Use a timeout appropriate to your application, allow cancellation, and close the stream when the reader stops.

Do not automatically append a restarted request to a partial answer: a new generation can differ and may consume additional allowance. See [error codes](https://deepseeksub.com/docs/error-codes/index.md) for retry guidance.

## Track usage
Usage fields may depend on your provider's streaming options. Do not estimate billed usage from the number of chunks or characters displayed. Compare returned usage with the provider's usage dashboard.

Reference: [DeepSeek Chat Completions API](https://api-docs.deepseek.com/api/create-chat-completion/).

---

HTML source: [Streaming responses — DeepSeek Subscription Tutorials](https://deepseeksub.com/docs/streaming/)
