pub struct ParserRegistry { /* private fields */ }Expand description
Registry for model-specific tool parsers with pooling support.
Implementations§
Source§impl ParserRegistry
impl ParserRegistry
Sourcepub fn register_parser<F>(&self, name: &str, creator: F)
pub fn register_parser<F>(&self, name: &str, creator: F)
Register a parser without structural tag support.
Sourcepub fn register_parser_with_structural_tag<F>(
&self,
name: &str,
creator: F,
build_structural_tag: fn(&[Tool], bool) -> Value,
)
pub fn register_parser_with_structural_tag<F>( &self, name: &str, creator: F, build_structural_tag: fn(&[Tool], bool) -> Value, )
Register a parser with a structural tag builder.
The build_structural_tag function takes (&[Tool], at_least_one) and returns
the full xgrammar structural tag JSON for this parser’s native tool-call format.
Sourcepub fn register_reasoning_prefix(
&self,
name: &str,
reasoning_prefix: fn(GrammarStart) -> Option<Value>,
)
pub fn register_reasoning_prefix( &self, name: &str, reasoning_prefix: fn(GrammarStart) -> Option<Value>, )
Register the reasoning block for a parser that already has a structural tag builder.
When Self::generate_tool_constraint is called with reasoning
set, the parser’s tag becomes sequence[prefix, tag.format] with the
prefix for GrammarStart::Unknown: free text, then the forced call.
Unlike xgrammar’s built-in tags with reasoning=True, that prefix is
not closed by the thought’s end token: an engine that runs a reasoning
parser applies the grammar only after the model’s own
</think> and would hold the model to a second one. Once the worker
that runs the grammar is known,
Self::structural_tag_for_grammar_start re-shapes the prefix for
where that engine starts the grammar. Parsers without a prefix keep
their unwrapped tag whatever reasoning says.
Sourcepub fn has_reasoning_prefix(&self, configured: Option<&str>) -> bool
pub fn has_reasoning_prefix(&self, configured: Option<&str>) -> bool
Whether the configured parser wraps its structural tag in a reasoning
block when asked (see Self::register_reasoning_prefix). False when
no parser is configured, like Self::has_structural_tag_for_parser.
Sourcepub fn structural_tag_for_grammar_start(
&self,
configured_parser: Option<&str>,
tag: &str,
start: GrammarStart,
) -> Result<Option<String>, String>
pub fn structural_tag_for_grammar_start( &self, configured_parser: Option<&str>, tag: &str, start: GrammarStart, ) -> Result<Option<String>, String>
A forced call’s structural tag, as Self::generate_tool_constraint
built it on a thinking prompt, re-shaped for where the engine that
will run it starts the grammar: for GrammarStart::AfterReasoning
the prefix is dropped (the grammar starts at the engine’s gate, so the
first constrained token opens a call); for GrammarStart::FirstToken
the prefix becomes the parser’s thought block, closed by </think>,
so the call follows the thought. Ok(None) leaves the tag as it is:
the start is unknown, the parser has no prefix, or the tag does not
carry one (a prompt outside the thinking block).
Sourcepub fn map_model(&self, model: impl Into<String>, parser: impl Into<String>)
pub fn map_model(&self, model: impl Into<String>, parser: impl Into<String>)
Map a model name/pattern to a parser
Sourcepub fn get_pooled_parser(&self, name: &str) -> Option<PooledParser>
pub fn get_pooled_parser(&self, name: &str) -> Option<PooledParser>
Get a pooled parser by exact name. Returns a shared parser instance from the pool, creating one if needed.
Sourcepub fn has_parser(&self, name: &str) -> bool
pub fn has_parser(&self, name: &str) -> bool
Check if a parser with the given name is registered.
Sourcepub fn create_parser(&self, name: &str) -> Option<Box<dyn ToolParser>>
pub fn create_parser(&self, name: &str) -> Option<Box<dyn ToolParser>>
Create a fresh (non-pooled) parser instance by exact name. Returns a new parser instance for each call - useful for streaming where state isolation is needed.
Sourcepub fn has_structural_tag(&self, name: &str) -> bool
pub fn has_structural_tag(&self, name: &str) -> bool
Check if the parser supports structural tag constraints.
Sourcepub fn has_structural_tag_for_parser(&self, configured: Option<&str>) -> bool
pub fn has_structural_tag_for_parser(&self, configured: Option<&str>) -> bool
Check if the configured parser supports structural tags.
Returns false if no parser is configured (structural tags require explicit opt-in
via --tool-call-parser).
Sourcepub fn uses_native_tool_format(&self, configured: Option<&str>) -> bool
pub fn uses_native_tool_format(&self, configured: Option<&str>) -> bool
Whether required/named responses retain model-native framing rather than the generic JSON schema fallback. Gemma4 follows vLLM’s native choice handling without claiming structural-tag grammar support.
Sourcepub fn generate_tool_constraint(
&self,
configured_parser: Option<&str>,
tools: &[Tool],
tool_choice: &ToolChoice,
reasoning: bool,
) -> Result<Option<ToolConstraint>, String>
pub fn generate_tool_constraint( &self, configured_parser: Option<&str>, tools: &[Tool], tool_choice: &ToolChoice, reasoning: bool, ) -> Result<Option<ToolConstraint>, String>
Generate tool call constraint.
If configured_parser supports structural tags → StructuralTag(json).
Gemma4 emits its native format without a decoding constraint.
Otherwise → JsonSchema(schema) for required/function tool_choice.
Returns Ok(None) for auto/none tool_choice.
reasoning says the rendered prompt ends inside the model’s thinking
block (the gateway’s chat_reasoning_starts_in_prefill). A parser that
registered a reasoning prefix then gets its tag wrapped so the forced
call follows the reasoning instead of preempting it; every other
constraint is unchanged and applies from the first generated token.
Sourcepub fn resolve_model_to_parser(&self, model: &str) -> Option<String>
pub fn resolve_model_to_parser(&self, model: &str) -> Option<String>
Resolve model name to parser name via model mappings. Returns None if no mapping matches (caller decides fallback).
Sourcepub fn has_parser_for_model(&self, model: &str) -> bool
pub fn has_parser_for_model(&self, model: &str) -> bool
Check if a parser can be created for a specific model without actually creating it.
Sourcepub fn create_for_model(&self, model: &str) -> Option<Box<dyn ToolParser>>
pub fn create_for_model(&self, model: &str) -> Option<Box<dyn ToolParser>>
Create a fresh (non-pooled) parser instance for a specific model.
Sourcepub fn get_pooled_for_model(&self, model: &str) -> Option<PooledParser>
pub fn get_pooled_for_model(&self, model: &str) -> Option<PooledParser>
Get parser for a specific model
Sourcepub fn clear_pool(&self)
pub fn clear_pool(&self)
Clear the parser pool, forcing new instances to be created.
Sourcepub fn set_default_parser(&self, name: impl Into<String>)
pub fn set_default_parser(&self, name: impl Into<String>)
Set the default parser
Trait Implementations§
Source§impl Clone for ParserRegistry
impl Clone for ParserRegistry
Auto Trait Implementations§
impl !RefUnwindSafe for ParserRegistry
impl !UnwindSafe for ParserRegistry
impl Freeze for ParserRegistry
impl Send for ParserRegistry
impl Sync for ParserRegistry
impl Unpin for ParserRegistry
impl UnsafeUnpin for ParserRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more