@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / 14cb86c187
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 5, 06:00 UTC

[Refactor][Parser] Simplify parse_delta (#39728)

Signed-off-by: sfeng33 <4florafeng@gmail.com>

Flora Feng committed Apr 13, 2026 at 17:02 UTC 14cb86c1872cbe0da1cf8030721b4aa2ab9ffbb8
1 file changed +36 -42
vllm/parser/abstract_parser.py
+36 -42
@@ -568,6 +568,20 @@ class DelegatingParser(Parser):
568 return input_ids
569 return self._reasoning_parser.extract_content_ids(input_ids)
570
571 + def _in_reasoning_phase(self, state: StreamState) -> bool:
572 + if self._reasoning_parser is None:
573 + return False
574 + if self._tool_parser is None:
575 + return True
576 + return not state.reasoning_ended
577 +
578 + def _in_tool_call_phase(self, state: StreamState) -> bool:
579 + if self._tool_parser is None:
580 + return False
581 + if self._reasoning_parser is None:
582 + return True
583 + return state.reasoning_ended
584 +
585 def parse_delta(
586 self,
587 delta_text: str,
@@ -584,47 +598,10 @@ class DelegatingParser(Parser):
598
599 current_text = state.previous_text + delta_text
600 current_token_ids = state.previous_token_ids + delta_token_ids
587 -
601 delta_message: DeltaMessage | None = None
602
590 - if self._reasoning_parser and self._tool_parser:
591 - if not state.reasoning_ended:
592 - delta_message = self.extract_reasoning_streaming(
593 - previous_text=state.previous_text,
594 - current_text=current_text,
595 - delta_text=delta_text,
596 - previous_token_ids=state.previous_token_ids,
597 - current_token_ids=current_token_ids,
598 - delta_token_ids=delta_token_ids,
599 - )
600 - if self.is_reasoning_end(delta_token_ids):
601 - state.reasoning_ended = True
602 - current_token_ids = self.extract_content_ids(delta_token_ids)
603 - if delta_message and delta_message.content:
604 - current_text = delta_message.content
605 - delta_message.content = None
606 - else:
607 - current_text = ""
608 -
609 - if state.reasoning_ended:
610 - if not state.tool_call_text_started:
611 - state.tool_call_text_started = True
612 - state.previous_text = ""
613 - state.previous_token_ids = []
614 - delta_text = current_text
615 - delta_token_ids = current_token_ids
616 -
617 - delta_message = self.extract_tool_calls_streaming(
618 - previous_text=state.previous_text,
619 - current_text=current_text,
620 - delta_text=delta_text,
621 - previous_token_ids=state.previous_token_ids,
622 - current_token_ids=current_token_ids,
623 - delta_token_ids=delta_token_ids,
624 - request=request, # type: ignore[arg-type]
625 - )
626 -
627 - elif self._reasoning_parser:
603 + # Reasoning extraction
604 + if self._in_reasoning_phase(state):
605 delta_message = self.extract_reasoning_streaming(
606 previous_text=state.previous_text,
607 current_text=current_text,
@@ -633,8 +610,24 @@ class DelegatingParser(Parser):
610 current_token_ids=current_token_ids,
611 delta_token_ids=delta_token_ids,
612 )
636 -
637 - elif self._tool_parser:
613 + # Hand off remaining content to tool parser
614 + if self._tool_parser and self.is_reasoning_end(delta_token_ids):
615 + state.reasoning_ended = True
616 + current_token_ids = self.extract_content_ids(delta_token_ids)
617 + if delta_message and delta_message.content:
618 + current_text = delta_message.content
619 + delta_message.content = None
620 + else:
621 + current_text = ""
622 +
623 + # Tool call extraction
624 + if self._in_tool_call_phase(state):
625 + if not state.tool_call_text_started:
626 + state.tool_call_text_started = True
627 + state.previous_text = ""
628 + state.previous_token_ids = []
629 + delta_text = current_text
630 + delta_token_ids = current_token_ids
631 delta_message = self.extract_tool_calls_streaming(
632 previous_text=state.previous_text,
633 current_text=current_text,
@@ -645,7 +638,8 @@ class DelegatingParser(Parser):
638 request=request, # type: ignore[arg-type]
639 )
640
648 - else:
641 + # No parsers: pass through as content
642 + if self._reasoning_parser is None and self._tool_parser is None:
643 delta_message = DeltaMessage(content=delta_text)
644
645 state.previous_text = current_text