[BUG] Regular expressions containing escapes cause ResponseErrors #280

Closed
opened 2026-02-15 16:29:32 -05:00 by yindo · 1 comment
Owner

Originally created by @jack5github on GitHub (Jul 16, 2025).

Ollama (version 0.9.6) Python library (version 0.5.1) throws ResponseError('Failed to create new sequence: unable to create sampling context\n') when attempting to use regular expressions that contain escapes. Code proving the error can be found below:

from ollama import generate
from pydantic import BaseModel, Field
from re import compile
from sys import argv
from typing import Annotated

class OllamaString(BaseModel):
    response: Annotated[str, Field(pattern="^[a-zA-Z_][a-zA-Z0-9._]*$")]

class OllamaEscapedString(BaseModel):
    response: Annotated[str, Field(pattern="^[a-zA-Z_][a-zA-Z0-9\\._]*$")]

regex: str = r"^[a-zA-Z_][a-zA-Z0-9._]*$"
regex_escaped: str = r"^[a-zA-Z_][a-zA-Z0-9\._]*$"

class OllamaRegex(BaseModel):
    response: Annotated[str, Field(pattern=regex)]

class OllamaEscapedRegex(BaseModel):
    response: Annotated[str, Field(pattern=regex_escaped)]

class OllamaCompiledRegex(BaseModel):
    response: Annotated[str, Field(pattern=compile(regex))]

class OllamaEscapedCompiledRegex(BaseModel):
    response: Annotated[str, Field(pattern=compile(regex_escaped))]

for model in (
    OllamaString,
    OllamaEscapedString,  # Fails
    OllamaRegex,
    OllamaEscapedRegex,  # Fails
    OllamaCompiledRegex,
    OllamaEscapedCompiledRegex,  # Fails
):
    try:
        print(
            generate(
                model=argv[1],
                system="Generate a JSON response with the schema `{'response': ...}` that matches the given regular expression.",
                prompt=str(model.model_fields["response"].metadata[0].pattern),
                format=model.model_json_schema(),
            ).response.strip()
        )
    except Exception as err:
        print(f"{model.__name__} failed: {repr(err)}")
Originally created by @jack5github on GitHub (Jul 16, 2025). Ollama (version 0.9.6) Python library (version 0.5.1) throws `ResponseError('Failed to create new sequence: unable to create sampling context\n')` when attempting to use regular expressions that contain escapes. Code proving the error can be found below: ``` from ollama import generate from pydantic import BaseModel, Field from re import compile from sys import argv from typing import Annotated class OllamaString(BaseModel): response: Annotated[str, Field(pattern="^[a-zA-Z_][a-zA-Z0-9._]*$")] class OllamaEscapedString(BaseModel): response: Annotated[str, Field(pattern="^[a-zA-Z_][a-zA-Z0-9\\._]*$")] regex: str = r"^[a-zA-Z_][a-zA-Z0-9._]*$" regex_escaped: str = r"^[a-zA-Z_][a-zA-Z0-9\._]*$" class OllamaRegex(BaseModel): response: Annotated[str, Field(pattern=regex)] class OllamaEscapedRegex(BaseModel): response: Annotated[str, Field(pattern=regex_escaped)] class OllamaCompiledRegex(BaseModel): response: Annotated[str, Field(pattern=compile(regex))] class OllamaEscapedCompiledRegex(BaseModel): response: Annotated[str, Field(pattern=compile(regex_escaped))] for model in ( OllamaString, OllamaEscapedString, # Fails OllamaRegex, OllamaEscapedRegex, # Fails OllamaCompiledRegex, OllamaEscapedCompiledRegex, # Fails ): try: print( generate( model=argv[1], system="Generate a JSON response with the schema `{'response': ...}` that matches the given regular expression.", prompt=str(model.model_fields["response"].metadata[0].pattern), format=model.model_json_schema(), ).response.strip() ) except Exception as err: print(f"{model.__name__} failed: {repr(err)}") ```
yindo closed this issue 2026-02-15 16:29:33 -05:00
Author
Owner

@ParthSareen commented on GitHub (Jul 22, 2025):

Hey @jack5github! We don't support regex as fields - although the error should be improved to communicate that

@ParthSareen commented on GitHub (Jul 22, 2025): Hey @jack5github! We don't support regex as fields - although the error should be improved to communicate that
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#280