The router supports streaming responses from all providers (OpenAI, Anthropic, Mistral) using Server-Sent Events (SSE). Streaming allows you to receive and process the response token by token instead of waiting for the complete response.
import openaiclient = openai.OpenAI( api_key=ROUTER_API_KEY, base_url="https://router.requesty.ai/v1",)response = client.chat.completions.create( model="openai/gpt-4", messages=[{"role": "user", "content": "Write a poem about the stars."}], stream=True)# Iterate over the stream and handle chunksfor chunk in response: # Access content from the chunk (if present) if chunk.choices[0].delta.content is not None: content = chunk.choices[0].delta.content print(content, end="", flush=True) # Print content as it arrives # Handle function calls in streaming (if present) if hasattr(chunk.choices[0].delta, 'function_call'): fc = chunk.choices[0].delta.function_call if hasattr(fc, 'name') and fc.name: print(f"\nFunction Call: {fc.name}") if hasattr(fc, 'arguments') and fc.arguments: print(f"Arguments: {fc.arguments}")
collected_messages = []for chunk in response: if chunk.choices[0].delta.content is not None: content = chunk.choices[0].delta.content collected_messages.append(content)full_response = "".join(collected_messages)