GirtAI
API documentation
← All guides

DeepSeek API streaming: read incremental responses

Understand SSE, empty chunks, cancellation and partial responses.

What stream: true means

The response arrives as server-sent events rather than one JSON document. The SDK parses these events into chunks. Some chunks contain metadata or reasoning instead of visible content; never assume choices[0] or delta.content exists.

Avoid accidental buffering

When your own backend forwards a stream, disable proxy buffering for that route and flush data promptly. Keep a single request open instead of repeatedly sending the same prompt to poll for output.

Cancellation and failure

Close the stream when the user cancels. A disconnected client can still have consumed upstream tokens. Do not silently restart an interrupted stream: users may receive duplicate text and incur another charge. Check the usage record.

Try a small request first

Use the Python example below after installing openai and setting GIRTAI_API_KEY on your server. It limits output to 128 tokens and displays only content chunks.

Example — run on your server

import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["GIRTAI_API_KEY"],
                base_url="https://girtai.com/v1", timeout=60, max_retries=0)
with client.chat.completions.create(
    model="deepseek-flash",
    messages=[{"role": "user", "content": "Explain streaming in one sentence."}],
    max_tokens=128,
    stream=True,
) as stream:
    for chunk in stream:
        if chunk.choices and chunk.choices[0].delta.content:
            print(chunk.choices[0].delta.content, end="", flush=True)
print()
View prices & local peak hours