Structured output, json schema error #182

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

Originally created by @QueryType on GitHub (Dec 7, 2024).

Hi, Trying to run the sample code from the blog, (or even from the examples), and getting the following error:
Any help will be greatly appreciated. I am running on MacOS Apple Silicon.

python structured_outputs.py Traceback (most recent call last): File "/Volumes/d/code/aiml/ollama_code/structured_outputs.py", line 11, in <module> response = chat( ^^^^^ File "/opt/homebrew/Caskroom/miniconda/base/envs/openai/lib/python3.11/site-packages/ollama/_client.py", line 332, in chat return self._request( ^^^^^^^^^^^^^^ File "/opt/homebrew/Caskroom/miniconda/base/envs/openai/lib/python3.11/site-packages/ollama/_client.py", line 177, in _request return cls(**self._request_raw(*args, **kwargs).json()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Caskroom/miniconda/base/envs/openai/lib/python3.11/site-packages/ollama/_client.py", line 122, in _request_raw raise ResponseError(e.response.text, e.response.status_code) from None ollama._types.ResponseError: json: cannot unmarshal object into Go struct field ChatRequest.format of type string (openai) nireve@Mini ollama_code % pip freeze | grep "pydantic" pydantic==2.10.3 pydantic_core==2.27.1 (openai) nireve@Mini ollama_code % pip freeze | grep ollama ollama==0.4.3
The code is:
`from ollama import chat
from pydantic import BaseModel

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

#print(Country.model_json_schema())

response = chat(
messages=[
{
'role': 'user',
'content': 'Tell me about Canada.',
}
],
model='llama3.2:3b-instruct-q6_K',
format=Country.model_json_schema(),
options={'temperature': 0},
)

print(response.message.content)

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

Originally created by @QueryType on GitHub (Dec 7, 2024). Hi, Trying to run the sample code from the blog, (or even from the examples), and getting the following error: Any help will be greatly appreciated. I am running on MacOS Apple Silicon. `python structured_outputs.py Traceback (most recent call last): File "/Volumes/d/code/aiml/ollama_code/structured_outputs.py", line 11, in <module> response = chat( ^^^^^ File "/opt/homebrew/Caskroom/miniconda/base/envs/openai/lib/python3.11/site-packages/ollama/_client.py", line 332, in chat return self._request( ^^^^^^^^^^^^^^ File "/opt/homebrew/Caskroom/miniconda/base/envs/openai/lib/python3.11/site-packages/ollama/_client.py", line 177, in _request return cls(**self._request_raw(*args, **kwargs).json()) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/homebrew/Caskroom/miniconda/base/envs/openai/lib/python3.11/site-packages/ollama/_client.py", line 122, in _request_raw raise ResponseError(e.response.text, e.response.status_code) from None ollama._types.ResponseError: json: cannot unmarshal object into Go struct field ChatRequest.format of type string (openai) nireve@Mini ollama_code % pip freeze | grep "pydantic" pydantic==2.10.3 pydantic_core==2.27.1 (openai) nireve@Mini ollama_code % pip freeze | grep ollama ollama==0.4.3 ` The code is: `from ollama import chat from pydantic import BaseModel class Country(BaseModel): name: str capital: str languages: list[str] #print(Country.model_json_schema()) response = chat( messages=[ { 'role': 'user', 'content': 'Tell me about Canada.', } ], model='llama3.2:3b-instruct-q6_K', format=Country.model_json_schema(), options={'temperature': 0}, ) print(response.message.content) country = Country.model_validate_json(response.message.content) print(country)`
yindo closed this issue 2026-02-15 16:28:32 -05:00
Author
Owner

@c4wrd commented on GitHub (Dec 7, 2024):

I ran into the same issue. The issue was that I had only updated my ollama Python package, but the ollama server (the main application process) was not updated. By installing the most recent version of ollama (binary), the issue was resolved.

@c4wrd commented on GitHub (Dec 7, 2024): I ran into the same issue. The issue was that I had only updated my `ollama` Python package, but the `ollama` server (the main application process) was not updated. By installing the most recent version of `ollama` (binary), the issue was resolved.
Author
Owner

@QueryType commented on GitHub (Dec 7, 2024):

Thanks , you are right. It icon at the top was showing that I need to restart the ollama server, not sure why I missed that. Closing the issue.

@QueryType commented on GitHub (Dec 7, 2024): Thanks , you are right. It icon at the top was showing that I need to restart the ollama server, not sure why I missed that. Closing the issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#182