[GH-ISSUE #11016] think=True crashes structured output #53776

Closed
opened 2026-04-29 04:44:29 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @raffaelemancuso on GitHub (Jun 8, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/11016

What is the issue?

from pydantic import BaseModel

from ollama import chat


# Define the schema for the response
class FriendInfo(BaseModel):
  name: str
  age: int
  is_available: bool


class FriendList(BaseModel):
  friends: list[FriendInfo]


# schema = {'type': 'object', 'properties': {'friends': {'type': 'array', 'items': {'type': 'object', 'properties': {'name': {'type': 'string'}, 'age': {'type': 'integer'}, 'is_available': {'type': 'boolean'}}, 'required': ['name', 'age', 'is_available']}}}, 'required': ['friends']}
response = chat(
  model='deepseek-r1:1.5b',
  messages=[{'role': 'user', 'content': 'I have two friends. The first is Ollama 22 years old busy saving the world, and the second is Alonso 23 years old and wants to hang out. Return a list of friends in JSON format'}],
  format=FriendList.model_json_schema(),  # Use Pydantic to generate the schema or format=schema
  options={'temperature': 0},  # Make responses more deterministic
  think=True
)

# Use Pydantic to validate the response
friends_response = FriendList.model_validate_json(response.message.content)
print(friends_response)

Relevant log output

---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[3], line 27
     18 response = chat(
     19   model='deepseek-r1:1.5b',
     20   messages=[{'role': 'user', 'content': 'I have two friends. The first is Ollama 22 years old busy saving the world, and the second is Alonso 23 years old and wants to hang out. Return a list of friends in JSON format'}],
   (...)     23   think=True
     24 )
     26 # Use Pydantic to validate the response
---> 27 friends_response = FriendList.model_validate_json(response.message.content)
     28 print(friends_response)

File c:\Users\raffaele\venvs\gensim\Lib\site-packages\pydantic\main.py:746, in BaseModel.model_validate_json(cls, json_data, strict, context, by_alias, by_name)
    740 if by_alias is False and by_name is not True:
    741     raise PydanticUserError(
    742         'At least one of `by_alias` or `by_name` must be set to True.',
    743         code='validate-by-alias-and-name-false',
    744     )
--> 746 return cls.__pydantic_validator__.validate_json(
    747     json_data, strict=strict, context=context, by_alias=by_alias, by_name=by_name
    748 )

ValidationError: 1 validation error for FriendList
  Invalid JSON: expected `:` at line 1 column 5 [type=json_invalid, input_value='{"{"friends": [{"name": ...t\t\t\t\t\t\t\t\t\t\t\t', input_type=str]
    For further information visit https://errors.pydantic.dev/2.11/v/json_invalid

Ollama version

0.9

Originally created by @raffaelemancuso on GitHub (Jun 8, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/11016 ### What is the issue? ``` from pydantic import BaseModel from ollama import chat # Define the schema for the response class FriendInfo(BaseModel): name: str age: int is_available: bool class FriendList(BaseModel): friends: list[FriendInfo] # schema = {'type': 'object', 'properties': {'friends': {'type': 'array', 'items': {'type': 'object', 'properties': {'name': {'type': 'string'}, 'age': {'type': 'integer'}, 'is_available': {'type': 'boolean'}}, 'required': ['name', 'age', 'is_available']}}}, 'required': ['friends']} response = chat( model='deepseek-r1:1.5b', messages=[{'role': 'user', 'content': 'I have two friends. The first is Ollama 22 years old busy saving the world, and the second is Alonso 23 years old and wants to hang out. Return a list of friends in JSON format'}], format=FriendList.model_json_schema(), # Use Pydantic to generate the schema or format=schema options={'temperature': 0}, # Make responses more deterministic think=True ) # Use Pydantic to validate the response friends_response = FriendList.model_validate_json(response.message.content) print(friends_response) ``` ### Relevant log output ```shell --------------------------------------------------------------------------- ValidationError Traceback (most recent call last) Cell In[3], line 27 18 response = chat( 19 model='deepseek-r1:1.5b', 20 messages=[{'role': 'user', 'content': 'I have two friends. The first is Ollama 22 years old busy saving the world, and the second is Alonso 23 years old and wants to hang out. Return a list of friends in JSON format'}], (...) 23 think=True 24 ) 26 # Use Pydantic to validate the response ---> 27 friends_response = FriendList.model_validate_json(response.message.content) 28 print(friends_response) File c:\Users\raffaele\venvs\gensim\Lib\site-packages\pydantic\main.py:746, in BaseModel.model_validate_json(cls, json_data, strict, context, by_alias, by_name) 740 if by_alias is False and by_name is not True: 741 raise PydanticUserError( 742 'At least one of `by_alias` or `by_name` must be set to True.', 743 code='validate-by-alias-and-name-false', 744 ) --> 746 return cls.__pydantic_validator__.validate_json( 747 json_data, strict=strict, context=context, by_alias=by_alias, by_name=by_name 748 ) ValidationError: 1 validation error for FriendList Invalid JSON: expected `:` at line 1 column 5 [type=json_invalid, input_value='{"{"friends": [{"name": ...t\t\t\t\t\t\t\t\t\t\t\t', input_type=str] For further information visit https://errors.pydantic.dev/2.11/v/json_invalid ``` ### Ollama version 0.9
GiteaMirror added the bug label 2026-04-29 04:44:30 -05:00
Author
Owner

@rick-github commented on GitHub (Jun 8, 2025):

#10929

<!-- gh-comment-id:2953755699 --> @rick-github commented on GitHub (Jun 8, 2025): #10929
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#53776