Streaming

The OpenRouter API allows streaming responses from any model. This is useful for building chat interfaces or other applications where the UI should update as the model generates the response.

To enable streaming, you can set the stream parameter to true in your request. The model will then stream the response to the client in chunks, rather than returning the entire response at once.

Here is an example of how to stream a response, and process it:

1import requests
2import json
3
4question = "How would you build the tallest building ever?"
5
6url = "https://openrouter.ai/api/v1/chat/completions"
7headers = {
8 "Authorization": f"Bearer {API_TOKEN}",
9 "Content-Type": "application/json"
10}
11
12payload = {
13 "model": "openai/gpt-4o",
14 "messages": [{"role": "user", "content": question}],
15 "stream": True
16}
17
18buffer = ""
19with requests.post(url, headers=headers, json=payload, stream=True) as r:
20 for chunk in r.iter_content(chunk_size=1024, decode_unicode=True):
21 buffer += chunk
22 while True:
23 try:
24 # Find the next complete SSE line
25 line_end = buffer.find('\n')
26 if line_end == -1:
27 break
28
29 line = buffer[:line_end].strip()
30 buffer = buffer[line_end + 1:]
31
32 if line.startswith('data: '):
33 data = line[6:]
34 if data == '[DONE]':
35 break
36
37 try:
38 data_obj = json.loads(data)
39 content = data_obj["choices"][0]["delta"].get("content")
40 if content:
41 print(content, end="", flush=True)
42 except json.JSONDecodeError:
43 pass
44 except Exception:
45 break

Additional Information

For SSE (Server-Sent Events) streams, OpenRouter occasionally sends comments to prevent connection timeouts. These comments look like:

: OPENROUTER PROCESSING

Comment payload can be safely ignored per the SSE specs. However, you can leverage it to improve UX as needed, e.g. by showing a dynamic loading indicator.

Some SSE client implementations might not parse the payload according to spec, which leads to an uncaught error when you JSON.stringify the non-JSON payloads. We recommend the following clients:

Was this page helpful?
Built with