on attempt 1/5: APITimeoutError - Request timed out… Retrying in 15.80s… 2026-04...

2026年04月07日 07:55 processing

错误信息

2026-04-06 23:43:46 - openai._base_client - INFO - Retrying request to /chat/completions in 0.954236 seconds 2026-04-06 23:43:46 - openai._base_client - INFO - Retrying request to /chat/completions in 0.847687 seconds 2026-04-06 23:43:46 - openai._base_client - INFO - Retrying request to /chat/completions in 0.797546 seconds 2026-04-06 23:43:46 - openai._base_client - INFO - Retrying request to /chat/completions in 0.852798 seconds 2026-04-06 23:43:46 - openai._base_client - INFO - Retrying request to /chat/completions in 0.807074 seconds 2026-04-06 23:43:46 - openai._base_client - INFO - Retrying request to /chat/completions in 0.848294 seconds 2026-04-06 23:43:46 - openai._base_client - INFO - Retrying request to /chat/completions in 0.841428 seconds 2026-04-06 23:43:46 - openai._base_client - INFO - Retrying request to /chat/completions in 0.957075 seconds 2026-04-06 23:43:47 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 15.80s… 2026-04-06 23:43:47 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 17.71s… 2026-04-06 23:43:47 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 18.30s… 2026-04-06 23:43:47 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 13.92s… 2026-04-06 23:43:47 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 12.87s… 2026-04-06 23:43:47 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 10.61s… 2026-04-06 23:43:47 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 10.19s… 2026-04-06 23:43:48 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 17.69s… 2026-04-06 23:43:48 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 12.66s… 2026-04-06 23:43:48 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 10.93s… 2026-04-07 00:43:46 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 18.53s… 2026-04-07 00:43:46 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 15.97s… 2026-04-07 00:43:46 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 17.16s… 2026-04-07 00:43:46 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 18.64s… 2026-04-07 00:43:46 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 18.50s… 2026-04-07 00:43:46 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 17.85s… 2026-04-07 00:43:46 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 17.99s… 2026-04-07 00:43:46 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 10.30s… 2026-04-07 00:43:46 - policy_extraction.concurrency - WARNING - Transient error on attempt 1/5: APITimeoutError - Request timed out… Retrying in 10.41s…

AI 解决方案

报错分析报告

1.错误翻译


原文:APITimeoutError - Request timed out…
译文:API超时错误 - 请求超时…

2. 原因分析


  • 根本原因:调用OpenAI /chat/completions接口时,请求超过默认超时时间未获响应。

  • 具体位置policy_extraction.concurrency模块的并发请求逻辑中,多次尝试均触发超时(attempt 1/5)。
  • 3. 解决方案


    # 修复后的代码(Python + OpenAI SDK)
    from openai import OpenAI
    import httpx
    from tenacity import retry, stop_after_attempt, wait_exponential
    
    # 1. 配置客户端(延长超时时间)
    client = OpenAI(
        api_key="your-api-key",
        timeout=httpx.Timeout(60.0, read=60.0)  # 总超时+读取超时设为60秒
    )
    
    # 2. 带指数退避的重试装饰器
    @retry(
        stop=stop_after_attempt(5),       # 最多重试5次
        wait=wait_exponential(multiplier=1, min=4, max=10)  # 指数退避(4-10秒)
    )
    def safe_chat_request():
        return client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": "你的请求内容"}],
            timeout=60  # 单次请求超时60秒
        )
    
    # 3. 执行请求
    try:
        response = safe_chat_request()
    except Exception as e:
        print(f"最终失败: {e}")  # 捕获剩余错误
    

    4. 预防措施


  • 最佳实践

  • 1. 为API请求设置动态超时(如根据接口复杂度设30-120秒),避免固定短超时;
    2. 用指数退避重试(而非固定间隔),减少服务端压力。
  • 推荐工具

  • - tenacity(Python重试库)、httpx(灵活超时控制);
    - Prometheus+Grafana(监控请求延迟与超时率)。

    ---
    以上方案直接解决超时重试失效问题,需替换your-api-key与请求内容为实际值。