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
_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.
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\n
(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 the thought\n prefix in the first reasoning
chunk is stripped by _events_to_delta as in the default flow.
_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.
_gemma4_arg_converter(raw_args, partial)
¶
Convert Gemma4 custom arg format to a JSON string.
_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.
_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.
_strip_partial_delim(value)
¶
Strip a trailing partial STRING_DELIM prefix from value.
Prevents partial delimiters from leaking into the streamed JSON diff.