tool-parser
Parser library for extracting tool/function calls from LLM model outputs. Supports both complete parsing and streaming incremental parsing with state management.
Supported Formats
| Parser | Model Family | Format |
|---|---|---|
CohereParser |
Cohere Command (CMD3/CMD4) | <|START_ACTION|>{...}<|END_ACTION|> |
MistralParser |
Mistral, Mixtral | [TOOL_CALLS][{...}] |
QwenParser |
Qwen 2/2.5/3 | <tool_call>{...}</tool_call> |
QwenCoderParser |
Qwen Coder | <tool_call><function=name>...</function></tool_call> |
LlamaParser |
Llama 3.2 | <|python_tag|>{...} |
PythonicParser |
Llama 4, DeepSeek R1 | [func_name(arg="val")] |
DeepSeekParser |
DeepSeek V3 | <|tool▁calls▁begin|>...<|tool▁calls▁end|> |
Glm4MoeParser |
GLM-4.5/4.6/4.7 | <|observation|>...<|/observation|> |
Step3Parser |
Step-3 | <steptml:function_call>...</steptml:function_call> |
KimiK2Parser |
Kimi K2 | <|tool_call_begin|>...<|tool_call_end|> |
MinimaxM2Parser |
MiniMax M2 | <FUNCTION_CALL>{...}</FUNCTION_CALL> |
JsonParser |
OpenAI, Claude, Gemini | Direct JSON tool calls |
Usage
Complete Parsing
use ;
let parser = new;
let input = r#"<|START_RESPONSE|>Let me search.<|END_RESPONSE|>
<|START_ACTION|>
{"tool_name": "search", "parameters": {"query": "rust"}}
<|END_ACTION|>"#;
let = parser.parse_complete.await?;
assert_eq!;
assert_eq!;
Streaming Parsing
use ;
use Tool;
let mut parser = new;
let tools: = vec!;
for chunk in stream
// Get any remaining unstreamed arguments
if let Some = parser.get_unstreamed_tool_args
// Reset for next request
parser.reset;
Factory Pattern
use ParserFactory;
let factory = new;
// Get parser by model ID (auto-detects format)
let parser = factory.get_pooled;
// Or create a fresh instance for isolated streaming
let parser = factory.registry.create_for_model;
Architecture
tool_parser/
├── src/
│ ├── lib.rs # Public exports
│ ├── traits.rs # ToolParser trait
│ ├── types.rs # ToolCall, StreamingParseResult
│ ├── errors.rs # ParserError
│ ├── factory.rs # ParserFactory, ParserRegistry
│ ├── partial_json.rs # Incomplete JSON handling
│ └── parsers/
│ ├── mod.rs # Parser re-exports
│ ├── helpers.rs # Shared utilities
│ ├── cohere.rs # CohereParser
│ ├── mistral.rs # MistralParser
│ └── ... # Other parsers
└── tests/
└── tool_parser_*.rs # Integration tests per parser
Key Types
/// Core parsing trait
/// Parsed tool call
/// Streaming parse result
Adding a New Parser
- Create
src/parsers/<model>.rsimplementingToolParser - Add to
src/parsers/mod.rsexports - Register in
src/factory.rs:registry.register_parser; registry.map_model; - Add to
src/lib.rspublic exports - Create
tests/tool_parser_<model>.rswith test cases
License
Apache-2.0