Skip to main content

ToolParser

Trait ToolParser 

Source
pub trait ToolParser: Send + Sync {
    // Required methods
    fn parse_complete<'life0, 'life1, 'async_trait>(
        &'life0 self,
        output: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = ParserResult<(String, Vec<ToolCall>)>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn parse_incremental<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 mut self,
        chunk: &'life1 str,
        tools: &'life2 [Tool],
    ) -> Pin<Box<dyn Future<Output = ParserResult<StreamingParseResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn has_tool_markers(&self, text: &str) -> bool;

    // Provided methods
    fn parse_complete_with_tools<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        output: &'life1 str,
        _tools: &'life2 [Tool],
    ) -> Pin<Box<dyn Future<Output = ParserResult<(String, Vec<ToolCall>)>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn get_unstreamed_tool_args(&self) -> Option<Vec<ToolCallItem>> { ... }
    fn take_unstreamed_normal_text(&mut self) -> String { ... }
    fn reset(&mut self) { ... }
}
Expand description

Core trait for all tool parsers

Required Methods§

Source

fn parse_complete<'life0, 'life1, 'async_trait>( &'life0 self, output: &'life1 str, ) -> Pin<Box<dyn Future<Output = ParserResult<(String, Vec<ToolCall>)>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Parse complete tool calls from final output Returns (remaining_normal_text, tool_calls) tuple

Source

fn parse_incremental<'life0, 'life1, 'life2, 'async_trait>( &'life0 mut self, chunk: &'life1 str, tools: &'life2 [Tool], ) -> Pin<Box<dyn Future<Output = ParserResult<StreamingParseResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Parse tool calls from model output (streaming) Parsers now maintain internal state, so self is mutable

§Arguments
  • chunk - New text chunk from model output
  • tools - List of available tools for validation
Source

fn has_tool_markers(&self, text: &str) -> bool

Check if text contains tool calls in this parser’s format

Provided Methods§

Source

fn parse_complete_with_tools<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, output: &'life1 str, _tools: &'life2 [Tool], ) -> Pin<Box<dyn Future<Output = ParserResult<(String, Vec<ToolCall>)>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Like Self::parse_complete, but with the request’s tool schemas so schema-aware parsers coerce arg values by declared type. The default ignores tools and delegates to parse_complete.

Source

fn get_unstreamed_tool_args(&self) -> Option<Vec<ToolCallItem>>

Get unstreamed tool call arguments Returns tool call items for arguments that have been parsed but not yet streamed

Source

fn take_unstreamed_normal_text(&mut self) -> String

Take any text still buffered by the streaming parser that never became a tool call, transferring ownership to the caller.

Streaming consumers call this once at end of stream, alongside Self::get_unstreamed_tool_args: text held back as a prospective tool call (a bare { prefix, a partial start marker, or tool JSON that never completed) must be surfaced as normal content instead of being silently dropped — mirroring the non-streaming fallback that returns unparsable tool text verbatim. Parsers that announced a tool call from the buffered text return an empty string (the remaining arguments are recovered via get_unstreamed_tool_args).

The default returns an empty string (parser holds no buffered text).

Source

fn reset(&mut self)

Reset the parser state for reuse across requests. This should clear all buffers and reset state to initial values.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§