Skip to content

fix(grammars): exclude unescaped control characters in JSON string rules - #13417

Merged
mudler merged 1 commit into
mudler:masterfrom
Kirep-06:fix/issue-13414
Nov 23, 2026
Merged

mudler merged 1 commit into
mudler:masterfrom
Kirep-06:fix/issue-13414

Conversation

@Kirep-06

Copy link
Copy Markdown
Contributor

Description

This PR fixes #13414

Exclude raw control characters (U+0000–U+001F) and DEL (U+007F) from the unescaped character class in LocalAI's JSON GBNF string rules (PRIMITIVE_RULES["string"] in pkg/functions/grammars/bnf_rules.go and JSONBNF in pkg/functions/json_mode.go), aligning them with llama.cpp's JSON grammar ([^"\\\x7F\x00-\x1F]) and RFC 8259 Section 7.

Previously, both string rules used [^"\\], which allowed models under response_format (json_schema or json_object) to emit literal newlines or other raw control characters inside JSON string values, producing invalid JSON that clients reject on parse.

Notes for Reviewers

  • Production changes (2 lines):
    • pkg/functions/grammars/bnf_rules.go: updated PRIMITIVE_RULES["string"] from [^"\\] to [^"\\\x7F\x00-\x1F] (covers JSONSchemaConverter single-string, root-string, multi-type union rules, and LLama31SchemaConverter; leaves freestring untouched at [^\x00]).
    • pkg/functions/json_mode.go: updated JSONBNF's string rule from [^"\\] to [^"\\\x7F\x00-\x1F] so response_format: { "type": "json_object" } enforces the same constraint.
  • Test updates & regressions:
    • pkg/functions/grammars/json_schema_test.go & pkg/functions/grammars/llama31_schema_test.go: updated expected string rule snapshots to [^"\\\x7F\x00-\x1F], and updated stale freestring fixture lines from [^"\\] to [^\x00] (those fixture lines previously passed only because line-by-line ContainSubstring accidentally matched [^"\\] | inside the string rule).
    • Added Ginkgo/Gomega regression specs covering single-string properties (including character-class validation across U+0000–U+001F, U+007F, ", \, and printable ASCII/Unicode), root-level string schemas, multi-type ["string", "null"] unions, freestring preservation, and JSONBNF in pkg/functions/functions_test.go.
  • Verification:
    $ go test -v -race ./pkg/functions/grammars/...
    Ran 32 of 32 Specs in 0.025 seconds
    SUCCESS! -- 32 Passed | 0 Failed | 0 Pending | 0 Skipped
    ok  	github.com/mudler/LocalAI/pkg/functions/grammars	2.806s
    
    $ go test -race -tags nosqlite,purego_utp ./pkg/functions/...
    ok  	github.com/mudler/LocalAI/pkg/functions	3.115s
    ok  	github.com/mudler/LocalAI/pkg/functions/grammars	2.942s
    ok  	github.com/mudler/LocalAI/pkg/functions/peg	2.677s
    
    $ go vet -tags nosqlite,purego_utp ./pkg/functions/...
    $ gofmt -l pkg/functions/
    
    (Note: golangci-lint is not installed in this local environment; new tests follow .golangci.yml forbidigo rules using Ginkgo/Gomega exclusively.)

Signed commits

  • Yes, I signed my commits.
  • Documentation updated (docs/content/) for user-facing changes, or not applicable

Exclude raw control characters (U+0000-U+001F) and DEL (U+007F) from the
unescaped character class in PRIMITIVE_RULES["string"] and JSONBNF,
matching llama.cpp's GBNF JSON string rule so constrained json_schema
and json_object outputs stay valid RFC 8259 JSON.

Update existing grammar test fixtures (including stale freestring lines
that previously matched the string rule via substring matching) and add
regression tests for single-string, root-string, multi-type union, and
JSONBNF rules.

Signed-off-by: Kirep-06 <fanhong0910@163.com>
@mudler
mudler merged commit 83d36f8 into mudler:master Nov 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON grammar allows unescaped control characters inside strings

2 participants