pymllm.parsers.tool_call_parser¶
Tool-call (function-calling) output parser.
Extracts structured tool calls from model output text. Supports both one-shot and incremental streaming modes.
Formats supported:
qwen25 —
<tool_call>{"name":...,"arguments":...}</tool_call>llama3 —
<|python_tag|>{"name":...,"parameters":...}hermes —
<tool_call>{"name":...,"arguments":...}</tool_call>(same tags, Hermes schema)
Usage:
# Non-streaming
parser = ToolCallParser("qwen25", tools=tools_list)
content, tool_calls = parser.parse_non_stream(full_text)
# Streaming
parser = ToolCallParser("qwen25", tools=tools_list)
for delta in deltas:
content_delta, tool_call_deltas = parser.parse_stream_chunk(delta)
Classes¶
A single parsed tool call. |
|
Model-agnostic tool-call parser. |
Module Contents¶
- class pymllm.parsers.tool_call_parser.ToolCallItem¶
A single parsed tool call.
- name: str | None = None¶
- arguments: str = ''¶
- tool_call_id: str = ''¶
- index: int = 0¶
- to_openai_dict(streaming=True)¶
Convert to OpenAI
tool_calls[]element format.- Parameters:
streaming (bool) – If True, include
index(streaming delta format). If False, omitindex(non-streaming message format).- Return type:
Dict[str, Any]
- class pymllm.parsers.tool_call_parser.ToolCallParser(model_type, tools=None)¶
Model-agnostic tool-call parser.
- Parameters:
model_type (str) – Key into the format registry (e.g.
"qwen25","llama3").tools (Optional[List[Any]]) – The
toolslist from the OpenAI chat request (used to resolve function names).
- SUPPORTED¶
- has_tool_call(text)¶
Return True if text contains a tool-call pattern.
- Parameters:
text (str)
- Return type:
bool
- parse_non_stream(text)¶
Parse complete text.
Returns
(remaining_content, tool_calls).- Parameters:
text (str)
- Return type:
Tuple[str, List[ToolCallItem]]
- parse_stream_chunk(delta)¶
Parse an incremental streaming delta.
Returns
(content_delta, tool_call_items).For tool call items: - First item for a call:
nameis set,argumentsis"". - Subsequent items:nameisNone,argumentsis the newcharacters appended (argument delta).
- Parameters:
delta (str)
- Return type:
Tuple[str, List[ToolCallItem]]
- flush()¶
Flush any remaining buffered tool call (call at request end).
- Return type:
List[ToolCallItem]