[Bugfix][Parser] Stream Gemma4 string tool arguments incrementally (#61494)

Signed-off-by: Ben Browning <56071+bbrowning@users.noreply.github.com>
Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
Ben Browning
2026-10-14 21:25:41 +00:00
committed by GitHub
co-authored by Flora Feng
parent 984c242ede
commit 4c5ea86f53
5 changed files with 369 additions and 22 deletions
+20 -3
View File
@@ -94,9 +94,17 @@ class ParserEngineConfig:
arg_structural_chars: frozenset[str] | None = None
# Called with the raw argument text when its converted JSON ends in an
# open value; returns an extender for that value, or None. Streaming
# skips ``arg_converter`` for every delta the extender accepts.
# Called with the raw argument text after each streaming conversion whose
# prefix-safe JSON extends the JSON streamed so far; returns an extender
# when the text ends in an open value that is the last value in the
# converted JSON, else None. While live, the extender receives every delta
# in place of ``arg_converter`` until it returns None, meaning the delta
# closed the value. Returned text is streamed as is, so it must match what
# ``arg_converter`` would serialise for those raw characters. Deltas are
# lexer output: a terminal is never split across deltas, but the lexer
# flushes a held partial terminal as text at the end of the stream or
# before a terminal matched by token ID, so a truncated value may end in
# a terminal prefix. Requires ``arg_converter`` and ``stream_arg_deltas``.
arg_value_extender: Callable[[str], ArgValueExtender | None] | None = None
# Special tokens exempt from auto-drop but not state-machine terminals.
@@ -118,6 +126,15 @@ class ParserEngineConfig:
# Reject tool calls whose names are absent from the request tools.
validate_tool_names: bool = False
def __post_init__(self) -> None:
if self.arg_value_extender is not None and (
self.arg_converter is None or not self.stream_arg_deltas
):
raise ValueError(
f"{self.name}: arg_value_extender requires arg_converter "
"and stream_arg_deltas=True"
)
def terminal_literal(self, name: str) -> str | None:
"""Canonical spelling of terminal *name*, or ``None`` if undeclared."""
value = self.terminals.get(name)