EN ▾
Get API key
OpenAI-compatible uncensored API for developers.Updated

OpenAI Compatible API Streaming in Python and Node

OpenAI compatible API streaming works the same way here as against OpenAI: set stream: true, point the SDK at our base URL, and read server-sent events as they arrive. This page has working Python and Node code, plus what to do when a stream drops.

Glowing blocks flowing one by one from a server to a laptop, illustrating token streaming

What streaming gives you

Without streaming, the API holds the whole answer until the model finishes, then sends it as one JSON body. For a long reply that can mean many seconds of a blank screen. With stream: true the server sends the same answer as a series of small events, each carrying a few characters of the reply, and your UI can print them as they come.

The wire format is server-sent events (SSE) over a normal HTTP response. Each event is a line starting with data: followed by a JSON chunk, and the stream ends with data: [DONE]. You rarely parse this by hand, because the OpenAI SDKs do it for you. If you want to see the raw bytes, the developer overview shows the endpoint layout and the API docs list every parameter.

Two things are worth deciding before you write code. First, who reads the stream: a browser talking to your server, or your server alone. Second, what you do when the connection breaks halfway. The rest of this page covers both.

Python: stream with the OpenAI SDK

Install the SDK with pip install openai, create a client with our base URL, and pass stream=True. The model ID is uncensored.

from openai import OpenAI

client = OpenAI(
    base_url="https://api.uncensoredllmapi.com/v1",
    api_key="YOUR_KEY",
)

stream = client.chat.completions.create(
    model="uncensored",
    messages=[{"role": "user", "content": "Write a four-line poem about rain."}],
    stream=True,
)

text = []
usage = None
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        piece = chunk.choices[0].delta.content
        text.append(piece)
        print(piece, end="", flush=True)
    if getattr(chunk, "usage", None):
        usage = chunk.usage

print()
print("tokens:", usage)

Notice the guard chunk.choices and .... The final chunk that carries usage can have an empty choices list, and indexing into it would raise an error. Collecting pieces in a list and joining at the end is also cheaper than repeated string concatenation on long replies.

Node: the same call in JavaScript

With the official openai package for Node the stream is an async iterator. This example uses ES modules.

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.uncensoredllmapi.com/v1",
  apiKey: process.env.API_KEY,
});

const stream = await client.chat.completions.create({
  model: "uncensored",
  messages: [{ role: "user", content: "Name three uses for a stream." }],
  stream: true,
});

let full = "";
let usage = null;
for await (const chunk of stream) {
  const piece = chunk.choices?.[0]?.delta?.content;
  if (piece) {
    full += piece;
    process.stdout.write(piece);
  }
  if (chunk.usage) usage = chunk.usage;
}
console.log("\nusage:", usage);

If you do not want the SDK, fetch works too: send the same JSON body with stream: true, then read response.body with a TextDecoder and split on blank lines. The SDK saves you from handling events that are cut in half at a chunk boundary, which is the usual bug in hand-written parsers.

Reading usage and balance

Usage is reported at the end of the stream. That is the number to log for cost tracking, because it is what your balance is charged against. At $0.15 per 1M input tokens and $0.50 per 1M output tokens, a reply with 1,200 input tokens and 800 output tokens costs 1,200 x 0.15 / 1,000,000 + 800 x 0.50 / 1,000,000 = $0.00018 + $0.0004 = $0.00058.

Every response also carries an X-Balance-USD header with your remaining credit, so you can show a low-balance warning without a second request. You can also call GET /v1/balance on a timer. Failed or refused requests are not charged, which means an error before the first token costs nothing.

The same response headers include X-Request-Id. Store it next to your own log line for each stream. If a stream behaves oddly, that ID is what you give support so the request can be found.

Dropped streams and what to do about them

Streams break. A phone switches from Wi-Fi to mobile data, a proxy closes an idle connection, a user closes the tab. Plan for it from the start.

Keep the partial text

