vllm.tool_parsers.utils ¶
UnexpectedAstError ¶
compute_tool_delta ¶
compute_tool_delta(
previously_sent_args: str,
new_call: ToolCall,
index: int,
withheld_suffix: str,
) -> DeltaToolCall | None
Compute the incremental delta between previously streamed arguments and the current tool call state.
Returns:
| Type | Description |
|---|---|
DeltaToolCall | None | A DeltaToolCall with only the new argument characters, or None |
DeltaToolCall | None | if there is no difference from what was previously sent. |
Source code in vllm/tool_parsers/utils.py
extract_intermediate_diff ¶
Given two strings, extract the difference in the middle between two strings that are known to have a common prefix and/or suffix.
This function is provided as a UTILITY for extracting information from JSON generated by partial_json_parser, to help in ensuring that the right tokens are returned in streaming, so that close-quotes, close-brackets and close-braces are not returned prematurely. The order of arguments IS important - the new version of the partially-parsed JSON must be the first argument, and the secnod argument must be from the previous generation.
What it returns, is tokens that should be streamed to the client.
e.g. extract_intermediate_diff('{"fruit": "apple"}', '{"fruit": "ap"}') -> 'ple'
Source code in vllm/tool_parsers/utils.py
find_common_prefix ¶
Finds a common prefix that is shared between two strings, if there is one. Order of arguments is NOT important.
This function is provided as a UTILITY for extracting information from JSON generated by partial_json_parser, to help in ensuring that the right tokens are returned in streaming, so that close-quotes, close-brackets and close-braces are not returned prematurely.
e.g. find_common_prefix('{"fruit": "ap"}', '{"fruit": "apple"}') -> '{"fruit": "ap'
Source code in vllm/tool_parsers/utils.py
find_common_suffix ¶
Finds a common suffix shared between two strings, if there is one. Order of arguments is NOT important. Stops when the suffix ends OR it hits an alphanumeric character
e.g. find_common_suffix('{"fruit": "ap"}', '{"fruit": "apple"}') -> '"}'
Source code in vllm/tool_parsers/utils.py
get_parameter_value ¶
get_parameter_value(val: expr) -> Any
Extract a Python literal value from an AST expression node.
Handles constants, dicts, lists, and JSON-style name literals (null, true, false) that some models produce instead of Python literals (None, True, False).
Raises:
| Type | Description |
|---|---|
UnexpectedAstError | If the AST node is not a supported literal type. |
Source code in vllm/tool_parsers/utils.py
handle_single_tool ¶
handle_single_tool(call: Call) -> ToolCall
Convert a single AST function call node into a ToolCall object.
Raises:
| Type | Description |
|---|---|
UnexpectedAstError | If the call node does not have a simple function name (e.g. it's an attribute access or subscript). |
Source code in vllm/tool_parsers/utils.py
make_valid_python ¶
Attempt to close all open brackets/quotes to make partial Python valid.
Used during streaming to parse incomplete tool call expressions by appending the necessary closing characters.
Returns:
| Type | Description |
|---|---|
tuple[str, str] | None | A tuple of (completed_text, added_suffix) if the text can be |
tuple[str, str] | None | made valid, or None if the text is too incomplete to complete |
tuple[str, str] | None | meaningfully (e.g. mid-parameter-name or mid-dict-key). |
Raises:
| Type | Description |
|---|---|
UnexpectedAstError | If mismatched brackets or parentheses are detected. |