fix: normalize escaped pattern literals in tool/format schemas passed to llama-server by ManoharPaturi · Pull Request #18248 · ollama/ollama · GitHub
Skip to content

fix: normalize escaped pattern literals in tool/format schemas passed to llama-server - #18248

Open
ManoharPaturi wants to merge 1 commit into
ollama:mainfrom
ManoharPaturi:fix/grammar-pattern-escapes
Open

ManoharPaturi wants to merge 1 commit into
ollama:mainfrom
ManoharPaturi:fix/grammar-pattern-escapes

Conversation

@ManoharPaturi

Copy link
Copy Markdown

Fixes #18226

Root cause

The asymmetry is in what Ollama forwards to llama-server. All API surfaces decode tool schemas into typed structs (api/types.go): ToolProperty has no pattern field, so a top-level string pattern is silently dropped during decoding (never compiled → 200 OK). But Items is any, so anything nested under array → items survives verbatim — including pattern values containing the ECMAScript identity escapes \/ and \-. In native chat mode, llama.cpp compiles tool schemas into the tool-call grammar and its regex dialect rejects those identity escapes → 400 Failed to initialize samplers: failed to parse grammar. This matches the issue's table exactly, including why Claude Code's Artifact tool (writes[].path pattern) breaks interactive mode against Ollama.

Fix

Unescape \/ → / and \- → - in every schema pattern value at the Ollama → llama-server boundary (llm/llama_server.go): applied to body["tools"], the chat response_format json_schema, and the completion json_schema (format schemas are raw at all depths, so escaped patterns failed there too). Semantics-preserving in every regex dialect (identity escapes carry no meaning); \\ is consumed as a unit so escaped-backslash patterns are untouched; other escapes and lookaheads pass through.

Notes kept out of scope deliberately: the top-level pattern dropping by the typed decode is a pre-existing schema-subset limitation (widening it is a separate decision), and the macOS mlxrunner xgrammar path is noted as a follow-up.

Tests

llm/llama_server_test.go:

  • TestLlamaServerChatRequestNormalizesToolPatterns — both placements from the issue plus the Claude Code pattern, with plain-pattern and escaped-backslash invariants
  • TestLlamaServerChatResponseFormatNormalizesPatterns
  • TestUnescapePatternLiterals
  • 2 new cases in the existing TestLlamaServerCompletionRequestFormat

go test ./llm/... passes, plus ./anthropic/ and ./api/; go vet ./llm/ and gofmt -l clean.

Copilot AI lite review requested due to automatic review settings September 5, 2026 02:19

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@ManoharPaturi
ManoharPaturi force-pushed the fix/grammar-pattern-escapes branch from 513b113 to 9225bbb Compare October 4, 2026 05:20
@ManoharPaturi

Copy link
Copy Markdown
Author

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants