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
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 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 | |
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
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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | |
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.