Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions tests/tool_parsers/test_deepseekv32_tool_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,45 @@ def test_emits_arguments_before_invoke_completes(self, parser):
assert combined # some partial args emitted
assert combined.startswith('{"k"')

def test_long_string_argument_streams_before_close(self):
"""A string body has no ">" and must not be buffered until
</parameter> arrives (GitHub #52846)."""
tool = ChatCompletionToolsParam(
function=FunctionDefinition(
name="emit",
parameters={
"type": "object",
"properties": {"text": {"type": "string"}},
},
),
)
parser = make_parser(tools=[tool])
body = "A" * 4096
chunks = [
f'{FC_START}\n{INV_START}emit">\n{PARAM_START}text" string="true">',
*(body[i : i + 32] for i in range(0, len(body), 32)),
f"{PARAM_END}\n{INV_END}\n{FC_END}",
]
per_chunk_args = []
prev = ""
for chunk in chunks:
result = parser.extract_tool_calls_streaming(
previous_text=prev,
current_text=prev + chunk,
delta_text=chunk,
previous_token_ids=[],
current_token_ids=[],
delta_token_ids=[1],
request=make_request(),
)
prev += chunk
per_chunk_args.append(
self._reconstruct_args([result]) if result is not None else ""
)

assert sum(1 for args in per_chunk_args[1:-1] if args) > 1
assert json.loads("".join(per_chunk_args)) == {"text": body}

def test_no_marker_leak_chunked(self, parser):
"""Chunked streaming must NOT leak DSML start-marker fragments
as content (GitHub #40801)."""
Expand Down
65 changes: 65 additions & 0 deletions tests/tool_parsers/test_deepseekv4_tool_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,71 @@ def test_streaming_emits_incremental_argument_chunks():
}


def _stream_param_body(properties: dict, param_open: str, body: str) -> list[str]:
"""Stream one parameter body in 32-char chunks, one argument list per chunk."""
tool = ChatCompletionToolsParam(
function=FunctionDefinition(
name="emit",
parameters={"type": "object", "properties": properties},
),
)
parser = make_parser(tools=[tool])
chunks = [
f'{TC_START}\n{INV_START}emit">\n{param_open}',
*(body[i : i + 32] for i in range(0, len(body), 32)),
f"{PARAM_END}\n{INV_END}\n{TC_END}",
]
per_chunk_args = []
previous_text = ""
for delta_text in chunks:
current_text = previous_text + delta_text
delta = parser.extract_tool_calls_streaming(
previous_text=previous_text,
current_text=current_text,
delta_text=delta_text,
previous_token_ids=[],
current_token_ids=[],
delta_token_ids=[1],
request=make_request(),
)
previous_text = current_text
per_chunk_args.append(
"".join(
tool_call.function.arguments
for tool_call in (delta.tool_calls if delta else [])
if tool_call.function and tool_call.function.arguments
)
)
return per_chunk_args


def test_streaming_long_string_argument_is_incremental():
# Regression for #52846: a string body has no ">" so it used to be
# buffered until </parameter> arrived.
body = "A" * 4096
per_chunk_args = _stream_param_body(
{"text": {"type": "string"}},
f'{PARAM_START}text" string="true">',
body,
)

body_chunks = per_chunk_args[1:-1]
assert sum(1 for args in body_chunks if args) > 1
assert json.loads("".join(per_chunk_args)) == {"text": body}


def test_streaming_non_string_argument_is_withheld_until_close():
body = "1" * 128
per_chunk_args = _stream_param_body(
{"count": {"type": "integer"}},
f'{PARAM_START}count" string="false">',
body,
)

assert not any(per_chunk_args[1:-1])
assert json.loads("".join(per_chunk_args)) == {"count": int(body)}


def _with_strict(
tools: list[ChatCompletionToolsParam],
) -> list[ChatCompletionToolsParam]:
Expand Down
4 changes: 3 additions & 1 deletion vllm/parser/deepseek_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ def _param_patterns(

The ``string`` attribute is optional: the model sometimes omits it, and
dropping such a parameter hands the client a tool call with no arguments.
The trailing-partial regex holds back an unfinished next parameter tag
so a streamed string value never includes it.
"""
start, close = re.escape(param_start), re.escape(param_close)
head = rf'{start}\s+name="([^"]+)"(?:\s+string="([^"]*)")?\s*>'
return (
re.compile(rf"{head}(.*?)(?:{close}|(?={start}\s+name=))", re.DOTALL),
re.compile(rf"{head}(.*)$", re.DOTALL),
re.compile(rf"{head}(.*?)(?:{start}[^>]*)?$", re.DOTALL),
Comment thread
bbrowning marked this conversation as resolved.
Resolved
)


Expand Down
12 changes: 10 additions & 2 deletions vllm/parser/engine/parser_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,11 +1014,19 @@ def _compute_arg_delta(self, idx: int, raw_delta: str) -> str | None:
if not self._stream_arg_deltas:
return None

slot = self._tool_slots[idx]
# Structural chars gate the converter call, but a string value can
# grow without them. Only gate when ``string_keys`` is ``set()`` (the
# schema has no string fields); ``None`` means no schema, so every
# string value is streamable.
structural = self._arg_structural_chars
if structural is not None and structural.isdisjoint(raw_delta):
if (
structural is not None
and slot.string_keys == set()
and structural.isdisjoint(raw_delta)
):
return None

slot = self._tool_slots[idx]
try:
current_json = converter(slot.args, True)
except (json.JSONDecodeError, ValueError, TypeError):
Expand Down
Loading