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
When using OpenAIModel with OpenRouter and the model google/gemini-2.0-flash-exp:free, the agent fails with:
TypeError: 'NoneType' object is not subscriptable
This happens because the response returned by OpenRouter for Gemini does not include the choices or created keys expected by _process_response().
Steps to Reproduce
frompydantic_aiimportAgentfrompydantic_ai.models.openaiimportOpenAIModelfrompydantic_ai.providers.openaiimportOpenAIProvidermodel=OpenAIModel(
'google/gemini-2.0-flash-exp:free',
provider=OpenAIProvider(
base_url='https://openrouter.ai/api/v1',
api_key='sk-or-v1-...',
),
)
agent=Agent(model=model, system_prompt="Be helpful.")
result=agent.run_sync("Tell me a joke.")
Observed Error
response.created is None, causing datetime.fromtimestamp() to fail
response.choices is None, causing choices[0] to throw
Suggestion
Add a fallback timestamp = datetime.now() when created is missing
Handle cases where choices is missing — or validate the response format before attempting to parse
Possibly support custom response parsers for models that aren't 100% OpenAI-compatible
Why it matters
OpenRouter is a growing ecosystem for multimodel access, and many of the available models (Gemini, Cohere, etc.) do not fully follow the OpenAI ChatCompletion schema. Supporting them would improve compatibility.
Let me know if you'd like me to submit a PR or share sample responses.
I was able to reproduce this. It only happens sometimes--since OpenRouter has two providers for this model, I'm guessing it happens for one of the providers but not the other.
wdhorton
added a commit
to wdhorton/pydantic-ai
that referenced
this issue
May 19, 2025
The OpenAI spec defines this as required, but other OpenAI-compatible providers (like OpenRouter) may not populate it
on all responses. This adds code to handle the case where the field is None.
Fixespydantic#1746
Probably yes. If I set up OpenAI or DeepSeek it works. The problem practically only occurs with Google models. In any case, I only noticed it with them.
I've created the OpenRouterProvider to make it easier to work with it, also, you can now use Agent('openrouter:<model_name>') to use OpenRouter - Assuming you have the OPENROUTER_API_KEY set.
Initial Checks
Description
Summary
When using
OpenAIModel
with OpenRouter and the modelgoogle/gemini-2.0-flash-exp:free
, the agent fails with:TypeError: 'NoneType' object is not subscriptable
This happens because the response returned by OpenRouter for Gemini does not include the
choices
orcreated
keys expected by_process_response()
.Steps to Reproduce
Observed Error
Suggestion
Why it matters
OpenRouter is a growing ecosystem for multimodel access, and many of the available models (Gemini, Cohere, etc.) do not fully follow the OpenAI ChatCompletion schema. Supporting them would improve compatibility.
Let me know if you'd like me to submit a PR or share sample responses.
Thanks for the awesome tool!
Example Code
Python, Pydantic AI & LLM client version
The text was updated successfully, but these errors were encountered: