[GH-ISSUE #8325] Invalid JSON Schema when using pydantic pattern #5333

Closed
opened 2026-04-12 16:31:59 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @ReinderVosDeWael on GitHub (Jan 6, 2025).
Original GitHub issue: https://github.com/ollama/ollama/issues/8325

What is the issue?

When running ollama chat with structured output and a regex pattern, I get the following error: ollama._types.ResponseError: invalid JSON schema in format. This seems to be exclusive to the pattern argument; removing it resolves the issue.

import pydantic
import ollama

class Test(pydantic.BaseModel):
    text: str = pydantic.Field(..., pattern="[0-9]")

ollama.chat(
    model="llama3.2:1b", 
    format=Test.model_json_schema(), 
    messages=[{"role":"system", "message": "Say 7"}, {"role":"user", "message": "say what"}]
)

OS

macOS

GPU

Other

CPU

Apple

Ollama version

0.5.4

Originally created by @ReinderVosDeWael on GitHub (Jan 6, 2025). Original GitHub issue: https://github.com/ollama/ollama/issues/8325 ### What is the issue? When running ollama chat with structured output and a regex pattern, I get the following error: `ollama._types.ResponseError: invalid JSON schema in format`. This seems to be exclusive to the pattern argument; removing it resolves the issue. ```python import pydantic import ollama class Test(pydantic.BaseModel): text: str = pydantic.Field(..., pattern="[0-9]") ollama.chat( model="llama3.2:1b", format=Test.model_json_schema(), messages=[{"role":"system", "message": "Say 7"}, {"role":"user", "message": "say what"}] ) ``` ### OS macOS ### GPU Other ### CPU Apple ### Ollama version 0.5.4
GiteaMirror added the bug label 2026-04-12 16:31:59 -05:00
Author
Owner

@rick-github commented on GitHub (Jan 7, 2025):

--- ./8325.py.orig	2025-01-07 12:58:48.820325234 +1000
+++ ./8325.py	2025-01-07 12:59:18.348706506 +1000
@@ -2,10 +2,10 @@
 import ollama
 
 class Test(pydantic.BaseModel):
-    text: str = pydantic.Field(..., pattern="[0-9]")
+    text: str = pydantic.Field(..., pattern="^[0-9]$")
 
 ollama.chat(
     model="llama3.2:1b", 
     format=Test.model_json_schema(), 
-    messages=[{"role":"system", "message": "Say 7"}, {"role":"user", "message": "say what"}]
+    messages=[{"role":"system", "content": "Say 7"}, {"role":"user", "content": "say what"}]
 )
<!-- gh-comment-id:2574299535 --> @rick-github commented on GitHub (Jan 7, 2025): ```diff --- ./8325.py.orig 2025-01-07 12:58:48.820325234 +1000 +++ ./8325.py 2025-01-07 12:59:18.348706506 +1000 @@ -2,10 +2,10 @@ import ollama class Test(pydantic.BaseModel): - text: str = pydantic.Field(..., pattern="[0-9]") + text: str = pydantic.Field(..., pattern="^[0-9]$") ollama.chat( model="llama3.2:1b", format=Test.model_json_schema(), - messages=[{"role":"system", "message": "Say 7"}, {"role":"user", "message": "say what"}] + messages=[{"role":"system", "content": "Say 7"}, {"role":"user", "content": "say what"}] ) ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/ollama#5333