ValueError
pydantic_ai.agent.ValueError: Agent model not configured
Stack trace
Traceback (most recent call last):
File "main.py", line 42, in <module>
agent.run(input_data)
File "/usr/local/lib/python3.9/site-packages/pydantic_ai/agent.py", line 88, in run
raise ValueError('Agent model not configured')
ValueError: Agent model not configured Why it happens
The Pydantic AI Agent requires a model to be explicitly configured before running. This error occurs if the agent's model attribute is None or unset, meaning the agent has no LLM or model instance to call. It usually happens when the developer forgets to assign a model or passes an invalid configuration.
Detection
Check if the agent's model attribute is None or missing before calling run(), or catch ValueError exceptions and log the missing configuration details.
Causes & fixes
The agent instance was created without assigning a model parameter.
Instantiate the agent with a valid model object, e.g., pass a configured LLM instance during agent creation.
The model configuration was cleared or overwritten to None after agent initialization.
Ensure the model attribute remains assigned and is not reset to None before calling agent methods.
Incorrect import or usage of the agent class leading to default constructor without model setup.
Verify you are importing the correct agent class and passing the model argument as required by the library version.
Code: broken vs fixed
from pydantic_ai import Agent
agent = Agent() # Missing model configuration
result = agent.run('Hello') # Raises ValueError: Agent model not configured import os
from pydantic_ai import Agent
from some_llm_library import LLM
os.environ['API_KEY'] = os.environ.get('OPENAI_API_KEY', 'your_api_key_here')
llm = LLM(api_key=os.environ['API_KEY'])
agent = Agent(model=llm) # Model properly configured
result = agent.run('Hello')
print(result) # Works without error Workaround
Wrap the agent.run() call in try/except ValueError, and if caught, initialize and assign a default model instance before retrying.
Prevention
Always configure and validate the agent's model attribute immediately after instantiation and before any method calls to ensure it is never None.