vllm.parser.gemma4
¶
Gemma4 parser.
Handles channel-based reasoning plus custom tool call format in a single state machine::
<|channel>thought
...reasoning...<channel|>
<|tool_call>call:func_name{key:<|"|>value<|"|>,num:42}<tool_call|>
Classes:
-
Gemma4Parser–Gemma4 parser:
<|channel>reasoning +<|tool_call>
Gemma4Parser
¶
Bases: ParserEngine
Gemma4 parser: <|channel> reasoning + <|tool_call>
tool calls in a single engine.
- Strips the
thought\nprefix from reasoning content - Sets
skip_special_tokens=Falseso boundary tokens are visible - Detects
<|tool_call>token as implicit reasoning end
Methods:
-
adjust_initial_state_from_prompt–Pre-initialise the engine to
REASONINGwhen the prompt ends
Source code in vllm/parser/gemma4.py
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 | |
_prompt_ends_in_open_reasoning(prompt_token_ids)
¶
Whether the prompt tail is inside an open <|channel> block.
Scans backwards: a <|channel> start token seen before any
closing or turn-boundary token means the block is still open.
Source code in vllm/parser/gemma4.py
adjust_initial_state_from_prompt(prompt_token_ids)
¶
Pre-initialise the engine to REASONING when the prompt ends
inside an open <|channel> block.
This covers the post-tool-response continuation case where the chat
template leaves the prompt ending with ``<|channel>thought
(issue #45834). A prompt that merely starts a new model turn must
not pre-initialise reasoning: the model may answer directly without
emitting any channel markers, and the non-streaming path classifies
such output as content (issue #48217). When the model does open its
own<|channel>, the(CONTENT, THINK_START)transition
handles it, and thethought
prefix in the first reasoning
chunk is stripped by_events_to_delta`` as in the default flow.
Source code in vllm/parser/gemma4.py
_drop_call_closer(text, paren_excess)
¶
Drop a trailing } or, with more ) than (, a trailing ).
The closer of the call body is dropped before parsing, so a value that itself ends in one loses that character until more text arrives.
Source code in vllm/parser/gemma4.py
_gemma4_arg_converter(raw_args, partial)
¶
Convert Gemma4 custom arg format to a JSON string.
Source code in vllm/parser/gemma4.py
_gemma4_arg_value_extender(raw_args)
¶
Extend the open top-level STRING_DELIM value at the end of
raw_args, if it is the last value in the converted JSON.
The extender streams exactly what _gemma4_arg_converter would, so it
holds back the same tail: trailing whitespace, a dropped closer and a
partial STRING_DELIM. STRING_DELIM is a special token but not a
lexer terminal, so unlike a tag it may reach the extender split across
deltas; the held tail doubles as the window that detects the close.
Source code in vllm/parser/gemma4.py
_parse_gemma4_fields(args_str, *, partial=False)
¶
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<|"|>]
Parameters:
-
(args_str¶str) –The raw Gemma4 argument string.
-
(partial¶bool, default:False) –When True (streaming), bare values at end of string are omitted because they may be incomplete and type-unstable (e.g. partial boolean parsed as bare string).
Returns a dict ready for json.dumps() and the key of the trailing
top-level STRING_DELIM value left unterminated, if any.
Source code in vllm/parser/gemma4.py
73 74 75 76 77 78 79 80 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
_strip_partial_delim(value)
¶
Strip a trailing partial STRING_DELIM prefix from value.
Prevents partial delimiters from leaking into the streamed JSON diff.