Expand description
Grammar-constrained decoding for token-by-token generation.
This module provides the TokenConstraint trait and concrete implementations
that restrict which tokens the model can emit at each decoding step:
NoConstraint— passthrough, all tokens allowedRegexConstraint— restricts output to strings matching a regex patternJsonConstraint— restricts output to syntactically valid JSONAllowListConstraint— restricts output to one of a finite set of token sequencesSequenceConstraint— forces output to reproduce a specific token sequenceLengthConstraint— enforces hard minimum and maximum generation lengths
The ConstrainedSampler wraps a crate::sampling_advanced::SamplerChain and
applies a mask to logits before sampling so that only valid continuations are drawn.
§Example
use oxibonsai_runtime::constrained_decoding::{ConstrainedSamplerBuilder, TokenConstraint};
let mut sampler = ConstrainedSamplerBuilder::new(128, 42)
.with_json_constraint();
assert!(!sampler.is_complete());§Module structure
Phase 30B split the monolithic constrained_decoding.rs (1966 lines) into
focused sub-modules; all external crate::constrained_decoding::* access
paths are preserved through the re-exports below.
- [
error_trait][] —ConstraintError, theTokenConstrainttrait, and the passthroughNoConstraint. - [
regex][] — NFA-basedRegexConstraint. - [
json][] — JSON-grammarJsonConstraintand itsJsonParseState. - [
sampler][] —ConstrainedSamplerandConstrainedSamplerBuilder. - [
allow_list][] —AllowListConstraint. - [
sequence][] —SequenceConstraint. - [
length][] —LengthConstraint.
Structs§
- Allow
List Constraint - A constraint that forces the generated token sequence to exactly match one of a finite set of allowed token-id sequences (e.g., multiple-choice answers).
- Constrained
Sampler - Wraps a
crate::sampling_advanced::SamplerChainwith aTokenConstraint. - Constrained
Sampler Builder - Ergonomic builder for
ConstrainedSampler. - Json
Constraint - Constrains generation to syntactically valid JSON.
- Length
Constraint - A constraint that enforces hard minimum and maximum token-count limits.
- NoConstraint
- A passthrough constraint that places no restriction on the vocabulary.
- Regex
Constraint - Constrains generation to strings that match a regular expression.
- Sequence
Constraint - A constraint that forces the generated output to reproduce a specific, pre-determined token sequence.
Enums§
- Constraint
Error - Errors that can arise when building or running a token constraint.
- Json
Parse State - Internal parser state for
JsonConstraint.
Traits§
- Token
Constraint - A constraint that restricts which tokens are valid at each decoding step.