[Bugfix][Gemma4] Pre-initialise streaming reasoning state when prompt ends inside an open <|channel> (fixes #45834) (#45852)
Signed-off-by: nikhilesh-csa <nchhetri@csa1.com>
This commit is contained in:
@@ -353,6 +353,14 @@ def gemma4_config() -> ParserEngineConfig:
|
||||
ParserState.REASONING,
|
||||
(EventType.REASONING_START,),
|
||||
),
|
||||
# No-op: if we pre-initialised the engine to REASONING from the
|
||||
# prompt (see ``adjust_initial_state_from_prompt``) but the model
|
||||
# still emits its own ``<|channel>`` opener, swallow it instead
|
||||
# of leaking it as TEXT_CHUNK.
|
||||
(ParserState.REASONING, "THINK_START"): Transition(
|
||||
ParserState.REASONING,
|
||||
(),
|
||||
),
|
||||
(ParserState.REASONING, "THINK_END"): Transition(
|
||||
ParserState.CONTENT,
|
||||
(EventType.REASONING_END,),
|
||||
@@ -524,6 +532,26 @@ class Gemma4Parser(ParserEngine):
|
||||
return True
|
||||
return True
|
||||
|
||||
def adjust_initial_state_from_prompt(self, prompt_token_ids: Sequence[int]) -> None:
|
||||
"""Pre-initialise the engine to ``REASONING`` when the prompt does
|
||||
not already end with reasoning concluded.
|
||||
|
||||
This covers the post-tool-response continuation case where the chat
|
||||
template leaves the prompt ending inside an open ``<|channel>``
|
||||
block (issue #45834). It is also safe in the common new-turn case
|
||||
where the model itself emits ``<|channel>`` first: the no-op
|
||||
``(REASONING, THINK_START)`` transition swallows it, and the
|
||||
``thought\n`` prefix in the first reasoning chunk is stripped by
|
||||
``_events_to_delta`` as it already is in the default flow.
|
||||
"""
|
||||
if self.is_reasoning_end(list(prompt_token_ids)):
|
||||
return
|
||||
self._engine.reset(initial_state=ParserState.REASONING)
|
||||
# Prevent a later default ``initialize_streaming()`` (e.g. from
|
||||
# ``ParserEngineReasoningAdapter.extract_reasoning_streaming``) from
|
||||
# clobbering this with ``CONTENT``.
|
||||
self._streaming_initialized = True
|
||||
|
||||
def _events_to_delta(
|
||||
self,
|
||||
events: list[SemanticEvent],
|
||||
|
||||
Reference in New Issue
Block a user