[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:
co-authored by
Flora Feng
parent
984c242ede
commit
4c5ea86f53
@@ -342,8 +342,10 @@ class ParserEngine(Parser):
|
||||
their unterminated content instead of buffering long arguments until
|
||||
the closing tag arrives.
|
||||
|
||||
Also returns whether the prefix ends inside that streamed trailing
|
||||
string value.
|
||||
Also returns whether the returned prefix stops inside that trailing
|
||||
string, so streamed text may be appended to it. This holds even when
|
||||
*json_str* closes the string: converters serialise an open raw value
|
||||
as a closed JSON string.
|
||||
"""
|
||||
last_colon = -1
|
||||
last_key: str | None = None
|
||||
@@ -1053,11 +1055,8 @@ class ParserEngine(Parser):
|
||||
if not self._stream_arg_deltas:
|
||||
return None
|
||||
|
||||
structural = self._arg_structural_chars
|
||||
if structural is not None and structural.isdisjoint(raw_delta):
|
||||
return None
|
||||
|
||||
slot = self._tool_slots[idx]
|
||||
structural = self._arg_structural_chars
|
||||
if slot.extend_open_value is not None:
|
||||
diff = slot.extend_open_value(raw_delta)
|
||||
if diff is not None:
|
||||
@@ -1066,6 +1065,8 @@ class ParserEngine(Parser):
|
||||
slot.append_streamed(diff)
|
||||
return diff
|
||||
slot.extend_open_value = None
|
||||
elif structural is not None and structural.isdisjoint(raw_delta):
|
||||
return None
|
||||
|
||||
try:
|
||||
current_json = converter(slot.args, True)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user