pub struct JsonStructure<V: TokenVocab> { /* private fields */ }Expand description
A TokenConstraint that keeps the model’s output structurally valid JSON.
See [JsonMachine] for exactly which subset is enforced. At each step
JsonStructure::allowed replays the already-generated bytes to find the
current grammar position, then tests every candidate token: a token is
allowed iff feeding its bytes from the current position never hits an
illegal transition.
§always_allowed — the escape hatch you almost always need
A JSON document, once complete, structurally permits only whitespace
next. If the model can never emit EOS, it can never stop. So control ids
(EOS above all) should be registered via
JsonStructure::with_always_allowed: they bypass the structural check and
are always permitted, which is also what stops the mask from going empty at
Done and raising ConstraintError::NoTokenAllowed.
§Cost
allowed is O(vocab_size × token_len) per step (it probes every
candidate token). Fine for the scaffold and for the small vocabularies the
tool-loop actually constrains; a later bead can make the machine
incremental (advance by the one chosen token instead of re-probing the
whole vocab) if a large-vocab JSON constraint ever needs it. The pure
allowed(&self, state) shape is kept deliberately so the answer stays a
function of state (reproducible), incremental caching being an internal
optimisation that must not change it.
Implementations§
Source§impl<V: TokenVocab> JsonStructure<V>
impl<V: TokenVocab> JsonStructure<V>
Sourcepub fn new(vocab: V) -> Self
pub fn new(vocab: V) -> Self
A JSON-structural constraint over vocab, with no always-allowed
control ids. Prefer JsonStructure::with_always_allowed so the model
has a way to stop.
Sourcepub fn with_always_allowed(vocab: V, ids: impl IntoIterator<Item = u32>) -> Self
pub fn with_always_allowed(vocab: V, ids: impl IntoIterator<Item = u32>) -> Self
As JsonStructure::new, but ids (typically just EOS) always pass
the mask regardless of grammar position. See the type’s docs for why
this is almost always needed.