mirror of
https://github.com/ollama/ollama-python.git
synced 2026-07-21 00:55:21 -04:00
Issue with Ollama and Pydantic #109
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @jonabeysens on GitHub (Jul 4, 2024).
Hi guys,
Are you also facing issues when using Ollama with Pydantic? It seems that the response from the LLM is often not put in the output format as requested by Pydantic. Hence, the subsequent code in our program throws an error.
In our tests, we used Codestral-22B and the following function to call the LLM inference. We also tested the same with the API from Mistral, and this works. So it seems there is a problem in the interface between Ollama and Pydantic.
Do you have good experience with Ollama and Pydantic? Do you know how we can solve this?
Many thanks for your help!
Pseudo code (not functional, just to show which functions we call)
We are using Pydantic through the call with_structured_output:
llm.with_structured_output(MyPydanticOutputClass)So, sometimes there are some fields of the Pydantic object that are not parsed, so the output of the LLM is incomplete, for example:
And sometimes producing a more general error:
@lennartpollvogt commented on GitHub (Jul 22, 2024):
Hey @jonabeysens
do you know from Langchain how it instructs the LLM with the Pydantic model you provide for the response format and how it processes it?
I do have some experience with Pydantic and Ollama within my own library (ollama-instructor), where I instructed the LLM to adhere to the JSON schema of the Pydantic model. And yes, sometimes the models are not able to provide the properties of Pydantic model correctly (e.g. Mistral often has problems to response as list/array of dicts/objects). For this reason I added retries for malformed responses and the possibilities to receive partial models. But I do not know how Langchain handles the response.
You could try to instruct the LLM with the JSON schema while using the ollama client instead of Langchain and see what is coming back from Codestral. And you could try it with my library if this is working for your use case.
To be honest, without knowing the exact response from the LLM it is not that easy. I hope my comment helps anyway. 😊