Repository navigation
fix(grammars): exclude unescaped control characters in JSON string rules - #13417
Merged
Merged
Conversation
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>
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.
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 GBNFstringrules (PRIMITIVE_RULES["string"]inpkg/functions/grammars/bnf_rules.goandJSONBNFinpkg/functions/json_mode.go), aligning them withllama.cpp's JSON grammar ([^"\\\x7F\x00-\x1F]) and RFC 8259 Section 7.Previously, both
stringrules used[^"\\], which allowed models underresponse_format(json_schemaorjson_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
pkg/functions/grammars/bnf_rules.go: updatedPRIMITIVE_RULES["string"]from[^"\\]to[^"\\\x7F\x00-\x1F](coversJSONSchemaConvertersingle-string, root-string, multi-type union rules, andLLama31SchemaConverter; leavesfreestringuntouched at[^\x00]).pkg/functions/json_mode.go: updatedJSONBNF'sstringrule from[^"\\]to[^"\\\x7F\x00-\x1F]soresponse_format: { "type": "json_object" }enforces the same constraint.pkg/functions/grammars/json_schema_test.go&pkg/functions/grammars/llama31_schema_test.go: updated expectedstringrule snapshots to[^"\\\x7F\x00-\x1F], and updated stalefreestringfixture lines from[^"\\]to[^\x00](those fixture lines previously passed only because line-by-lineContainSubstringaccidentally matched[^"\\] |inside thestringrule).U+0000–U+001F,U+007F,",\, and printable ASCII/Unicode), root-level string schemas, multi-type["string", "null"]unions,freestringpreservation, andJSONBNFinpkg/functions/functions_test.go.golangci-lintis not installed in this local environment; new tests follow.golangci.ymlforbidigorules using Ginkgo/Gomega exclusively.)Signed commits