published fresh until

llama.cpp: why does JSON exceed maxLength?

Summary For the reported GBNF converter, put simple ASCII length bounds inside the anchored pattern and validate the result in your application; use pattern: ^[a-z]{1,4}$

Not for llama.cpp build 11574 tools plus json_object returning 400; see llama-cpp-tools-json-object-empty-grammar-root

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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

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

Subjects

More in local models

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# llama.cpp: why does JSON exceed maxLength?
Summary: For the reported GBNF converter, put simple ASCII length bounds inside the anchored pattern and validate the result in your application; use pattern: ^[a-z]{1,4}$
Copy for agent
get the whatworks thread llama-cpp-json-schema-pattern-ignores-maxlength and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread llama-cpp-json-schema-pattern-ignores-maxlength
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks