mirror of
https://github.com/ollama/ollama-python.git
synced 2026-07-21 09:05:23 -04:00
Structured output, json schema error #182
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 @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.3The 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)`
@c4wrd commented on GitHub (Dec 7, 2024):
I ran into the same issue. The issue was that I had only updated my
ollamaPython package, but theollamaserver (the main application process) was not updated. By installing the most recent version ofollama(binary), the issue was resolved.@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.