High severity beginner · Fix: 2-5 min

ValueError

pydantic_ai.agent.ValueError: Agent model not configured

What this error means
The Pydantic AI Agent raises this error when its required model configuration is missing or not properly set before usage.

Stack trace

traceback
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
QUICK FIX
Assign a valid model instance to the agent before calling run(), e.g., agent = Agent(model=llm).

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

1

The agent instance was created without assigning a model parameter.

✓ Fix

Instantiate the agent with a valid model object, e.g., pass a configured LLM instance during agent creation.

2

The model configuration was cleared or overwritten to None after agent initialization.

✓ Fix

Ensure the model attribute remains assigned and is not reset to None before calling agent methods.

3

Incorrect import or usage of the agent class leading to default constructor without model setup.

✓ Fix

Verify you are importing the correct agent class and passing the model argument as required by the library version.

Code: broken vs fixed

Broken - triggers the error
python
from pydantic_ai import Agent

agent = Agent()  # Missing model configuration
result = agent.run('Hello')  # Raises ValueError: Agent model not configured
Fixed - works correctly
python
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
Added a valid LLM model instance to the Agent constructor so the agent has a configured model to call, preventing the ValueError.

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.

Python 3.9+ · pydantic-ai >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.