Skip to content

Fix #236: treat max_tokens=0 as empty string - #356

Merged
hudson-ai merged 1 commit into
guidance-ai:mainfrom
pjdurden:fix/236-max-tokens-zero
Jun 17, 2026
Merged

Fix #236: treat max_tokens=0 as empty string#356
hudson-ai merged 1 commit into
guidance-ai:mainfrom
pjdurden:fix/236-max-tokens-zero

Conversation

@pjdurden

Copy link
Copy Markdown
Contributor

max_tokens=0 was compiled into a token-limited lexeme that never terminates at runtime (the {-then-garbage symptom in #236). Since a rule capped at zero tokens can only match the empty string, this special-cases it to compile to "", placed before the stop/terminal/subgrammar dispatch so it's handled uniformly for all body types. Includes a regression test (test_ll_max_tokens_zero) with the issue's repro plus a name: "" equivalence baseline. Full parser suite + fmt + clippy clean.

Copilot AI review requested due to automatic review settings June 14, 2026 15:46

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.

Fixes compilation/runtime behavior for Lark rules configured with max_tokens=0, aligning it with “empty rule” semantics and preventing non-terminating matcher output.

Changes:

  • Add a regression test asserting max_tokens=0 behaves like an explicitly empty rule.
  • Update the compiler to treat max_tokens=0 rules as matching the empty string (compile to "").

Reviewed changes

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

File Description
parser/tests/test_ll.rs Adds regression coverage for max_tokens=0 producing an empty match.
parser/src/lark/compiler.rs Implements the max_tokens=0 special-case by compiling it as an empty string.

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

Comment thread parser/src/lark/compiler.rs Outdated
Comment on lines +528 to +529
// max_tokens=0 means the rule is allowed to emit no tokens at all,
// i.e. it matches only the empty string. Compiling it as a

@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.

Ok. reworded to make the cap semantics explicit: max_tokens=N caps a rule at N emitted tokens, so max_tokens=0 forces exactly zero (epsilon) rather than being optional.

Comment thread parser/tests/test_ll.rs Outdated
fn test_ll_max_tokens_zero() {
// max_tokens=0 means the rule emits no tokens, i.e. it matches the empty
// string -- so `name` contributes nothing and we go straight to "xy".
// See issue #236.

@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.

done. switched to the full GitHub URL to match the compiler-side comment.

A rule with max_tokens=0 should be allowed to emit no tokens, i.e. match
only the empty string. It was instead compiled to a token-limited lexeme
that never terminates at runtime (emits an opening token then loops).
Special-case max_tokens=0 to compile to "", uniformly across terminal,
subgrammar and json bodies. Adds a regression test.

Fixes guidance-ai#236
@pjdurden
pjdurden force-pushed the fix/236-max-tokens-zero branch from c6f1a03 to e17ba90 Compare June 14, 2026 15:50
@hudson-ai hudson-ai linked an issue Jun 16, 2026 that may be closed by this pull request
@hudson-ai

Copy link
Copy Markdown
Contributor

Thank you for the fix! Looks clean to me. Running CI

@hudson-ai
hudson-ai merged commit 0dbc7f0 into guidance-ai:main Jun 17, 2026
15 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.

max_tokens=0 doesn't work

3 participants