Repository navigation
Support integer minimum, maximium in JSON schemas - #2475
Open
yezhengli-Mr9 wants to merge 2 commits into
Open
yezhengli-Mr9 wants to merge 2 commits into
yezhengli-Mr9 wants to merge 2 commits into
Conversation
yezhengli-Mr9
marked this pull request as ready for review
December 4, 2026 03:54
This branch has not been deployed
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.
Reference PRs
Compatibility status: llama.cpp #7797's Python implementation is the selected exact-match target, including its limitations. The current code in this PR is not yet an exact port: it separately normalizes fractional bounds, intersects simultaneous inclusive/exclusive bounds, supports long one-sided integers without the reference's default digit cap, and explicitly admits
-0whenever zero is allowed. The implementation and validation results below describe the current code, not verified equivalence with #7797.Summary and logic
Enforce JSON Schema integer bounds during grammar-constrained generation instead of silently ignoring them, for tool arguments such as retry counts, line numbers and edit offsets.
ceil(minimum)/floor(maximum), and exclusive bounds withfloor(exclusiveMinimum) + 1/ceil(exclusiveMaximum) - 1. Intersect simultaneous bounds.-0exactly when zero is admissible. Preserve JSON integer syntax and allow valid long integers in bounded schemas.ValueErrorif the intersection contains no integer; never fall back to an unrestricted integer.Only explicitly typed
integerschemas take the new path, including those reached recursively by the existing visitor. Unbounded schemas and existing union/type-list handling are unchanged. Real-number bounds,multipleOfand general schema satisfiability remain out of scope.Concrete API example
Generated grammar:
This admits complete values
8,9,10,11,12, but rejects7,13,999, and the incomplete prefix1at EOS. For example,minimum=0, exclusiveMinimum=1, maximum=5, exclusiveMaximum=4admits only2and3.Measured comparison
Re-executed against base
77fbb5d7190149d20870cd8d73646a5df3e72b05, with the actual native grammar sampler and EOS checks: 25 schemas x 290 candidate strings = 7,250 decisions per converter.The four original false rejections are valid 21-digit integers excluded by the existing unrestricted integer primitive's digit limit. That primitive is unchanged; bounded schemas no longer inherit the unrelated limit.
Representation optimization: prefix rules instead of value enumeration
These are measured representation sizes, not a throughput benchmark. The original converter's smaller unrestricted grammar is not a correct implementation of the bounded schema. No general inference-speed or AI-coding token-saving claim is made.
Real local coding-model sample
Re-executed with the existing local
qwen2.5-coder:1.5bGGUF; no model download. Prompt:999/9999/9999/999-9/-9999/9991/1Twelve completed calls / six matched pairs, all finishing with
finish_reason="stop". This deliberately adversarial range-conformance smoke test is not a representative coding benchmark; the smaller completion-token counts reflect shorter numbers, and repeated seeds are not independent tasks.Settings:
n_ctx=512,n_batch=128,n_threads=2,n_threads_batch=2,max_tokens=12,temperature=0.2,top_p=0.9,repeat_penalty=1.0; seeds 0 and 1, model state reset before each call. Ubuntu 24.04 Docker, Python 3.12, 2 CPUs / 4 GiB limit, networking disabled, capabilities dropped, model volume and source read-only.Focused test cleanup and validation
finally.98..321, covering shared prefixes, an interior digit branch and a digit-length transition without another test case. Retain distinct negative, zero-crossing, one-sided, zero-only, inclusive/exclusive fractional, intersected and empty-range cases.llama-cpp-python==0.3.38CPU wheel libraries selected viaLLAMA_CPP_LIB_PATH; not rebuilt from sourceExact matrix inputs and experiment identity
The matrix uses the committed test fixture
grammar_acceptsfor both converter classes, loading the original converter withgit show 77fbb5d7190149d20870cd8d73646a5df3e72b05:llama_cpp/llama_grammar.py.Inclusive
(minimum, maximum)intervals:[ (-130, -110), (-101, -99), (-100, -1), (-12, -8), (-10, 0), (-2, 2), (-1, 0), (0, 0), (0, 1), (0, 9), (0, 10), (1, 1), (1, 9), (8, 12), (9, 100), (10, 99), (98, 102), (110, 130), ]Seven additional
type="integer"bound dictionaries:[ {"minimum": 98}, {"maximum": -98}, {"minimum": -2}, {"maximum": 2}, {"minimum": 1.5, "maximum": 4.5}, {"exclusiveMinimum": -1.5, "exclusiveMaximum": 2.5}, {"minimum": 0, "exclusiveMinimum": 1, "maximum": 5, "exclusiveMaximum": 4}, ]Candidates:
The oracle requires
re.fullmatch(r"-?(0|[1-9][0-9]*)", text), then comparesint(text)with every applicable inclusive/exclusive bound. Grammar-size measurements uselen(grammar._grammar.encode()).Existing model GGUF SHA-256:
29d8c98fa6b098e200069bfb88b9508dc3e85586d20cba59f8dda9a808165104.For inference, instantiate
Llamawith the recorded settings and pass each converter class'sfrom_json_schemaresult asgrammartocreate_completion, resetting state before each interval/seed/converter call.