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() -> Value,
)
pub fn register_reasoning_prefix( &self, name: &str, reasoning_prefix: fn() -> 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]: the
model reasons, closes its thinking block, and only then emits the
forced call — the layout of xgrammar’s built-in tags with
reasoning=True. 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 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 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).
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