IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).

Python class

GrammarMatcher

GrammarMatcher

class max.pipelines.context.GrammarMatcher(*args, **kwargs)

source

Bases: Protocol

Per-request grammar matcher stepped each decode step.

Backend-agnostic interface; method names mirror llguidance’s LLMatcher so a context can hold any backend’s matcher (llguidance, xgrammar) without branching. The llguidance LLMatcher satisfies this protocol natively.

deep_copy()

deep_copy()

source

Independent copy for speculative walks (never mutates the original).

Return type:

GrammarMatcher

get_error()

get_error()

source

Error message for the last rejection, if any (diagnostics).

Return type:

str | None

get_grammar_warnings()

get_grammar_warnings()

source

Grammar compilation warnings, if any (diagnostics).

Return type:

Any

is_accepting()

is_accepting()

source

Whether the matcher is at an accepting (stoppable) state.

Return type:

bool

is_stopped()

is_stopped()

source

Whether the matcher has reached a terminal state.

Return type:

bool

try_consume_tokens()

try_consume_tokens(tokens)

source

Advance the matcher; returns the number of tokens consumed.

Parameters:

tokens (list[int])

Return type:

int