vllm.tool_parsers.utils ¶
UnexpectedAstError ¶
_ast_callable_dotted_name ¶
_ast_callable_dotted_name(node: expr) -> str
Return the dotted name for a call target, walking ast.Attribute chains so a.b.c(...) becomes "a.b.c".
Raises:
| Type | Description |
|---|---|
UnexpectedAstError | If the chain does not bottom out in an |
Source code in vllm/tool_parsers/utils.py
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
find_tool_properties ¶
Find a tool by name and return its properties dict, or {}.
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.
Accepts both bare names (foo(...)) and dotted attribute chains (a.b.c(...)); the resulting tool call name field preserves the dotted form.
Raises:
| Type | Description |
|---|---|
UnexpectedAstError | If the call target is neither a simple name nor a chain of attribute accesses bottoming out in a name. |
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. |
Source code in vllm/tool_parsers/utils.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | |
partial_tag_overlap ¶
Length of the longest prefix of tag that matches a suffix of text.
E.g. text ending in "<tool_" returns 6 when tag is "<tool_call>". Returns 0 when there is no overlap.