[Bugfix][Parser] Emit REASONING_END for Inkling tool calls that follow no thinking block (#50528)
Signed-off-by: Jason Yao <wsyjh8@gmail.com>
This commit is contained in:
@@ -844,3 +844,61 @@ class TestSkipToolParsing:
|
||||
engine.skip_tool_parsing = True
|
||||
engine.reset()
|
||||
assert engine.skip_tool_parsing is True
|
||||
|
||||
|
||||
def _message_header_config(reasoning_end: bool) -> ParserEngineConfig:
|
||||
"""MESSAGE_HEADER-based grammar whose tool opener optionally carries
|
||||
REASONING_END, mirroring the two shapes a transition table can take."""
|
||||
return ParserEngineConfig(
|
||||
name=f"message_header_test_{reasoning_end}",
|
||||
initial_state=ParserState.MESSAGE_HEADER,
|
||||
terminals={"HEADER_END": "<|header_end|>", "TOOL_START": "<tool_call>"},
|
||||
transitions={
|
||||
(ParserState.MESSAGE_HEADER, "HEADER_END"): Transition(
|
||||
ParserState.CONTENT,
|
||||
(EventType.TEXT_CHUNK,),
|
||||
),
|
||||
(ParserState.MESSAGE_HEADER, "TOOL_START"): Transition(
|
||||
ParserState.TOOL_ARGS,
|
||||
(EventType.REASONING_END, EventType.TOOL_CALL_START)
|
||||
if reasoning_end
|
||||
else (EventType.TOOL_CALL_START,),
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class TestSkipToolParsingFromMessageHeader:
|
||||
"""Leaving MESSAGE_HEADER through the skip_tool_parsing gate must honour
|
||||
the transition's REASONING_END.
|
||||
|
||||
The reasoning pass hands off to the tool pass only once it has seen a
|
||||
REASONING_END, so swallowing it under the header passthrough strands the
|
||||
whole tool block in content. Tables carrying no REASONING_END keep the
|
||||
plain passthrough.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def _skip_events(reasoning_end: bool):
|
||||
engine = StreamingParserEngine(
|
||||
_message_header_config(reasoning_end), tokenizer=None
|
||||
)
|
||||
engine.skip_tool_parsing = True
|
||||
return engine, engine.parse_complete("name<tool_call>{}")
|
||||
|
||||
def test_reasoning_end_precedes_forwarded_tool_syntax(self):
|
||||
engine, events = self._skip_events(reasoning_end=True)
|
||||
assert [e.type for e in events[:2]] == [
|
||||
EventType.REASONING_END,
|
||||
EventType.TEXT_CHUNK,
|
||||
]
|
||||
assert events[1].value == "<tool_call>"
|
||||
assert engine._message_header_buffer == ""
|
||||
|
||||
def test_passthrough_only_when_transition_omits_reasoning_end(self):
|
||||
engine, events = self._skip_events(reasoning_end=False)
|
||||
types = [e.type for e in events]
|
||||
assert EventType.REASONING_END not in types
|
||||
assert types[0] == EventType.TEXT_CHUNK
|
||||
assert events[0].value == "<tool_call>"
|
||||
assert engine._message_header_buffer == ""
|
||||
|
||||
@@ -23,8 +23,10 @@ from vllm.entrypoints.openai.chat_completion.protocol import (
|
||||
ChatCompletionToolsParam,
|
||||
FunctionDefinition,
|
||||
)
|
||||
from vllm.parser.engine.events import EventType
|
||||
from vllm.parser.engine.parser_engine_config import ParserState
|
||||
from vllm.parser.inkling import InklingParser, _inkling_arg_converter
|
||||
from vllm.parser.engine.streaming_parser_engine import StreamingParserEngine
|
||||
from vllm.parser.inkling import InklingParser, _inkling_arg_converter, inkling_config
|
||||
from vllm.parser.parser_manager import ParserManager
|
||||
|
||||
MSG_MODEL = "<|message_model|>"
|
||||
@@ -786,3 +788,84 @@ class TestDelegatingTwoPass:
|
||||
assert json.loads(args[0]) == {"city": "SF"}
|
||||
assert TOOL_JSON not in content
|
||||
assert END_MESSAGE not in content
|
||||
|
||||
@pytest.mark.parametrize("chunk_size", [1, 3, 7, 64])
|
||||
def test_tool_start_from_message_header_streaming(
|
||||
self, mock_tokenizer, mock_request, chunk_size
|
||||
):
|
||||
"""A tool call with no block of any kind ahead of it.
|
||||
|
||||
The generation prompt ends in ``<|message_model|>``, so this fires
|
||||
TOOL_START straight from MESSAGE_HEADER. A tool block is the one
|
||||
opener that confirms no reasoning is open without rendering visible
|
||||
content, so it is the case the visible-block openers cannot cover.
|
||||
"""
|
||||
tools = [_function_tool()]
|
||||
mock_request.tools = tools
|
||||
content, _, names, args = _stream_delegating(
|
||||
_delegating(mock_tokenizer, tools),
|
||||
mock_request,
|
||||
_tool_block("get_weather", '{"city":"Seattle"}'),
|
||||
chunk_size,
|
||||
self.GEN_PROMPT,
|
||||
)
|
||||
assert names == ["get_weather"]
|
||||
assert json.loads(args[0]) == {"city": "Seattle"}
|
||||
assert content == ""
|
||||
assert TOOL_JSON not in content
|
||||
assert END_MESSAGE not in content
|
||||
|
||||
def test_function_name_header_before_tool_start_streaming(
|
||||
self, mock_tokenizer, mock_request
|
||||
):
|
||||
"""The optional function name between ``<|message_model|>`` and the
|
||||
content-kind marker is metadata: the buffered header must be
|
||||
discarded on the way out, not flushed into content."""
|
||||
tools = [_function_tool()]
|
||||
mock_request.tools = tools
|
||||
content, _, names, args = _stream_delegating(
|
||||
_delegating(mock_tokenizer, tools),
|
||||
mock_request,
|
||||
"someFn" + _tool_block("get_weather", '{"city":"Seattle"}'),
|
||||
1,
|
||||
self.GEN_PROMPT,
|
||||
)
|
||||
assert names == ["get_weather"]
|
||||
assert json.loads(args[0]) == {"city": "Seattle"}
|
||||
assert content == ""
|
||||
|
||||
def test_content_state_tool_start_streaming(self, mock_tokenizer, mock_request):
|
||||
"""Same opener reached from CONTENT rather than MESSAGE_HEADER: a
|
||||
text block closed with no ``<|message_model|>`` before the tool
|
||||
block. Preceding text must reach content exactly once, unmarked."""
|
||||
tools = [_function_tool()]
|
||||
mock_request.tools = tools
|
||||
content, _, names, args = _stream_delegating(
|
||||
_delegating(mock_tokenizer, tools),
|
||||
mock_request,
|
||||
f"{TEXT_START}intro{END_MESSAGE}"
|
||||
+ _tool_block("get_weather", '{"city":"Seattle"}'),
|
||||
1,
|
||||
self.GEN_PROMPT,
|
||||
)
|
||||
assert names == ["get_weather"]
|
||||
assert json.loads(args[0]) == {"city": "Seattle"}
|
||||
assert content == "intro"
|
||||
|
||||
|
||||
def test_content_tool_start_emits_reasoning_end_in_reasoning_pass():
|
||||
"""(CONTENT, TOOL_START) must carry REASONING_END through the
|
||||
reasoning pass. Every visible-block opener already confirms the
|
||||
boundary (#49876), but the header-flush path
|
||||
(MESSAGE_HEADER --END_MESSAGE--> CONTENT) reaches CONTENT without
|
||||
one, so a tool block opening from there relies on this transition
|
||||
alone to hand off to the tool pass."""
|
||||
engine = StreamingParserEngine(inkling_config(), tokenizer=None)
|
||||
engine.skip_tool_parsing = True
|
||||
engine.reset(initial_state=ParserState.CONTENT)
|
||||
events = engine.parse_complete(f'{TOOL_JSON}{{"name":"f","args":{{}}}}')
|
||||
assert [e.type for e in events[:2]] == [
|
||||
EventType.REASONING_END,
|
||||
EventType.TEXT_CHUNK,
|
||||
]
|
||||
assert events[1].value == TOOL_JSON
|
||||
|
||||
@@ -342,16 +342,12 @@ class StreamingParserEngine:
|
||||
self._in_skipped_tool_span = True
|
||||
elif is_exit:
|
||||
self._in_skipped_tool_span = False
|
||||
if self.state == ParserState.MESSAGE_HEADER:
|
||||
self.state = ParserState.CONTENT
|
||||
leaving_message_header = self.state == ParserState.MESSAGE_HEADER
|
||||
if leaving_message_header:
|
||||
self._message_header_buffer = ""
|
||||
return [
|
||||
SemanticEvent(
|
||||
EventType.TEXT_CHUNK,
|
||||
value=value,
|
||||
tool_index=self.tool_index,
|
||||
)
|
||||
]
|
||||
# A tool terminal that implicitly ends reasoning must report
|
||||
# that even from the header state, or the reasoning pass never
|
||||
# hands the block to the tool pass.
|
||||
if EventType.REASONING_END in transition.events:
|
||||
self.state = ParserState.CONTENT
|
||||
return [
|
||||
@@ -366,6 +362,15 @@ class StreamingParserEngine:
|
||||
tool_index=self.tool_index,
|
||||
),
|
||||
]
|
||||
elif leaving_message_header:
|
||||
self.state = ParserState.CONTENT
|
||||
return [
|
||||
SemanticEvent(
|
||||
EventType.TEXT_CHUNK,
|
||||
value=value,
|
||||
tool_index=self.tool_index,
|
||||
)
|
||||
]
|
||||
content_type = self.config.content_events.get(self.state)
|
||||
if content_type is not None:
|
||||
return [
|
||||
|
||||
@@ -202,9 +202,11 @@ def inkling_config() -> ParserEngineConfig:
|
||||
ParserState.REASONING,
|
||||
(EventType.REASONING_START,),
|
||||
),
|
||||
# A tool block confirms the same boundary, and is the one opener that
|
||||
# can start a turn with no visible block ahead of it.
|
||||
(ParserState.CONTENT, "TOOL_START"): Transition(
|
||||
ParserState.TOOL_ARGS,
|
||||
(EventType.TOOL_CALL_START,),
|
||||
(EventType.REASONING_END, EventType.TOOL_CALL_START),
|
||||
),
|
||||
# Raw / error tool blocks render as visible text.
|
||||
(ParserState.CONTENT, "TOOL_TEXT"): Transition(
|
||||
@@ -231,7 +233,7 @@ def inkling_config() -> ParserEngineConfig:
|
||||
),
|
||||
(ParserState.MESSAGE_HEADER, "TOOL_START"): Transition(
|
||||
ParserState.TOOL_ARGS,
|
||||
(EventType.TOOL_CALL_START,),
|
||||
(EventType.REASONING_END, EventType.TOOL_CALL_START),
|
||||
),
|
||||
(ParserState.MESSAGE_HEADER, "TOOL_TEXT"): Transition(
|
||||
ParserState.CONTENT,
|
||||
|
||||
Reference in New Issue
Block a user