-
Notifications
You must be signed in to change notification settings - Fork 476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validation with nested Pydantic models (ollama, llama3.1) #607
Comments
Your example works fine with Gemini 1.5 Flash for me but fails with Ollama 3.2. Might be specific to ollama. |
This is also my diagnosis. However, I am surprised that it works with Ollama's library but not with Pydantic AI. Nested structures are quite practical, and I must admit that this is what holds me back from delving deeper into Pydantic AI. Should an issue be opened in Ollama, or is this something that can be resolved here? |
So I think I've figured it out - it looks like it has to do with how the Ollama API formats JSON responses. Here's my testing example: model = cast(KnownModelName, os.getenv('PYDANTIC_AI_MODEL', 'ollama:llama3.2'))
agent = Agent(model, result_type=MyModel, system_prompt="Return what city, state, and country are provided to you.")
with capture_run_messages() as messages:
result = agent.run_sync(
'Chicago, Illinois, USA'
)
print(result.data)
print(messages) Place a breakpoint here and look at the value of c.function.arguments in the debugger when running the example above. Check out the difference between what the OpenAI value is vs. what the Ollama value is. I see the following: Running gpt-4o, I see:
Running Ollama 3.2, I see:
The inner quotation marks (around the inner JSON object) and the double-escaped quotation marks appear to only come up for nested models. Single-layer models look the same between the two models. Add a third layer and it gets even messier with Ollama but OpenAI is still fine. Under the hood, Pydantic-AI uses the OpenAI Python library to interact with the Ollama model, so I suspect this is related to the OpenAI library not working perfectly with Ollama. I'm fiddling with some fixes - I think it would be reasonable to make an OllamaAgentModel as a subclass of OpenAIAgentModel and override the _process_response method and change how the JSON responses are parsed. It's also possible the Ollama API provides a solution for this, which would maybe justify writing an AgentModel and Model specifically for Ollama rather than reusing the OpenAI one. |
Opened a PR (#621) with a fix. Would love some feedback. |
PLEASE try to look for similar issues before creating new issues!. This is a duplicate of #242 as far as I can tell. |
I think it's a partial duplicate. #242 seems more focused on the fact that Ollama doesn't always decide to use the tool when provided a response model but this issue is running into issues with Ollama providing a response that adheres to the provided response model but the OpenAI model isn't set up to decode properly. |
@samuelcolvin Apologies, I guess with the number of open issues, I focused more on the nested models part since everything else was working fine in my case. I didn’t really think this might tie into deeper issues with Ollama. I’ll take a closer look at #242 and see how it connects. |
Seems similar to my issue 639 incorrectly closed. |
When using the
pydantic_ai
library with a nestedBaseModel
, anUnexpectedModelBehavior
error occurs, despite the underlying model (e.g.,ollama:llama3.1
) being capable of handling the requested structure and providing valid output.The example here https://ai.pydantic.dev/examples/pydantic-model/#running-the-example is fully functional with
ollama:llama3.1
, but this slight modification to include nested models fails to work:I get this error:
It seems that the model fails to generate the expected nested structure.
Validation of the Model's Capability:
To confirm that the underlying model (
ollama:llama3.1
) supports this functionality, the following custom implementation was tested:This implementation returned the expected result without any errors:
Environment:
pydantic_ai[examples]>=0.0.17
Could I be misunderstanding or misusing the library?
The text was updated successfully, but these errors were encountered: