Skip to content

fix(LlamaJsonSchemaGrammar.parse): count Unicode code points for minLength and maxLength checks - #675

Merged
giladgd merged 3 commits into
withcatai:masterfrom
ktz03:fix/json-schema-unicode-length
Oct 20, 2026
Merged

giladgd merged 3 commits into
withcatai:masterfrom
ktz03:fix/json-schema-unicode-length

Conversation

@ktz03

@ktz03 ktz03 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Description of change

LlamaJsonSchemaGrammar.parse rejects "😀" for {type: "string", minLength: 1, maxLength: 1} even though the native grammar accepts it. Conversely, parsing accepts the same single code point with minLength: 2, while the native grammar rejects it. The validator uses JavaScript's UTF-16 string length, which counts this character twice.

Count decoded Unicode code points for string length checks and their error messages. Add public grammar/parse regressions for ASCII, BMP characters, astral characters, combining characters, a ZWJ sequence, decoded JSON escape pairs, and object properties. GBNF generation is unchanged.

Validation on Windows with Node.js 24.21.0:

  • Five new regression cases fail on master; 25 existing or control cases pass. All 30 grammar tests pass after the fix against current compiled public modules in an isolated package runtime with the prebuilt Vulkan v0.5.0 backend.
  • 148 other offline standalone tests pass.
  • npm run format, npm run lint:eslint, and npm run test:typescript pass.
  • npm run docs:generateTypedoc and the VitePress documentation build pass.
  • A separate 31-case public API check passes after the fix.

The full model-dependent suite, other native suites, and live Hugging Face tests were not run. No model inference was performed.

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • npm run format to apply eslint formatting
  • npm run test passes with this change — the validation above passed; the full model-dependent suite was not run
  • This pull request links relevant issues as Fixes #0000 — N/A, no existing issue found for this validation error
  • There are new or updated unit tests validating the change
  • Documentation has been updated to reflect this change — N/A, no API or usage change
  • The new commits and pull request title follow conventions explained in pull request guidelines (PRs that do not follow this convention will not be merged)

@giladgd giladgd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR!
I've pushed a performance optimization so that validating the length against code points will be a cheaper and faster operation

@giladgd giladgd changed the title fix: count Unicode code points in JSON string lengths fix(LlamaJsonSchemaGrammar.parse): count Unicode code points for minLength and maxLength checks Oct 20, 2026
@giladgd
giladgd merged commit 8030e0d into withcatai:master Oct 20, 2026
22 checks 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.

2 participants