Structured Output - Literal Validation Error #228

Closed
opened 2026-02-15 16:28:53 -05:00 by yindo · 4 comments
Owner

Originally created by @XanderBrooks on GitHub (Feb 26, 2025).

I am able to run tools, but structured outputs are giving me an issue.
It seems like ollama version (0.5.12) that I have installed only accepts a literal for the format arg. Take a look:


Docstring output:

chat?

...
format: Optional[Literal['', 'json']] = None,
...


So I am basically able to get NON-specified json out (if i set it to 'json', it will output json, but NOT within a pre-defined structure.) This means that it likely fails during validation because there isn't a specified structure going into the chat instance.

If I run the basic code included on the blog post (and most tutorials, the 'tell me more about Canada' example), it fails. I am also using llama3.2:8b (same as Parth Sareen's recent video here [https://www.youtube.com/watch?v=UoZkkz1oXgQ]). I tried both pydantic as in the example, and multiple attempts to see if loading the json in using json.loads or just a direct py object. Same error every time, which is why I checked the docstring.

I understand I could reinforce the structure within the prompt, but I was curious why this was not natively working.


Curious if this is a 0.5.12 bug or a windows/mac vs linux limitation.


Example code and error:

from ollama import chat
from pydantic import BaseModel

class Country(BaseModel):
  name: str
  capital: str
  languages: list[str]

response = chat(
  messages=[
    {
      'role': 'user',
      'content': 'Tell me about Canada.',
    }
  ],
  model='llama3.1:8b',
  format=Country.model_json_schema(),
)

country = Country.model_validate_json(response.message.content)
print(country)

Output

ValidationError: 1 validation error for ChatRequest
format
  Input should be '' or 'json' [type=literal_error, input_value={'properties': {'name': {...ntry', 'type': 'object'}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.10/v/literal_error

Thanks for any help.

Originally created by @XanderBrooks on GitHub (Feb 26, 2025). I am able to run tools, but structured outputs are giving me an issue. It seems like ollama version (0.5.12) that I have installed only accepts a literal for the format arg. Take a look: --------------------------------------------------------------------------- ### Docstring output: chat? > ... > format: Optional[Literal['', 'json']] = None, > ... --------------------------------------------------------------------------- So I am basically able to get NON-specified json out (if i set it to 'json', it will output json, but NOT within a pre-defined structure.) This means that it likely fails during validation because there isn't a specified structure going into the chat instance. If I run the basic code included on the blog post (and most tutorials, the 'tell me more about Canada' example), it fails. I am also using llama3.2:8b (same as Parth Sareen's recent video here [https://www.youtube.com/watch?v=UoZkkz1oXgQ]). I tried both pydantic as in the example, and multiple attempts to see if loading the json in using json.loads or just a direct py object. Same error every time, which is why I checked the docstring. I understand I could reinforce the structure within the prompt, but I was curious why this was not natively working. --------------------------------------------------------------------------- Curious if this is a 0.5.12 bug or a windows/mac vs linux limitation. --------------------------------------------------------------------------- ### Example code and error: ``` from ollama import chat from pydantic import BaseModel class Country(BaseModel): name: str capital: str languages: list[str] response = chat( messages=[ { 'role': 'user', 'content': 'Tell me about Canada.', } ], model='llama3.1:8b', format=Country.model_json_schema(), ) country = Country.model_validate_json(response.message.content) print(country) ``` ### Output ``` ValidationError: 1 validation error for ChatRequest format Input should be '' or 'json' [type=literal_error, input_value={'properties': {'name': {...ntry', 'type': 'object'}, input_type=dict] For further information visit https://errors.pydantic.dev/2.10/v/literal_error ``` --------------------------------------------------------------------------- Thanks for any help.
yindo closed this issue 2026-02-15 16:28:53 -05:00
Author
Owner

@ParthSareen commented on GitHub (Feb 26, 2025):

What's your ollama python version? pip show ollama

@ParthSareen commented on GitHub (Feb 26, 2025): What's your ollama python version? `pip show ollama`
Author
Owner

@XanderBrooks commented on GitHub (Feb 26, 2025):

Thanks for the quick reply!


What's your ollama python version? pip show ollama

Version: 0.4.2


Name: ollama
Version: 0.4.2
Summary: The official Python client for Ollama.
Home-page: https://ollama.com
Author: Ollama
Author-email: hello@ollama.com
License: MIT
Location: C:\Users...\AppData\Local\Programs\Python\Python312\Lib\site-packages
Requires: httpx, pydantic
Required-by: langchain-ollama

@XanderBrooks commented on GitHub (Feb 26, 2025): Thanks for the quick reply! - - - - - - - - > What's your ollama python version? `pip show ollama` Version: 0.4.2 - - - - - - - - Name: ollama Version: 0.4.2 Summary: The official Python client for Ollama. Home-page: https://ollama.com Author: Ollama Author-email: hello@ollama.com License: MIT Location: C:\Users\...\AppData\Local\Programs\Python\Python312\Lib\site-packages Requires: httpx, pydantic Required-by: langchain-ollama
Author
Owner

@ParthSareen commented on GitHub (Feb 26, 2025):

Yup that's the issue. Structured outputs support came in 0.4.3+

pip install ollama --upgrade

Hope it helps!

@ParthSareen commented on GitHub (Feb 26, 2025): Yup that's the issue. Structured outputs support came in 0.4.3+ `pip install ollama --upgrade` Hope it helps!
Author
Owner

@XanderBrooks commented on GitHub (Feb 26, 2025):

Yup that's the issue. Structured outputs support came in 0.4.3+

pip install ollama --upgrade

Hope it helps!

Cured! Thank you! Sorry I thought updating the other day to 0.5.12 was enough. Makes complete sense why that is NOT the case. I'll be sure to upgrade both sides consistently.

@XanderBrooks commented on GitHub (Feb 26, 2025): > Yup that's the issue. Structured outputs support came in 0.4.3+ > > `pip install ollama --upgrade` > > Hope it helps! Cured! Thank you! Sorry I thought updating the other day to 0.5.12 was enough. Makes complete sense why that is NOT the case. I'll be sure to upgrade both sides consistently.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#228