vllm.tool_parsers.gemma4_tool_parser ¶
Tool call parser for Google Gemma4 models.
Gemma4 uses a custom serialization format (not JSON) for tool calls::
<|tool_call>call:func_name{key:<|"|>value<|"|>,num:42}<tool_call|>
Strings are delimited by <|"|> (token 52), keys are unquoted, and multiple tool calls are concatenated without separators.
Used when --enable-auto-tool-choice --tool-call-parser gemma4 are set.
For offline inference tool call parsing (direct tokenizer.decode() output), see vllm.tool_parsers.gemma4_utils.parse_tool_calls.
Gemma4ToolParser ¶
Bases: ToolParser
Tool call parser for Google Gemma4 models.
Handles the Gemma4 function call format::
<|tool_call>call:func_name{key:<|"|>value<|"|>}<tool_call|>
Used when --enable-auto-tool-choice --tool-call-parser gemma4 are set.
Streaming strategy: accumulate-then-parse-then-diff
Instead of trying to convert Gemma4's custom format to JSON token-by-token (which fails because Gemma4 uses bare keys, custom delimiters, and structural braces that differ from JSON), this parser:
- Accumulates the raw Gemma4 argument string during streaming
- Parses it with
_parse_gemma4_args()into a Python dict - Converts to JSON with
json.dumps() - Diffs against the previously-streamed JSON string
- Emits only the new JSON fragment as the delta
This follows the same pattern used by FunctionGemma, Hermes, and Llama tool parsers.
Source code in vllm/tool_parsers/gemma4_tool_parser.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 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 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 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 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 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 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 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 | |
_buffer_delta_text ¶
Buffer incoming delta text to handle multi-token special sequences.
Accumulates partial tokens that could be the start of <|tool_call> or <tool_call|> and only flushes them when the complete sequence is recognized or the sequence breaks.
This prevents partial special tokens (e.g., <|tool) from being emitted prematurely as content text.
Source code in vllm/tool_parsers/gemma4_tool_parser.py
_emit_argument_diff ¶
_emit_argument_diff(
raw_args_str: str,
) -> DeltaMessage | None
Parse raw Gemma4 arguments, convert to JSON, diff, and emit.
This is the core of the accumulate-then-parse-then-diff strategy: 1. Parse raw_args_str with _parse_gemma4_args() 2. Convert to JSON string with json.dumps() 3. Withhold trailing closing characters ("}) that may move as more tokens arrive 4. Diff against previously streamed JSON and emit only new chars
Why withholding is necessary:
Gemma4's custom format produces structurally incomplete JSON during streaming. For example, when <|"|>Paris arrives without a closing delimiter, _parse_gemma4_args treats it as a complete value and produces {"location": "Paris"}. But when , France<|"|> arrives next, the JSON becomes {"location": "Paris, France"}. If we had sent the closing "} from the first parse, the concatenated client output would be {"location": "Paris"}France"}, which is garbage.
The solution: never send trailing closing chars during streaming. They get flushed by _handle_tool_call_end() when the <tool_call|> end marker arrives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_args_str | str | The raw Gemma4 argument text accumulated so far (without the surrounding | required |
Returns:
| Type | Description |
|---|---|
DeltaMessage | None | DeltaMessage with the argument diff, or None if no new content. |
Source code in vllm/tool_parsers/gemma4_tool_parser.py
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 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 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 | |
_extract_partial_call ¶
Extract function name and raw argument string from partial text.
Returns (func_name, raw_args_str) or (None, "") if not parseable yet.
Source code in vllm/tool_parsers/gemma4_tool_parser.py
_extract_streaming ¶
Tag-counting streaming parser.
Uses the proven approach from FunctionGemma/Hermes: count start/end tags in previous vs current text to determine phase, then accumulate-parse-diff for arguments.
Format: <|tool_call>call:name{args}<tool_call|>
Source code in vllm/tool_parsers/gemma4_tool_parser.py
_handle_tool_call_end ¶
_handle_tool_call_end(
current_text: str,
) -> DeltaMessage | None
Handle streaming when a tool call has just completed.
Performs a final parse of the complete tool call and flushes any remaining un-streamed argument fragments.
Source code in vllm/tool_parsers/gemma4_tool_parser.py
_handle_tool_call_middle ¶
_handle_tool_call_middle(
current_text: str,
) -> DeltaMessage | None
Handle streaming when we're inside an active tool call.
Accumulates the raw Gemma4 arguments, parses them into JSON, and diffs against the previously-streamed JSON to emit only the new fragment.
Source code in vllm/tool_parsers/gemma4_tool_parser.py
_reset_streaming_state ¶
Reset all streaming state for a new request.
Source code in vllm/tool_parsers/gemma4_tool_parser.py
_parse_gemma4_args ¶
Parse Gemma4's custom key:value format into a Python dict.
Format examples::
location:<|"|>Tokyo<|"|>
location:<|"|>San Francisco<|"|>,unit:<|"|>celsius<|"|>
count:42,flag:true
nested:{inner_key:<|"|>val<|"|>}
items:[<|"|>a<|"|>,<|"|>b<|"|>]
Returns a dict ready for json.dumps().
Source code in vllm/tool_parsers/gemma4_tool_parser.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
_parse_gemma4_array ¶
Parse a Gemma4 array content string into a Python list.
Source code in vllm/tool_parsers/gemma4_tool_parser.py
_parse_gemma4_value ¶
Parse a single Gemma4 value (after key:) into a Python object.