-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Labels
Description
Initial Checks
- I confirm that I'm using the latest version of Pydantic AI
- I confirm that I searched for my issue in https://github.com/pydantic/pydantic-ai/issues before opening this issue
Description
I am trying to run Qwen3 via Ollama locally and getting api call errors (400) as presented below:
Exception has occurred: ModelHTTPError
status_code: 400, model_name: qwen3:32b, body: {'message': 'invalid message content type: <nil>', 'type': 'invalid_request_error', 'param': None, 'code': None}
httpx.HTTPStatusError: Client error '400 Bad Request' for url 'http://localhost:11434/v1/chat/completions'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400
During handling of the above exception, another exception occurred:
openai.BadRequestError: Error code: 400 - {'error': {'message': 'invalid message content type: <nil>', 'type': 'invalid_request_error', 'param': None, 'code': None}}
The above exception was the direct cause of the following exception:
File "/data/home/dvirla/llm_search_eval/src/services/base_agent.py", line 56, in run
response = self.agent.run_sync(user_input)
File "/data/home/dvirla/llm_search_eval/src/services/base_agent.py", line 65, in test_agent
result = agent.run(user_query)
File "/data/home/dvirla/llm_search_eval/main.py", line 5, in <module>
res = test_agent()
pydantic_ai.exceptions.ModelHTTPError: status_code: 400, model_name: qwen3:32b, body: {'message': 'invalid message content type: <nil>', 'type': 'invalid_request_error', 'param': None, 'code': None}
The code to reproduce is attached.
I have tried multiple ways to initialize the agent as detailed in the documentation, but still getting this error.
When I try to run the model from the terminal using Ollama, I see it uses the endpoint /api/generate and not v1/chat/completion.
Example Code
class BaseAgent:
def __init__(self, prompt_path: str = None, provider_name: str = "Google", model_name: str = "gemini-flash-latest"):
if provider_name == "Google":
settings = GoogleModelSettings(google_thinking_config={'include_thoughts': True})
provider = GoogleProvider(api_key=os.getenv("GOOGLE_API_KEY"))
self.model = GoogleModel(model_name, provider=provider)
elif provider_name == "OpenAI":
provider = OpenAIProvider(api_key=os.getenv("OPENAI_API_KEY"))
self.model = OpenAIChatModel(model_name=model_name, provider=provider)
elif provider_name == "ollama":
provider = OllamaProvider(base_url='http://localhost:11434/v1/')
self.model = OpenAIChatModel(
model_name=model_name,
provider=provider
)
else:
raise ValueError(f"Invalid provider: {provider_name}")
if prompt_path:
self.system_prompt = self._load_prompt(prompt_path)
else:
self.system_prompt = [None]
self.agent = Agent(
model=self.model,
system_prompt=self.system_prompt,
output_type=str
)
@staticmethod
def _load_prompt(path: str) -> str:
with open(path, 'r') as f:
return f.read()
def run(self, user_input: str):
response = self.agent.run_sync(user_input)
return response
def test_agent():
agent = BaseAgent(
provider_name="ollama",
model_name="qwen3:32b"
)
user_query = "What is the capital of France?"
result = agent.run(user_query)
print(user_query)
print(result)Python, Pydantic AI & LLM client version
ollama version is 0.12.6
pydantic_ai 1.14.1
Python 3.11.4