自定义超时、重试、备选
所有配置项通过 turing_options 传入。
Timeout 超时
| 参数 | 默认值 | 范围 |
|---|---|---|
timeout | stream: 90s, non-stream: 120s | 0-300 秒 |
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="turing/gpt-4.1",
messages=[
{"role": "user", "content": "Hello!"}
],
turing_options={
"timeout": 30 # 单位秒
}
)
curl $TURING_BASE_URL/chat/completions \
-H "Authorization: Bearer $TURING_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "turing/gpt-4.1",
"messages": [
{"role": "user", "content": "Hello!"}
],
"turing_options": {
"timeout": 30
}
}'
Retry 重试
| 参数 | 默认值 | 范围 |
|---|---|---|
max_retries | 0 | 1-3 |
completion = client.chat.completions.create(
model="turing/gpt-4.1",
messages=[{"role": "user", "content": "Hello!"}],
turing_options={
"max_retries": 2
}
)
Fallback 备选
| 参数 | 类型 |
|---|---|
fallbacks | string | object | array |
参数继承规则
| 参数 | 行为 |
|---|---|
messages、tools、temperature、stream 等请求参数 | 自动继承 |
turing_options.timeout | 自动继承 |
turing_options.max_retries、turing_options.fallbacks | 不继承 |
字符串格式
turing_options={
"fallbacks": "doubao-seed-2.1-turbo"
}
对象格式(可覆盖参数)
turing_options={
"fallbacks": {
"model": "doubao-seed-2.1-turbo",
"thinking": {"type": "enabled"},
"temperature": 0.7
}
}
数组格式(多个备选)
turing_options={
"fallbacks": [
"doubao-seed-2.1-turbo",
"turing/gpt-5.4-mini"
]
}
Auto 模式(自动备选)
设置 fallbacks: "auto" 后,平台会根据当前模型自动生成跨厂商的 fallback 链。
turing_options={
"fallbacks": "auto"
}
自动备选策略:
- 优先尝试同系列的其他世代模型(如同厂商的上一代)
- 然后依次尝试其他系列的最新模型(跨厂商兜底)
风险提示
自动备选会将请求切换到不同厂商的模型,各模型的参数支持和能力存在差异,请自行评估是否适用:
- 多模态能力不互通:主模型支持视频/图像理解时,fallback 模型可能不具备该能力
- 特殊参数可能不兼容:thinking/reasoning 模式、tool calling 等行为在不同厂商间存在差异
- 不适用于强依赖特定模型能力的场景:如 Agent 依赖某模型的特有功能,请手动指定 fallback
Auto 模式适合通用文本生成场景,对模型能力没有强绑定要求的情况。
组合使用
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="deepseek-v4-flash",
messages=[{"role": "user", "content": "Hello!"}],
turing_options={
"timeout": 10,
"max_retries": 1,
"fallbacks": [
"doubao-seed-2.1-turbo",
"turing/gpt-5.4-mini"
]
}
)
常见问题
为什么会遇到超时错误?
如果您遇到超时错误,可能是以下原因:
- 大文件传输:上传大量图片或文件需要更多时间
- 长思考问题:使用思考模式的模型(如 o 系列、Deepseek R1)在复杂问题上可能需要较长的推理时间
- 模型静默期:模型在处理请求时可能有一段时间没有返回数据
建议排查步骤:
-
使用流式模式验证:切换到
stream=True模式,检查是否有首个 chunk 返回completion = client.chat.completions.create(model="turing/deepseek-r1",messages=[{"role": "user", "content": "复杂的数学问题..."}],stream=True # 开启流式模式)for chunk in completion:print(chunk) # 观察首个 chunk 返回时间 -
如果有首 chunk 返回但后续超时:说明模型正在工作,只是处理时间较长,可以增加超时时间
completion = client.chat.completions.create(model="turing/deepseek-r1",messages=[{"role": "user", "content": "复杂的数学问题..."}],turing_options={"timeout": 180 # 增加到 3 分钟}) -
如果一直没有返回:可能是网络问题或请求参数错误,请检查网络连接和请求格式
客户端超时
OpenAI SDK 的超时是客户端超时,turing_options.timeout 是服务端超时。建议客户端超时 > 服务端超时:
client = OpenAI(
timeout=60.0 # 客户端超时
)
completion = client.chat.completions.create(
model="turing/gpt-4.1",
messages=[{"role": "user", "content": "Hello!"}],
turing_options={
"timeout": 30 # 服务端超时
}
)