easydel.inference.tools.parsers.llama4_pythonic_tool_parser#

class easydel.inference.tools.parsers.llama4_pythonic_tool_parser.Llama4PythonicToolParser(tokenizer: PreTrainedTokenizerBase)[source]#

Bases: ToolParser

Tool call parser for Llama 4 models with Pythonic syntax.

Specialized version of the pythonic parser for Llama 4 models that generate tool calls as Python function invocations. Supports optional <|python_start|> and <|python_end|> delimiters.

Features: - Python AST-based parsing - Optional delimiter token support - Streaming with bracket completion - Regex timeout handling for complex patterns - Support for nested data structures

Format: <|python_start|>[func1(arg1=”val1”), func2(arg2=”val2”)]<|python_end|> or simply: [func1(arg1=”val1”), func2(arg2=”val2”)]

Use –enable-auto-tool-choice –tool-call-parser llama4_pythonic

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

DeltaMessage

Raises

NotImplementedError – Must be implemented by subclasses

Note

This method is stateful - it uses instance variables to track parsing progress across streaming chunks.