- Notifications
You must be signed in to change notification settings - Fork1k
Open
Description
In case the API returns a 429/Rate limit exceeded, pydantic-ai throws a date-time parsing exception instead of surfacing the appropriate error message from the API around RLE(rate-limit-exceeded).
This can easily be replicated by using openrouter with one of the free gemini models.
frompydantic_aiimportAgentfrompydantic_ai.models.openaiimportOpenAIModelmodel=OpenAIModel("google/gemini-2.0-flash-exp:free",base_url="https://openrouter.ai/api/v1",api_key="key",)agent=Agent(model=model,system_prompt='Be concise, reply with one sentence.', )result=agent.run_sync('Who are you?')print(result.data)
The above returns -
Traceback (mostrecentcalllast):File"/Users/sam/dev/openai/openai_demo.py",line32,in<module>result=agent.run_sync('Who are you?')^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/Users/sam/dev/openai/.venv/lib/python3.12/site-packages/pydantic_ai/agent.py",line327,inrun_syncreturnasyncio.get_event_loop().run_until_complete(^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/opt/homebrew/Cellar/python@3.12/3.12.6/Frameworks/Python.framework/Versions/3.12/lib/python3.12/asyncio/base_events.py",line687,inrun_until_completereturnfuture.result()^^^^^^^^^^^^^^^File"/Users/sam/dev/openai/.venv/lib/python3.12/site-packages/pydantic_ai/agent.py",line255,inrunmodel_response,request_usage=awaitagent_model.request(messages,model_settings)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/Users/sam/dev/openai/.venv/lib/python3.12/site-packages/pydantic_ai/models/openai.py",line152,inrequestreturnself._process_response(response),_map_usage(response)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File"/Users/sam/dev/openai/.venv/lib/python3.12/site-packages/pydantic_ai/models/openai.py",line207,in_process_responsetimestamp=datetime.fromtimestamp(response.created,tz=timezone.utc)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^TypeError:'NoneType'objectcannotbeinterpretedasaninteger
This happens because the error response is not correctly handled in _process_response -
ChatCompletion(id=None,choices=None,created=None,model=None,object=None,service_tier=None,system_fingerprint=None,usage=None,error={'message':'Provider returned error','code':429,'metadata': {'raw':'{\n "error": {\n "code": 429,\n "message": "Quota exceeded for aiplatform.googleapis.com/generate_content_requests_per_minute_per_project_per_base_model with base model: gemini-experimental. Please submit a quota increase request. https://cloud.google.com/vertex-ai/docs/generative-ai/quotas-genai.",\n "status": "RESOURCE_EXHAUSTED"\n }\n}\n','provider_name':'Google'}},user_id='user_...')
We should check for the presence of the error object and handle the other fields appropriately.
Note: I have noticed this with both google's OpenAI compat API and openrouter's gemini API.
This is what an example output response may look like