| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | from collections.abc import Iterable, Sequence |
| 5 | from typing import TYPE_CHECKING |
| 6 | |
| 7 | from transformers import PreTrainedTokenizerBase |
| 8 | |
| 9 | from vllm.entrypoints.generate.base.protocol import DeltaMessage |
| 10 | from vllm.reasoning import ReasoningParser |
| 11 | |
| 12 | if TYPE_CHECKING: |
| 13 | from vllm.entrypoints.openai.chat_completion.protocol import ChatCompletionRequest |
| 14 | from vllm.entrypoints.openai.responses.protocol import ResponsesRequest |
| 15 | |
| 16 | |
| 17 | class IdentityReasoningParser(ReasoningParser): |
| 18 | """Identity reasoning parser. |
| 19 | |
| 20 | This parser does not attempt to parse or strip out reasoning tokens. |
| 21 | It treats the entire model output as content and ignores reasoning. |
| 22 | """ |
| 23 | |
| 24 | def __init__(self, tokenizer: PreTrainedTokenizerBase, *args, **kwargs): |
| 25 | super().__init__(tokenizer, *args, **kwargs) |
| 26 | if not self.model_tokenizer: |
| 27 | raise ValueError( |
| 28 | "The model tokenizer must be passed to the ReasoningParser " |
| 29 | "constructor during construction." |
| 30 | ) |
| 31 | |
| 32 | @property |
| 33 | def reasoning_start_str(self) -> str | None: |
| 34 | return None |
| 35 | |
| 36 | @property |
| 37 | def reasoning_end_str(self) -> str | None: |
| 38 | return None |
| 39 | |
| 40 | def is_reasoning_end(self, input_ids: Sequence[int]) -> bool: |
| 41 | # Always return True, since we never treat reasoning specially |
| 42 | return True |
| 43 | |
| 44 | def is_reasoning_end_streaming( |
| 45 | self, input_ids: Sequence[int], delta_ids: Iterable[int] |
| 46 | ) -> bool: |
| 47 | return True |
| 48 | |
| 49 | def extract_content_ids(self, input_ids: list[int]) -> list[int]: |
| 50 | # Identity: return all tokens as content |
| 51 | return input_ids |
| 52 | |
| 53 | def extract_reasoning_streaming( |
| 54 | self, |
| 55 | previous_text: str, |
| 56 | current_text: str, |
| 57 | delta_text: str, |
| 58 | previous_token_ids: Sequence[int], |
| 59 | current_token_ids: Sequence[int], |
| 60 | delta_token_ids: Sequence[int], |
| 61 | ) -> DeltaMessage | None: |
| 62 | # Just wrap delta_text as content, ignore reasoning |
| 63 | if delta_text: |
| 64 | return DeltaMessage(content=delta_text) |
| 65 | return None |
| 66 | |
| 67 | def extract_reasoning( |
| 68 | self, model_output: str, request: "ChatCompletionRequest | ResponsesRequest" |
| 69 | ) -> tuple[str | None, str | None]: |
| 70 | # No reasoning separation: return None for reasoning, |
| 71 | # and full model_output as content |
| 72 | return None, model_output |