What this thread does
Put the length bound inside a simple anchored ASCII pattern, and validate the decoded response against the original schema before accepting it.
This concerns the built-in GBNF converter on master at eb1e1f495f, rather than every grammar backend.
What changed
- amitabhsh's October 18 report, with CyberStorm75's October 20 b11718 follow-up, shows a schema containing both a regex and a string-length limit can admit longer strings.
- bantianyue's October 29 repair proposal combines bounds for simple patterns and proposes warnings for harder cases. The proposal was closed without merging on November 17; the issue was also closed that day.
Do first: move simple bounds into pattern
For an identifier consisting of one to four lowercase ASCII letters, change this schema:
{"type":"string","pattern":"^[a-z]+$","minLength":1,"maxLength":4}
To this equivalent constraint for that specific alphabet:
{"type":"string","pattern":"^[a-z]{1,4}$","minLength":1,"maxLength":4}
This is a narrow workaround inferred from the converter's pattern branch and the proposed repair's example; it is not a general regex-intersection algorithm. The reported master converter returns from the pattern branch before visiting the separate length bounds.
Send the schema through json_schema on the native completion route or inside response_format on chat completions. Describe the requested identifier in the prompt as well: the versioned GBNF guide says output constraints do not automatically tell the model what to produce.
Keep the original schema in your application validator. Reject an overlong result before storing it or using it as a tool argument; regenerate with a bounded retry policy. Do not silently truncate an identifier, since that changes its value.
Diagnostic: valid JSON exceeds maxLength
This can be a schema-enforcement failure even when JSON parsing succeeds. To inspect the server-generated grammar without requesting output tokens, the issue gives this native POST /completion body:
{
"prompt":"Return a lowercase identifier.",
"n_predict":0,
"json_schema":{"type":"string","pattern":"^[a-z]+$","maxLength":4},
"response_fields":["generation_settings/grammar"]
}
Inspect whether the string production contains an unbounded repetition. The report's example admits an eight-letter value despite the four-character limit. Reproduction and generated grammar
For a regression check in your application, include one valid short identifier, an empty string, an uppercase string and an overlong lowercase string. Parsing JSON alone cannot distinguish these cases.
Evidence and limits
amitabhsh describes the failure as “The length bound disappears”; the public report provides the schema and generated grammar, and the follow-up names b11718. The source code independently explains the dropped constraint. The official guide documents only a subset of JSON Schema, requires anchored patterns and warns that unsupported features may disappear silently.
This workaround is limited to simple ASCII character classes. More complex regexes, Unicode length semantics and other schema-keyword interactions need separate checks. The closed proposal does not promise arbitrary intersections.
Not for: llama.cpp build 11574 tools plus json_object returning 400; see the empty-root grammar failure.
Started from a post by @amitabhsh; CyberStorm75; bantianyue, packaged by the editor. Original post