Skip to content

fix: propagate grammar errors and improve llguidance backend#20467

Merged
hnyls2002 merged 7 commits intosgl-project:mainfrom
jellysnack:fix/grammar-error-propagation
Mar 15, 2026
Merged

fix: propagate grammar errors and improve llguidance backend#20467
hnyls2002 merged 7 commits intosgl-project:mainfrom
jellysnack:fix/grammar-error-propagation

Conversation

@jellysnack
Copy link
Copy Markdown
Contributor

Motivation

This PR addresses two related issues:

  1. Opaque error messages on invalid grammars: When a grammar fails to compile, the original error from the backend is only logged server-side. The user receives a generic message like "Invalid grammar request" with no indication of what actually went wrong.

  2. Silent failures and missing features in llguidance backend: When an invalid schema is provided, the matcher is created in an error state but this wasn't being checked. Generation would proceed and return empty output with no error indication. Additionally, rollback() was not implemented, causing speculative decoding to fail.

Modifications

Error propagation across all grammar backends:

  • Add InvalidGrammarObject(BaseGrammarObject) class in base_grammar_backend.py with an error_message field to carry the original compilation error
  • Replace opaque INVALID_GRAMMAR_OBJ sentinel returns with InvalidGrammarObject in all backends
  • Update grammar_manager.py to use InvalidGrammarObject and include error_message in user-facing abort messages
  • Update reasoner_grammar_backend.py to use InvalidGrammarObject

Improved error message format:

  • Replaced verbose debug-style messages (e.g., Invalid grammar request: req.grammar_key=('json', '{"type": "strng"}')) with a clean, user-friendly format: Failed to compile json grammar: JSON schema has unsupported type 'strng'
  • The grammar input is no longer included in the error message since the user already knows what they sent; the value is in the server-side log

llguidance backend improvements:

  • Add _check_err() helper method that validates matcher state and raises descriptive errors
  • Check for errors immediately after matcher creation in __init__()
  • Check for errors after all matcher operations (accept_token, rollback, fill_vocab_mask)
  • Implement rollback() method with proper EOS token handling for speculative decoding
  • Add is_terminated() method

Accuracy Tests

Benchmarking and Profiling

Checklist

Review Process

  1. Ping Merge Oncalls to start the PR flow. See the PR Merge Process.
  2. Get approvals from CODEOWNERS and other reviewers.
  3. Trigger CI tests with comments or contact authorized users to do so.
    • /tag-run-ci-label, /rerun-failed-ci, /tag-and-rerun-ci
  4. After green CI and required approvals, ask Merge Oncalls to merge.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@hnyls2002
Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

Copy link
Copy Markdown
Collaborator

@hnyls2002 hnyls2002 left a comment

Choose a reason for hiding this comment

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

@jellysnack
Copy link
Copy Markdown
Contributor Author

https://github.com/sgl-project/sglang/actions/runs/23017902793/job/66881094094?pr=20467#step:5:85429

I ran test_constrained_decoding_spec_reasoning locally and it passes successfully. Looks like a flaky CI failure maybe?

@hnyls2002
Copy link
Copy Markdown
Collaborator

/rerun-ut test_constrained_decoding_spec_reasoning.py

@github-actions
Copy link
Copy Markdown
Contributor

/rerun-ut is not available for fork PRs (security restriction).

Please ask a maintainer to add the run-ci label and use the normal CI flow, or use /rerun-failed-ci to rerun workflows that have already passed the gate.

@hnyls2002 hnyls2002 merged commit 53f8316 into sgl-project:main Mar 15, 2026
10 of 12 checks passed
JustinTong0323 pushed a commit to JustinTong0323/sglang that referenced this pull request Apr 7, 2026
yhyang201 pushed a commit to yhyang201/sglang that referenced this pull request Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants