Error code: 429 - {'error': {'message': 'GeminiException - {"error":{"message":"...

2026年07月03日 11:59 35.30 秒 success

错误信息

openai.RateLimitError: Error code: 429 - {'error': {'message': 'GeminiException - {"error":{"message":"Resource has been exhausted (e.g. check quota). (request id: 20260703034920122914212UAVDHRA0)","type":"upstream_error","param":"","code":429}}Error happened to model=AWS-Gemini-3.1-Pro-Preview. Received Model Group=AWS-Gemini-3.1-Pro-Preview\nAvailable Model Group Fallbacks=None\nError doing the fallback: GeminiException - {"error":{"message":"Resource has been exhausted (e.g. check quota). (request id: 20260703034920122914212UAVDHRA0)","type":"upstream_error","param":"","code":429}}Error happened to model=AWS-Gemini-3.1-Pro-Preview', 'type': 'throttling_error', 'param': None, 'code': '429'}}

AI 解决方案

🔍 报错分析

1. 🌐 错误翻译


原文:openai.RateLimitError: Error code: 429 - {'error': {'message': 'GeminiException - {"error":{"message":"Resource has been exhausted (e.g. check quota). (request id: 20260703034920122914212UAVDHRA0)","type":"upstream_error","param":"","code":429}}Error happened to model=AWS-Gemini-3.1-Pro-Preview. Received Model Group=AWS-Gemini-3.1-Pro-Preview\nAvailable Model Group Fallbacks=None\nError doing the fallback: GeminiException - {"error":{"message":"Resource has been exhausted (e.g. check quota). (request id: 20260703034920122914212UAVDHRA0)","type":"upstream_error","param":"","code":429}}Error happened to model=AWS-Gemini-3.1-Pro-Preview', 'type': 'throttling_error', 'param': None, 'code': '429'}}
译文:openai.RateLimitError: 错误代码:429 - {'error': {'message': 'Gemini异常 - {"error":{"message":"资源已耗尽(例如检查配额)。(请求ID:20260703034920122914212UAVDHRA0)","type":"上游错误","param":"","code":429}}模型AWS-Gemini-3.1-Pro-Preview发生错误。接收到的模型组=AWS-Gemini-3.1-Pro-Preview\n可用的模型组回退方案=无\n执行回退时出错:Gemini异常 - {"error":{"message":"资源已耗尽(例如检查配额)。(请求ID:20260703034920122914212UAVDHRA0)","type":"上游错误","param":"","code":429}}模型AWS-Gemini-3.1-Pro-Preview发生错误', 'type': '限流错误', 'param': None, 'code': '429'}}

2. 🎯 原因分析


  • 根本原因:API请求超出了服务商设定的速率限制或配额限制,导致服务端拒绝请求

  • 具体位置:在调用AWS-Gemini-3.1-Pro-Preview模型时,触发了429限流错误,且无可用回退方案

  • 核心问题:当前请求频率过高或账号/项目的配额已耗尽,触发上游服务(Gemini)的资源保护机制
  • 3. 💡 解决方案


    方案1:添加请求重试与指数退避
    import time
    import openai
    from openai.error import RateLimitError
    
    def call_model_with_retry(max_retries=5, base_delay=1):
        """带指数退避的请求重试函数"""
        for attempt in range(max_retries):
            try:
                # 替换为实际的模型调用逻辑
                response = openai.ChatCompletion.create(
                    model="AWS-Gemini-3.1-Pro-Preview",
                    # ... 其他参数
                )
                return response
            except RateLimitError as e:
                if attempt == max_retries - 1:
                    raise  # 最后一次重试失败,抛出原错误
                delay = base_delay * (2 ** attempt)  # 指数退避
                print(f"触发限流,{delay}秒后第{attempt+1}次重试...")
                time.sleep(delay)
    

    方案2:检查并调整请求频率

  • 降低并发请求数量,避免同时发送过多请求

  • 减少单时间窗口内的请求次数(如将每秒请求数控制在服务商限制内)
  • 方案3:申请配额提升

  • 登录API服务商后台,检查当前配额使用情况

  • 根据需求提交配额提升申请,或调整使用计划
  • 4. 🛡️ 预防措施


  • 动态限流控制:在代码中加入速率限制逻辑,实时监控请求频率,确保不超过服务商阈值;可使用令牌桶算法或漏桶算法实现自适应限流

  • 资源监控与预警:集成API调用监控工具(如Prometheus+Grafana),设置限流阈值预警,当请求接近配额时提前通知,避免突发超限

  • 备用模型策略:提前配置多模型回退机制,当主模型触发限流时,自动切换