easydel.inference.tools.parsers.pythonic_tool_parser#
- class easydel.inference.tools.parsers.pythonic_tool_parser.PythonicToolParser(tokenizer: PreTrainedTokenizerBase)[source]#
Bases:
ToolParserTool call parser for models using Python-style function call syntax.
Designed for models like Llama 3.2 and Llama 4 that generate tool calls as Python function invocations within list literals, e.g.: [function1(param1=”value1”, param2=”value2”), function2(…)]
Features: - Parses Python AST to extract function calls - Supports nested data structures in arguments - Handles streaming with bracket/quote tracking - Validates Python syntax during extraction - Supports multiple tool calls in single list
Used when –enable-auto-tool-choice –tool-call-parser pythonic are set.
Note
Tool arguments must be valid Python literals (strings, numbers, lists, dicts). Complex Python expressions are not supported.
- TOOL_CALL_REGEX = re.compile('\\[([a-zA-Z]+\\w*\\(([a-zA-Z]+\\w*=.*,\\s*)*([a-zA-Z]+\\w*=.*\\s)?\\),\\s*)*([a-zA-Z]+\\w*\\(([a-zA-Z]+\\w*=.*,\\s*)*([a-zA-Z]+\\w*=.*\\s*)?\\)\\s*)+\\]', re.DOTALL)#
- property current_tool_index: int#
- extract_tool_calls(model_output: str, request: ChatCompletionRequest) ExtractedToolCallInformation[source]#
Extract the tool calls from a complete model response.
- extract_tool_calls_streaming(previous_text: str, current_text: str, delta_text: str, previous_token_ids: Sequence[int], current_token_ids: Sequence[int], delta_token_ids: Sequence[int], request: ChatCompletionRequest) easydel.inference.openai_api_modules.DeltaMessage | None[source]#
Extract tool calls from streaming model output.
Processes incremental model output to identify partial tool calls and emit appropriate streaming updates. Maintains state across calls to handle incomplete JSON/XML structures.
- Parameters
previous_text – Text accumulated up to previous call
current_text – Text accumulated including current chunk
delta_text – New text in current chunk
previous_token_ids – Token IDs up to previous call
current_token_ids – Token IDs including current chunk
delta_token_ids – New token IDs in current chunk
request – Original request with tool definitions
- Returns
Incremental tool call update, or None if no update
- Return type
- Raises
NotImplementedError – Must be implemented by subclasses
Note
This method is stateful - it uses instance variables to track parsing progress across streaming chunks.