] [OpenAI API] Error calling API (Status: 524, Code: ERR_BAD_RESPONSE): Request ...
🚨 错误信息
[ERROR] [OpenAI API] Error calling API (Status: 524, Code: ERR_BAD_RESPONSE): Request failed with status code 524
🤖 AI解决方案
报错分析报告
1.错误翻译
原文:Request failed with status code 524
译文:请求失败,状态码为524
2.原因分析
3.解决方案
python
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry1. 配置重试策略(应对临时超时)
session = requests.Session()
retry = Retry(
total=3, # 总重试3次
backoff_factor=1, # 退避间隔:1s→2s→4s
status_forcelist=[524, 429, 500, 502, 503, 504]
)
session.mount("https://", HTTPAdapter(max_retries=retry))2. 带超时的API调用
try:
resp = session.post(
"https://api.openai.com/v1/chat/completions",
headers={"Authorization": "Bearer YOUR_API_KEY"},
json={"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Hello"}]},
timeout=(10, 30) # 连接超时10s,读取超时30s
)
resp.raise_for_status() # 抛出HTTP错误
print(resp.json())
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
4. 预防措施
curl -v https://api.openai.com测试网络连通性,或用Postman模拟请求验证超时配置。