[Bugfix][Reasoning] Nemotron V3: surface reasoning as content when thinking is unterminated (#39091)
Signed-off-by: Andrii Skliar <askliar@nvidia.com> Co-authored-by: Andrii Skliar <askliar@nvidia.com>
This commit is contained in:
co-authored by
Andrii Skliar
parent
77f42d9725
commit
4882fd7632
@@ -776,6 +776,28 @@ class DelegatingParser(Parser):
|
||||
last_tc.function.arguments or ""
|
||||
) + self._tool_parser.get_remaining_unstreamed_args()
|
||||
|
||||
def finalize_generation(
|
||||
self,
|
||||
delta_message: DeltaMessage | None,
|
||||
request: ChatCompletionRequest | ResponsesRequest,
|
||||
state: StreamState,
|
||||
) -> DeltaMessage | None:
|
||||
"""Finalize generation for cases where generation was incomplete.
|
||||
For example, if streaming terminated before reasoning ended
|
||||
"""
|
||||
fallback_fn = getattr(
|
||||
self._reasoning_parser, "get_streaming_fallback_content", None
|
||||
)
|
||||
if fallback_fn is not None and not state.reasoning_ended:
|
||||
promoted = fallback_fn(state.previous_text, request)
|
||||
if promoted:
|
||||
if delta_message is None:
|
||||
delta_message = DeltaMessage()
|
||||
delta_message.content = (delta_message.content or "") + promoted
|
||||
|
||||
self._append_unstreamed_tool_args(delta_message)
|
||||
return delta_message
|
||||
|
||||
def parse(
|
||||
self,
|
||||
model_output: str,
|
||||
@@ -883,6 +905,6 @@ class DelegatingParser(Parser):
|
||||
state.previous_token_ids = current_token_ids
|
||||
|
||||
if finished:
|
||||
self._append_unstreamed_tool_args(delta_message)
|
||||
delta_message = self.finalize_generation(delta_message, request, state)
|
||||
|
||||
return delta_message
|
||||
|
||||
@@ -14,20 +14,35 @@ class NemotronV3ReasoningParser(DeepSeekR1ReasoningParser):
|
||||
Reasoning parser for Nemotron V3 models.
|
||||
"""
|
||||
|
||||
def extract_reasoning(
|
||||
self, model_output: str, request: ChatCompletionRequest | ResponsesRequest
|
||||
) -> tuple[str | None, str | None]:
|
||||
reasoning, final_content = super().extract_reasoning(model_output, request)
|
||||
def _should_force_content(
|
||||
self, request: ChatCompletionRequest | ResponsesRequest
|
||||
) -> bool:
|
||||
chat_template_kwargs = getattr(request, "chat_template_kwargs", None)
|
||||
|
||||
if (
|
||||
return bool(
|
||||
chat_template_kwargs
|
||||
and (
|
||||
chat_template_kwargs.get("enable_thinking") is False
|
||||
or chat_template_kwargs.get("force_nonempty_content") is True
|
||||
)
|
||||
and (final_content is None or not final_content.strip())
|
||||
)
|
||||
|
||||
def extract_reasoning(
|
||||
self, model_output: str, request: ChatCompletionRequest | ResponsesRequest
|
||||
) -> tuple[str | None, str | None]:
|
||||
reasoning, final_content = super().extract_reasoning(model_output, request)
|
||||
|
||||
if self._should_force_content(request) and (
|
||||
final_content is None or not final_content.strip()
|
||||
):
|
||||
reasoning, final_content = final_content, reasoning
|
||||
|
||||
return reasoning, final_content
|
||||
|
||||
def get_streaming_fallback_content(
|
||||
self, text: str, request: ChatCompletionRequest | ResponsesRequest
|
||||
) -> str | None:
|
||||
"""Reasoning to duplicate into content on the terminal streaming delta."""
|
||||
if not self._should_force_content(request):
|
||||
return None
|
||||
reasoning, _ = super().extract_reasoning(text, request)
|
||||
return reasoning
|
||||
|
||||
Reference in New Issue
Block a user