Fluxio
← Volver al blog

Streaming in Python with the OpenAI SDK

SSE y APIsEN

§1 How Python Receives Streaming AI Responses

Streaming lets your application display the beginning of an answer while the model continues generating the rest. The OpenAI SDK supports HTTP streaming through server-sent events, so you can process incremental output instead of waiting for a complete response. This walkthrough uses Chat Completions and its text deltas. ([developers.openai.com](https://developers.openai.com/api/docs/guides/streaming-responses))

Fluxio fits this workflow when you want a streaming-first, pay-per-token relay with one API key across multiple model providers. We will configure the SDK for Fluxio, send a short prompt, and assemble the answer while printing incoming text.

§2 Step 1: Install the OpenAI Python SDK

Create a project directory and install the package in your chosen Python environment. Run this command in your terminal:

python -m pip install --upgrade openai

The official SDK provides synchronous and asynchronous clients. Start with the synchronous client to keep the first streaming example easy to inspect. ([developers.openai.com](https://developers.openai.com/api/reference/python?utm_source=openai))

Configure two environment variables before running your script: FLUXIO_API_KEY for your relay key and FLUXIO_MODEL for the exact API model identifier. Choose an available model such as Gemini 2.5 Flash, but copy its identifier from your provider configuration rather than assuming its display name is accepted.

§3 Step 2: Configure the Python Client for Fluxio

Create a file named stream_demo.py. Add the following statements as separate lines. Keeping the model identifier configurable makes later comparisons easier without changing your streaming loop.

>>> import os
>>> from openai import OpenAI
>>> client = OpenAI(api_key=os.environ["FLUXIO_API_KEY"], base_url="https://api.fluxioapi.com/v1", timeout=60.0, max_retries=2)

This configuration sends requests to Fluxio rather than the SDK's default endpoint. The /v1 suffix belongs in the base URL for this OpenAI-style connection. Keep the key on your server and out of source control.

The SDK accepts explicit timeout and retry settings. Treat the values here as starting points for testing, not a guarantee that every request finishes within one minute across all attempts. ([developers.openai.com](https://developers.openai.com/api/reference/python?utm_source=openai))

§4 Step 3: Request and Print Streaming Text Deltas

Add these statements beneath the client configuration. Keep the entire for statement on one line; its semicolon-separated operations form the loop body.

parts = []

>>> stream = client.chat.completions.create(model=os.environ["FLUXIO_MODEL"], messages=[{"role": "user", "content": "Explain Python generators in three sentences."}], stream=True)

for chunk in stream: text = (chunk.choices[0].delta.content or "") if chunk.choices else ""; parts.append(text); print(text, end="", flush=True)

>>> print()

answer = "".join(parts)

>>> stream.close()
>>> client.close()

Chat Completions streaming chunks expose incremental content through delta rather than a completed message. Some chunks contain metadata or no text, and usage-only chunks can have an empty choices array. The guard handles those cases. Do not assume each chunk represents exactly one token. ([developers.openai.com](https://developers.openai.com/api/reference/resources/chat/subresources/completions/streaming-events))

Run python stream_demo.py. Check that text appears progressively and that answer contains the complete assembled response. The flush argument asks Python to write each printed fragment immediately rather than retaining it in its output buffer.

§5 Step 4: Handle Interrupted Python Streams Safely

Before deploying, wrap request creation and iteration in exception handling, and move resource cleanup into context managers or a finally block. Catch SDK connection, timeout, and API status errors at the application boundary. An error can occur after some text has already reached the user. ([developers.openai.com](https://developers.openai.com/api/reference/python?utm_source=openai))

  • – Preserve partial output and label interrupted answers as incomplete.
  • – Offer an explicit retry instead of silently appending a regenerated answer.
  • – Close the upstream stream when the user cancels generation.
  • – Test empty content, unavailable models, invalid credentials, and network interruptions.

§6 Step 5: Test Streaming Before Shipping Your AI Interface

Measure time to first visible text separately from total completion time. Test through your actual hosting stack, and check whether buffering prevents incremental delivery. Plan moderation carefully: partially displayed content is harder to evaluate than a finished answer. ([developers.openai.com](https://developers.openai.com/api/docs/guides/streaming-responses))

Fluxio is worth considering when a shared streaming integration and pay-per-token billing fit your product. Validate each selected model with the same prompt and cancellation tests before relying on interchangeable behavior.

Sign up for Fluxio to try streaming.

Crear una cuenta de Fluxio →

Más artículos de Corrientes