JavaScript / TypeScript API

On this page

Vocab

Each token ID and its bytes.

class Vocab
constructor(entries: Iterable<readonly [number, Uint8Array<ArrayBuffer>]>)

Hugging Face tokenizers don’t expose token bytes (#2719); converting them is outside GLRMask’s scope.

clone(): Vocab
dispose(): void

Frees the native handle now instead of at garbage collection.

[Symbol.dispose](): void

Grammar

class Grammar
static fromGlrm(source: string): Grammar

Parses GLRM source.

static fromLark(source: string): Grammar

Parses Lark source.

static fromJsonSchema(schema: string | boolean | object): Grammar

Parses a JSON Schema, given as JSON text, a boolean or an object.

static fromExpr(expr: Expr): Grammar

Builds a grammar from an Expr.

readonly externs: Externs

All extern declarations.

compile(vocab: Vocab, options?: CompileOptions): Promise<Constraint>

Compiles the grammar into a constraint.

clone(): Grammar

Shares the immutable grammar data.

dispose(): void

Frees the native handle now instead of at garbage collection.

[Symbol.dispose](): void

Constraint

class Constraint
start(): ConstraintState

Starts a new sequence.

readonly maskBitLength: number

The mask’s length in bits (max token ID plus one).

save(): Promise<Uint8Array<ArrayBuffer>>

Serializes the compiled constraint for later loading.

static load(data: Uint8Array, vocab: Vocab): Promise<Constraint>

May fail if saved by a different GLRMask version. Fails if vocab differs from the one it was compiled with.

clone(): Constraint
dispose(): void

Frees the native handle now instead of at garbage collection.

[Symbol.dispose](): void

ConstraintState

class ConstraintState
mask(): Uint32Array

Packed uint32 words: token i is bit i % 32 of word Math.floor(i / 32).

fillMask(output: Uint32Array): void

Like mask(), but writes the words into output. Throws GLRMaskError with code GLRMASK_BUFFER_TOO_SMALL if output has fewer than Math.ceil(maskBitLength / 32) elements.

readonly maskBitLength: number

The mask’s length in bits (max token ID plus one).

commitToken(id: number): boolean

Advances the state by the token. If it isn’t allowed, returns false and terminates the state.

commitBytes(bytes: Uint8Array): boolean

Advances the state by the bytes. If they aren’t allowed, returns false and terminates the state.

forcedBytes(): Uint8Array

The bytes that must come next.

isAccepting(): boolean

Whether the output so far could end here.

isTerminated(): boolean

Whether nothing more can be committed.

clone(): ConstraintState

Creates an independent state at the same position.

dispose(): void

Frees the native handle now instead of at garbage collection.

[Symbol.dispose](): void

Expr

A grammar expression.

class Expr
static choice(...parts: ExprPart[]): Expr

Matches any one of parts.

static sequence(...parts: ExprPart[]): Expr

Matches parts in order.

static literal(value: string | Uint8Array<ArrayBuffer>): Expr

Matches value exactly.

clone(): Expr
dispose(): void

Frees the native handle now instead of at garbage collection.

[Symbol.dispose](): void

TokenSet

A set of token IDs.

class TokenSet
constructor(ids: Iterable<number>)
static fromRanges(ranges: Iterable<TokenRange>): TokenSet
equals(other: unknown): boolean
toString(): string

Returns a string representation of an object.

clone(): TokenSet
dispose(): void

Frees the native handle now instead of at garbage collection.

[Symbol.dispose](): void

Extern

A named placeholder in a grammar.

interface Extern
readonly name: string
readonly kind: "token" | "grammar"

Externs

A read-only mapping of extern names to Extern.

interface Externs
readonly unbound: Externs

Only externs that still need bindings.

MaskStrategy

type MaskStrategy = typeof MaskStrategy[keyof typeof MaskStrategy]

MaskStrategy (value)

Controls how the constraint is compiled.

const MaskStrategy: Readonly<{ Auto: "auto"; FastCompile: "fast-compile"; FastMasks: "fast-masks" }>
Auto: "auto"

The default; currently FastMasks.

FastCompile: "fast-compile"

Faster compilation, slower masks.

FastMasks: "fast-masks"

Faster masks, slower compilation.

CompileOptions

Options for compiling a grammar or module.

interface CompileOptions
maskStrategy?: MaskStrategy

Defaults to MaskStrategy.Auto.

GLRMaskError

class GLRMaskError
readonly code: ErrorCode

The category of this error.

constructor(code: ErrorCode, message: string, options?: ErrorOptions)

Module

A partially compiled grammar.

class Module
constructor(grammar: Grammar)

Starts with the grammar and its unbound externs.

readonly externs: Externs

All extern declarations, including bound externs.

bind(name: string, value: ExprPart): void

Binds an extern by name.

precompile(vocab: Vocab, options?: CompileOptions): Promise<void>

Compiles what it can now, even with externs unbound. Further compilation must use the same vocab.

compile(vocab: Vocab, options?: CompileOptions): Promise<Constraint>

Finishes compiling the module into a constraint.

save(): Promise<Uint8Array<ArrayBuffer>>

Serializes the module, including any precompiled work.

static load(data: Uint8Array): Promise<Module>

May fail if saved by a different GLRMask version.

clone(): Module

Creates a snapshot with independent bindings.

dispose(): void

Frees the native handle now instead of at garbage collection.

[Symbol.dispose](): void

ErrorCode

type ErrorCode = "GLRMASK_SYNTAX" | "GLRMASK_INVALID_INPUT" | "GLRMASK_COMPILATION" | "GLRMASK_RESOURCE_LIMIT" | "GLRMASK_STRATEGY_UNSUPPORTED" | "GLRMASK_SERIALIZATION" | "GLRMASK_INTERNAL" | "GLRMASK_BUFFER_TOO_SMALL" | "GLRMASK_CLOSED" | "GLRMASK_VOCAB_MISMATCH"

ExprPart

A value that can be part of an Expr.

type ExprPart = Grammar | Module | Constraint | Expr | TokenSet

TokenRange

A range of token IDs from start up to, but not including, end.

interface TokenRange
readonly start: number
readonly end: number

The ID just past the range.

version

const version: string

Documentation source