{{ message }}
Repository navigation
fix: normalize escaped pattern literals in tool/format schemas passed to llama-server - #18248
Open
ManoharPaturi wants to merge 1 commit into
Open
ManoharPaturi wants to merge 1 commit into
ManoharPaturi wants to merge 1 commit into
Conversation
ManoharPaturi
force-pushed
the
fix/grammar-pattern-escapes
branch
from
October 4, 2026 05:20
513b113 to
9225bbb
Compare
Author
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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):ToolPropertyhas nopatternfield, so a top-level stringpatternis silently dropped during decoding (never compiled → 200 OK). ButItemsisany, so anything nested underarray→itemssurvives verbatim — includingpatternvalues 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'sArtifacttool (writes[].pathpattern) breaks interactive mode against Ollama.Fix
Unescape
\/→/and\-→-in every schemapatternvalue at the Ollama → llama-server boundary (llm/llama_server.go): applied tobody["tools"], the chatresponse_formatjson_schema, and the completionjson_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 invariantsTestLlamaServerChatResponseFormatNormalizesPatternsTestUnescapePatternLiteralsTestLlamaServerCompletionRequestFormatgo test ./llm/...passes, plus./anthropic/and./api/;go vet ./llm/andgofmt -lclean.