Skip to main content

Python class

ToolParser

ToolParser

class max.interfaces.ToolParser(*args, **kwargs)

source

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)

source

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:

ParsedToolResponse

parse_delta()

parse_delta(delta)

source

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 list of tool call deltas if any can be extracted, or None if more tokens are needed.

Return type:

list[ParsedToolCallDelta] | None

reset()

reset()

source

Resets internal state for a new streaming session.

Return type:

None