Append every piece to a buffer as it arrives, and save the buffer when the loop ends or throws. If the stream failed, you have a usable half-reply. Show it with a retry button instead of discarding it.

Retry or continue

The simplest recovery is to resend the original request. For a short reply that is fine. For a long one, you can send the saved partial text as an assistant message and ask the model to continue from where it stopped. The result may not match what the first stream would have produced, so treat it as a best effort and label it in your UI if exactness matters.

Close what you abandon

A streamed request occupies its concurrency slot until the stream ends. A full key allows 8 requests at once, and a trial key only 2. If your server keeps reading a stream for a user who has gone, that slot stays busy. Cancel the request when the client disconnects. In Python, call stream.close(); in Node, call stream.controller.abort().

Relaying a stream to a browser

Never put your API key in browser code. The usual setup is a small endpoint on your own server that calls the API with stream: true and forwards each piece to the page. In Node with Express, set Content-Type: text/event-stream, write each piece as data: ... followed by a blank line, and listen for the close event on the request to abort the upstream call. On the page, EventSource handles GET endpoints, while fetch with a reader handles POST bodies, which is what you need when you send a message history.

Keep the message history on your server or in the request body, and trim the oldest turns when the conversation gets long. The context window is 100,000 tokens for prompt plus completion, which is a lot of chat, but a long-running session will reach it eventually.

Rate limits while streaming

The limits are 300 requests per minute and 8 concurrent requests on a full key, and 60 per minute and 2 concurrent on a trial key. Going over returns HTTP 429. The error code tells you which limit you hit: rate_limited means too many requests per minute, and concurrency means too many at once. On temporary overload the API also returns 429 with a Retry-After: 5 header.

Since a 429 arrives before any tokens are streamed, handle it before you start reading. A small retry wrapper is enough:

import time
from openai import OpenAI, RateLimitError

client = OpenAI(base_url="https://api.uncensoredllmapi.com/v1", api_key="YOUR_KEY")

def open_stream(messages, tries=4):
    for n in range(tries):
        try:
            return client.chat.completions.create(
                model="uncensored", messages=messages, stream=True
            )
        except RateLimitError as e:
            wait = 5 if n == 0 else 5 * (n + 1)
            time.sleep(wait)
    raise RuntimeError("still rate limited")

For chat apps with many users, the real fix for concurrency errors is a small queue on your side that allows no more than 8 live streams at a time.

Checklist before shipping a streaming chat

  • Guard against empty choices in the last chunk.
  • Buffer partial text and persist it on error.
  • Abort the upstream request when the browser disconnects.
  • Cap live streams at your concurrency limit.
  • Log usage and the request ID per stream.
  • Turn off response buffering on any reverse proxy in front of your server, or tokens will arrive in bursts.

The last point trips up many first deployments. Nginx, for example, buffers proxied responses by default, so the stream works on localhost and looks frozen in production until buffering is disabled for that route. For JSON output instead of free text, see the notes on JSON mode. To try the code above, create an API key; new accounts can claim $0.50 of trial credit with one click, valid for 7 days. Top-ups are crypto only (USDT on TRC20 or USDC on Base), from $20.

Questions and answers

Do I need a special SDK to stream?

No. The standard OpenAI Python and Node packages work. Change the base URL, the key and the model name to uncensored, then pass stream: true.

Can I stream tool calls and JSON mode?

Yes for tool calls: they are streamed as tool_calls deltas, and you join the pieces before parsing the arguments. Test any JSON-mode stream the same way, parsing only after the last chunk.

Am I charged for a stream that I cancel?

Failed or refused requests are not charged. For a stream you cancel midway, check the usage and X-Balance-USD values in your own logs to see what was billed.

Why does my stream arrive all at once?

A proxy or CDN in front of your app is probably buffering the response. Disable buffering for the streaming route and check again.

Your key is one form away

Create an account, copy the key, change the base URL. That is the whole setup.