Skip to content

Accept negative numbers in multipleOf (#222) - #357

Merged
hudson-ai merged 3 commits into
guidance-ai:mainfrom
pjdurden:fix/222-multipleof-negative
Jun 17, 2026
Merged

Accept negative numbers in multipleOf (#222)#357
hudson-ai merged 3 commits into
guidance-ai:mainfrom
pjdurden:fix/222-multipleof-negative

Conversation

@pjdurden

Copy link
Copy Markdown
Contributor

RegexAst::MultipleOf only matches unsigned digits, so And-ing it with the signed range regex rejected all negative values — including valid negative multiples like -6 for multipleOf 3. This wraps it as Concat(["-?", MultipleOf]); divisibility is sign-independent and -? matches empty for positives (no regression). Adds negative integer cases and re-enables the maintainer-marked negative 3.5 cases.

Scope: this fixes the sign facet of #222. Two related facets are left as follow-ups and noted in the tests — (1) integer-valued multipleOf (e.g. 3.0) on number still chokes on the decimal point, and (2) scientific notation (3.5e22) is #216. Full suite + fmt + clippy clean.

RegexAst::MultipleOf only matches the unsigned digit sequence, so And-ing
it with the (signed) int/number range regex rejected every negative value,
including valid negative multiples (e.g. -6 for multipleOf 3). Wrap it as
Concat(["-?", MultipleOf]) so the sign is allowed; divisibility is
independent of sign. Adds negative integer cases and re-enables the
negative non-integer (3.5) cases.

Partially fixes guidance-ai#222 (sign handling). The decimal-point handling of
integer-valued multipleOf on `number` (e.g. 3.0) and scientific notation
(guidance-ai#216) are left as follow-ups.
Copilot AI review requested due to automatic review settings June 14, 2026 16:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR updates JSON Schema multipleOf handling to accept negative numeric literals and expands tests to cover negative cases.

Changes:

  • Extend integer multipleOf tests to include negative values.
  • Enable negative test cases for number multipleOf (non-integer multiples).
  • Update the JSON compiler to allow an optional leading - when applying RegexAst::MultipleOf.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
parser/tests/test_json_primitives.rs Expands multipleOf test matrices to include negative inputs and clarifies an issue reference.
parser/src/json/compiler.rs Adjusts regex AST construction so multipleOf validation accepts negative numeric strings via optional leading -.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread parser/src/json/compiler.rs Outdated
Comment on lines +305 to +313
// RegexAst::MultipleOf only matches the unsigned digit sequence, so
// allow an optional leading '-' to accept negative multiples (e.g.
// -6 for multipleOf 3); divisibility is independent of sign.
// https://github.com/guidance-ai/llguidance/issues/222
let multiple_of = RegexAst::Concat(vec![
RegexAst::Regex("-?".to_string()),
RegexAst::MultipleOf(d.coef, d.exp),
]);
ast = RegexAst::And(vec![ast, multiple_of]);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Extracted a signed_multiple_of_ast(coef, exp) helper and used it in both json_int and json_number, so the construction is no longer duplicated.

Comment thread parser/src/json/compiler.rs Outdated
Comment on lines +305 to +307
// RegexAst::MultipleOf only matches the unsigned digit sequence, so
// allow an optional leading '-' to accept negative multiples (e.g.
// -6 for multipleOf 3); divisibility is independent of sign.

@pjdurden pjdurden Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reworded it. RegexAst::MultipleOf matches the unsigned numeric literal (the digits, plus a decimal point for non-integer multipleOf), not just a digit sequence. Updated the comment to say that.

Dedupe the optional-sign multipleOf construction across json_int and
json_number into a helper; reword the comment to note MultipleOf matches
the unsigned numeric literal (incl. decimal point), not just digits.
@pjdurden

Copy link
Copy Markdown
Contributor Author

@riedgar-ms the two red checks are baseline mismatches from this fix improving results (MaskBench reports 0 errors, 2 improvements), so the expected_maskbench.json and expected_json_schema_test_suite.json snapshots need regenerating rather than any logic change.

@riedgar-ms

Copy link
Copy Markdown
Contributor

@riedgar-ms the two red checks are baseline mismatches from this fix improving results (MaskBench reports 0 errors, 2 improvements), so the expected_maskbench.json and expected_json_schema_test_suite.json snapshots need regenerating rather than any logic change.

Makes sense - can you update the JSON so that the tests pass? It's a manual process unfortunately (and the update might be to 'newly revealed test failure' not 'test now passes')

@pjdurden

pjdurden commented Jun 17, 2026

Copy link
Copy Markdown
Contributor Author

@riedgar-ms updated expected_maskbench.json.
btw the JSON Schema Test Suite check looks unrelated: it exited in about 90ms with no test output, which is a startup crash rather than a baseline mismatch (the suite never ran). On this commit it passes locally (all 1897 results match the existing baseline), so I think it was a transient failure. Could you re-run that check?

@riedgar-ms riedgar-ms left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm OK with this. Thoughts @hudson-ai ?

@riedgar-ms
riedgar-ms requested a review from hudson-ai June 17, 2026 16:45

@hudson-ai hudson-ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks great! Agreed that it doesn't quite close #222, but it addresses an important facet. Thank you!

@hudson-ai
hudson-ai merged commit c9bedc5 into guidance-ai:main Jun 17, 2026
28 of 29 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.

4 participants