IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /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. /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 that any grammar backend’s matcher must satisfy. The methods are: try_consume_tokens to advance the matcher, is_accepting / is_stopped to query terminal state, and deep_copy for speculative walks.

deep_copy()​

deep_copy()

source

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

Return type:

GrammarMatcher

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