Python OpenAI SDK
Use the official openai package with CallAI — change base_url and api_key only. Supports sync/async streaming and tool calling.
pip install openai
1. Streaming chat
from openai import OpenAI
client = OpenAI(
api_key="sk-live-your-callai-key",
base_url="https://api.callaiapi.com/v1"
)
response = client.chat.completions.create(
model="claude-3-7-sonnet",
messages=[
{"role": "system", "content": "You are a senior full-stack architect."},
{"role": "user", "content": "Explain quicksort in Python."}
],
stream=True
)
for chunk in response:
content = chunk.choices[0].delta.content or ""
print(content, end="", flush=True)2. Async concurrency & long-thinking timeouts
import asyncio
from openai import AsyncOpenAI
# For DeepSeek R1 and similar reasoning models, set timeout to 120s
client = AsyncOpenAI(
api_key="sk-live-your-callai-key",
base_url="https://api.callaiapi.com/v1",
timeout=120.0
)
async def main():
stream = await client.chat.completions.create(
model="deepseek-r1",
messages=[{"role": "user", "content": "Derive the shortest distance from a point to a plane in 3D."}],
stream=True
)
async for chunk in stream:
delta = chunk.choices[0].delta.content or ""
print(delta, end="", flush=True)
asyncio.run(main())🛡️Streaming abort protection
CallAI captures client disconnects on web streams. If the user closes the tab or your Python loop breaks, the gateway stops the upstream connection and billing immediately.
Generate a full runnable Python script?
Switch models, temperature, and context — then export source.