fix: reject negative counts in exactly/at_least/at_most/between#1922
Merged
RobinPicard merged 1 commit intoJul 20, 2026
Merged
Conversation
QuantifyExact, QuantifyMinimum, and QuantifyMaximum accepted negative
counts with no validation, unlike their sibling QuantifyBetween (which
only validated min_count <= max_count, itself missing a non-negative
check). A negative count silently produces a regex quantifier Python's
re engine doesn't recognize (e.g. `{-1}`), which `re` then treats as
literal text instead of raising — so `exactly(-1, "a")` builds a term
that matches the literal string "a{-1}" rather than failing loudly.
Add a __post_init__ guard to all four Quantify* dataclasses, mirroring
the existing min_count/max_count check on QuantifyBetween.
Disclosure: I used an AI coding assistant (Claude) to help identify
this bug and draft the fix; I reviewed the diff, wrote the regression
tests, and ran the full test suite plus pre-commit locally before
opening this PR.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
📚 Documentation preview: https://dottxt-ai.github.io/outlines/pr-preview/pr-1922/ Preview updates automatically with each commit. |
RobinPicard
approved these changes
Jul 20, 2026
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.
What
QuantifyExact,QuantifyMinimum, andQuantifyMaximum(backingexactly(),at_least(),at_most()) accepted negative counts with no validation.QuantifyBetween(backingbetween()) validatedmin_count <= max_countbut was also missing a non-negative check.Why this matters
A negative count silently produces a regex quantifier that Python's
reengine doesn't recognize as a quantifier at all (e.g.{-1}), sorefalls back to treating it as literal text:Instead of failing at construction time with a clear error, the term silently becomes something that matches a completely different (and almost certainly unintended) string.
Fix
Add a
__post_init__guard to all fourQuantify*dataclasses, mirroring the existingmin_count <= max_countcheck already onQuantifyBetween.Testing
tests/types/test_dsl.py::test_dsl_initcovering all four classes; confirmed they fail without the fix (DID NOT RAISE ValueError) and pass with it.pytest tests/types/(232 passed) andpre-commit run --files src/outlines/types/dsl.py tests/types/test_dsl.py(mypy + ruff clean).exactly/at_least/at_most/betweenfactory functions andTerminstance methods, which pass through unchanged.Disclosure: I used an AI coding assistant (Claude) to help identify this bug and draft the fix. I reviewed the diff, wrote/ran the regression tests, and ran the full local test suite + pre-commit before opening this PR.