[GH-ISSUE #15416] gemma4:e4b thinking failing with structured output #9856

Closed
opened 2026-04-12 22:43:03 -05:00 by GiteaMirror · 0 comments
Owner

Originally created by @Raa23 on GitHub (Apr 8, 2026).
Original GitHub issue: https://github.com/ollama/ollama/issues/15416

What is the issue?

llm client calls produce output with json fences when thinking is not set to False.
using package ollama 0.6.1

Reproducible code:

import ollama
from pydantic import BaseModel, ValidationError

class TestModel(BaseModel):
    name: str
    age: int

def test_llm_client():
    client = ollama.Client()
    system = "You are a helpful assistant that generates JSON objects with name and age."
    prompt = "Generate a JSON object with name and age."
    try:
        response = client.chat(
            model="gemma4:e4b",
            messages=[
                    {"role": "system", "content": system},
                    {"role": "user",   "content": prompt},
                ],
                format=TestModel.model_json_schema(),
                think="low",
                options={
                    "temperature": 1.0,
                    "top_p": 0.95,
                    "top_k": 64,
                },
            )
        content = response.message.content or ""
        print("LLM Response:", content)
        result = TestModel.model_validate_json(content)
        print("Parsed Result:", result)
    except ValidationError as ve:
        print("Validation Error:", ve)
    except Exception as e:
        print("Error:", e)

if __name__ == "__main__":
    test_llm_client()

Relevant log output

LLM Response: 
{
  "name": "Alice",
  "age": 30
}

Validation Error: 1 validation error for TestModel
  Invalid JSON: expected value at line 1 column 1 [type=json_invalid, input_value='\n{\n  "name": "A...",\n  "age": 30\n}\n', input_type=str]
    For further information visit https://errors.pydantic.dev/2.12/v/json_invalid

OS

Windows

GPU

Nvidia

CPU

Intel

Ollama version

0.20.3

Originally created by @Raa23 on GitHub (Apr 8, 2026). Original GitHub issue: https://github.com/ollama/ollama/issues/15416 ### What is the issue? llm client calls produce output with json fences when thinking is not set to False. using package ollama 0.6.1 Reproducible code: ```python import ollama from pydantic import BaseModel, ValidationError class TestModel(BaseModel): name: str age: int def test_llm_client(): client = ollama.Client() system = "You are a helpful assistant that generates JSON objects with name and age." prompt = "Generate a JSON object with name and age." try: response = client.chat( model="gemma4:e4b", messages=[ {"role": "system", "content": system}, {"role": "user", "content": prompt}, ], format=TestModel.model_json_schema(), think="low", options={ "temperature": 1.0, "top_p": 0.95, "top_k": 64, }, ) content = response.message.content or "" print("LLM Response:", content) result = TestModel.model_validate_json(content) print("Parsed Result:", result) except ValidationError as ve: print("Validation Error:", ve) except Exception as e: print("Error:", e) if __name__ == "__main__": test_llm_client() ``` ### Relevant log output ```shell LLM Response: { "name": "Alice", "age": 30 } Validation Error: 1 validation error for TestModel Invalid JSON: expected value at line 1 column 1 [type=json_invalid, input_value='\n{\n "name": "A...",\n "age": 30\n}\n', input_type=str] For further information visit https://errors.pydantic.dev/2.12/v/json_invalid ``` ### OS Windows ### GPU Nvidia ### CPU Intel ### Ollama version 0.20.3
GiteaMirror added the bug label 2026-04-12 22:43:03 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#9856