vllm.tool_parsers.utils ¶
Classes:
-
UnexpectedAstError–Raised when the AST structure does not match the expected
Functions:
-
coerce_to_schema_type–Best-effort coercion of a raw string value to a JSON Schema type.
-
compute_tool_delta–Compute the incremental delta between previously streamed arguments
-
extract_intermediate_diff–Given two strings, extract the difference in the middle between two strings
-
extract_types_from_schema–Extract all possible type strings from a JSON Schema definition.
-
find_common_prefix–Finds a common prefix that is shared between two strings, if there is one.
-
find_common_suffix–Finds a common suffix shared between two strings, if there is one. Order of
-
find_tool_name–Return whether a function tool with tool_name exists.
-
find_tool_properties–Find a tool by name and return its properties dict, or {}.
-
get_parameter_value–Extract a Python literal value from an AST expression node.
-
handle_single_tool–Convert a single AST function call node into a ToolCall object.
-
make_valid_python–Attempt to close all open brackets/quotes to make partial Python valid.
-
partial_tag_overlap–Length of the longest prefix of tag that matches a suffix of text.
UnexpectedAstError ¶
_ast_callable_dotted_name(node) ¶
Return the dotted name for a call target, walking ast.Attribute chains so a.b.c(...) becomes "a.b.c".
Raises:
-
UnexpectedAstError–If the chain does not bottom out in an
ast.Name(e.g. subscript or call expression as receiver).
Source code in vllm/tool_parsers/utils.py
_is_json_finite(obj) ¶
Whether obj can be serialized to valid JSON.
json.dumps(..., allow_nan=False) raises ValueError on any non-finite float (inf/-inf/nan) anywhere in the value, so this detects non-finite floats nested inside parsed lists/dicts too.
Source code in vllm/tool_parsers/utils.py
coerce_to_schema_type(value, schema_type) ¶
Best-effort coercion of a raw string value to a JSON Schema type.
Tries each type in priority order (null > integer > number > boolean > object > array > string) and returns the first successful coercion. Falls back to the original string when no coercion succeeds.
Parameters:
-
(value¶str) –The raw string value from the model output.
-
(schema_type¶str | list[str]) –One or more JSON Schema type strings (e.g.
"string"or["string", "null"]).
Source code in vllm/tool_parsers/utils.py
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 | |
compute_tool_delta(previously_sent_args, new_call, index, withheld_suffix) ¶
Compute the incremental delta between previously streamed arguments and the current tool call state.
Returns:
-
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(curr, old) ¶
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
extract_types_from_schema(schema) ¶
Extract all possible type strings from a JSON Schema definition.
Handles type (string or list), enum value inference, and recursive anyOf/oneOf/allOf. Returns ["string"] when no type information can be determined.
Source code in vllm/tool_parsers/utils.py
find_common_prefix(s1, s2) ¶
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(s1, s2) ¶
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_name(tools, tool_name) ¶
Return whether a function tool with tool_name exists.
Source code in vllm/tool_parsers/utils.py
find_tool_properties(tools, tool_name) ¶
Find a tool by name and return its properties dict, or {}.
Source code in vllm/tool_parsers/utils.py
get_parameter_value(val) ¶
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:
-
UnexpectedAstError–If the AST node is not a supported literal type.
Source code in vllm/tool_parsers/utils.py
handle_single_tool(call) ¶
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:
-
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(text) ¶
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:
-
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:
-
UnexpectedAstError–If mismatched brackets or parentheses are detected.
Source code in vllm/tool_parsers/utils.py
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 | |
partial_tag_overlap(text, tag) ¶
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.