You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
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
The text was updated successfully, but these errors were encountered:
sambhav
changed the title
[BUG] Error responses are not handled correctly.
[BUG] Error responses are not handled correctly
Dec 22, 2024
sambhav
changed the title
[BUG] Error responses are not handled correctly
[BUG] Error responses are not handled correctly for google openapi/openrouter
Dec 22, 2024
This seems to happen due to inappropriate handling of type-casting by the openai client. OpenAI client always casts the response to a ChatCompletion response type, which they allow additional fields to be set on.
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.
The above returns -
This happens because the error response is not correctly handled in _process_response -
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
The text was updated successfully, but these errors were encountered: