Chat API (Python)

Methods, examples, and parameters for client.chat.

Module Overview

OpenAI-compatible chat APIs with TheRouter.ai routing, guardrails, and fallbacks.

Available Methods

  • client.chat.completions.create() - Create a chat completion.
  • client.chat.completions.stream() - Stream chat completion chunks.
  • client.chat.sessions.list() - List persisted chat sessions.

Examples

Python
from therouter import TheRouter.ai

client = TheRouter.ai(
    api_key=os.getenv("THEROUTER_API_KEY"),
    base_url="https://api.therouter.ai/v1",
)

completion = client.chat.completions.create(
    model="anthropic/claude-sonnet-4.5",
    messages=[{"role": "user", "content": "Plan a launch checklist."}],
)

print(completion.choices[0].message.content)
chat-response.json
{
  "id": "req_01HXYZ123",
  "module": "chat",
  "status": "ok"
}

Parameter Types

NameTypeRequiredDescription
model
stringRequiredModel ID, for example anthropic/claude-sonnet-4.5.
messages
arrayRequiredConversation messages in order.
stream
booleanEnable SSE streaming response.
SDK parity
Method signatures are aligned across SDKs so migration between TypeScript and Python stays predictable.