Skip to main content

JsonStructure

Struct JsonStructure 

Source
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>

Source

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.

Source

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.

Trait Implementations§

Source§

impl<V: TokenVocab> TokenConstraint for JsonStructure<V>

Source§

fn allowed(&self, state: &DecodeState<'_>) -> AllowedSet

The set of token ids that keep the output valid if emitted next.

Auto Trait Implementations§

§

impl<V> Freeze for JsonStructure<V>
where V: Freeze,

§

impl<V> RefUnwindSafe for JsonStructure<V>
where V: RefUnwindSafe,

§

impl<V> Send for JsonStructure<V>
where V: Send,

§

impl<V> Sync for JsonStructure<V>
where V: Sync,

§

impl<V> Unpin for JsonStructure<V>
where V: Unpin,

§

impl<V> UnsafeUnpin for JsonStructure<V>
where V: UnsafeUnpin,

§

impl<V> UnwindSafe for JsonStructure<V>
where V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.