Chat API(Python)导读
如果你的 Python 主要场景还是 chat completions,这页会先说明 client.chat 的关键用法。
Python SDK 里的 client.chat 更像是迁移入口,而不是终点。 如果你已经有大量 OpenAI-compatible chat 代码,这条路径最低成本; 但如果你开始追求更统一的工具、结构化输出和状态语义,就应该逐步评估 Responses API。
可用方法
client.chat.completions.create():创建一次标准聊天补全请求。client.chat.completions.stream():按增量方式读取流式结果。client.chat.sessions.list():列出持久化聊天 session。
最小示例
Python
from therouter import TheRouter
client = TheRouter(api_key=os.getenv("THEROUTER_API_KEY"))
completion = client.chat.completions.create(
model="anthropic/claude-sonnet-4.5",
messages=[{"role": "user", "content": "给我一份 launch checklist。"}],
)
print(completion.choices[0].message.content)核心参数
| Name | Type | Required | Description |
|---|---|---|---|
model | string | Required | 模型 ID,例如 anthropic/claude-sonnet-4.5。 |
messages | array | Required | 按顺序传入的对话消息。 |
stream | boolean | 设为 true 时返回流式 SSE 结果。 | |
tools | array | 如果你要做原始工具调用,这里传 OpenAI-compatible tools。 |
什么时候考虑 Responses API
如果你开始在 chat 接口里叠加 tools、复杂输出和多轮状态,通常就值得认真评估 Responses API。