Python class
ToolParser
ToolParserβ
class max.pipelines.modeling.types.ToolParser(*args, **kwargs)
Bases: Protocol
Protocol for parsing tool calls from model responses.
Implementations parse model-specific tool calling formats into generic tool call structures. Supports both complete (non-streaming) and incremental (streaming) parsing modes.
Different model architectures use different tool calling formats:
- Llama models use JSON-based tool calls.
- Kimi K2.5 uses structural tags like
<|tool_call_begin|>.
The serving layer is responsible for translating these generic types to API-specific schemas (for example, OpenAI).
parse_complete()β
parse_complete(response)
Parses a complete response into tool calls.
-
Parameters:
-
response (str) β The full model response text.
-
Returns:
-
A ParsedToolResponse containing any text content and parsed tool calls.
-
Raises:
-
ValueError β If the response cannot be parsed.
-
Return type:
parse_delta()β
parse_delta(delta)
Parses an incremental token delta for streaming tool calls.
Accumulates tokens internally and returns tool call deltas when complete or partial tool calls can be extracted.
-
Parameters:
-
delta (str) β The incremental token(s) to process.
-
Returns:
-
- A non-empty list of
ParsedToolCallDeltawhen new content (tool name, id, or argument bytes) is ready to stream. - An empty list
[]when the parser has consumed the token and is inside the tool-calls section but has no deltas to emit yet; the caller must suppress the raw token from flowing as text content. Nonewhen more tokens are needed before anything can be emitted (e.g. buffering a potential section-begin marker).
- A non-empty list of
-
Return type:
-
list[ParsedToolCallDelta] | None
reset()β
reset()
Resets internal state for a new streaming session.
-
Return type:
-
None
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!