[Feature][Parser] Support include_reasoning param for non-Harmony models (#44301)
Signed-off-by: Alberto Perdomo <aperdomo@redhat.com> Co-authored-by: Chauncey <chaunceyjiang@gmail.com>
This commit is contained in:
@@ -600,19 +600,8 @@ class OpenAIServingChat(GenerateBaseServing):
|
||||
prompt_token_ids=res.prompt_token_ids,
|
||||
finished=output.finish_reason is not None,
|
||||
)
|
||||
if delta_message is not None:
|
||||
if delta_message.tool_calls:
|
||||
tools_streamed[i] = True
|
||||
|
||||
if (
|
||||
delta_message.reasoning
|
||||
and not request.include_reasoning
|
||||
):
|
||||
delta_message.reasoning = None
|
||||
if not (
|
||||
delta_message.content or delta_message.tool_calls
|
||||
):
|
||||
delta_message = None
|
||||
if delta_message is not None and delta_message.tool_calls:
|
||||
tools_streamed[i] = True
|
||||
|
||||
# handle streaming just a content delta (no parsers)
|
||||
else:
|
||||
@@ -627,13 +616,22 @@ class OpenAIServingChat(GenerateBaseServing):
|
||||
# "control token" for tool calls or the parser otherwise
|
||||
# wasn't ready to send a token, then
|
||||
# get the next token without streaming a chunk
|
||||
# When reasoning is hidden, suppress per-token
|
||||
# metadata (logprobs, token_ids) on every chunk to
|
||||
# prevent leaking reasoning tokens through decoded
|
||||
# token text in logprob entries or raw token IDs.
|
||||
hide_stream_metadata = (
|
||||
not request.include_reasoning and parser is not None
|
||||
)
|
||||
if hide_stream_metadata:
|
||||
logprobs = None
|
||||
|
||||
if delta_message is None:
|
||||
# NOTE: If return_token_ids is enabled, we still need to
|
||||
# send a chunk with token_ids even if delta_message is None
|
||||
# to ensure all tokens are included in the response
|
||||
if (
|
||||
output.finish_reason is None
|
||||
and not request.return_token_ids
|
||||
if output.finish_reason is None and (
|
||||
not request.return_token_ids or hide_stream_metadata
|
||||
):
|
||||
continue
|
||||
delta_message = DeltaMessage()
|
||||
@@ -666,6 +664,10 @@ class OpenAIServingChat(GenerateBaseServing):
|
||||
delta=True,
|
||||
)
|
||||
|
||||
include_token_ids = (
|
||||
request.return_token_ids and not hide_stream_metadata
|
||||
)
|
||||
|
||||
if output.finish_reason is None:
|
||||
# Send token-by-token response for each request.n
|
||||
choice_data = ChatCompletionResponseStreamChoice(
|
||||
@@ -674,9 +676,7 @@ class OpenAIServingChat(GenerateBaseServing):
|
||||
logprobs=logprobs,
|
||||
finish_reason=None,
|
||||
token_ids=(
|
||||
as_list(output.token_ids)
|
||||
if request.return_token_ids
|
||||
else None
|
||||
as_list(output.token_ids) if include_token_ids else None
|
||||
),
|
||||
)
|
||||
|
||||
@@ -704,9 +704,7 @@ class OpenAIServingChat(GenerateBaseServing):
|
||||
finish_reason=finish_reason_,
|
||||
stop_reason=output.stop_reason,
|
||||
token_ids=(
|
||||
as_list(output.token_ids)
|
||||
if request.return_token_ids
|
||||
else None
|
||||
as_list(output.token_ids) if include_token_ids else None
|
||||
),
|
||||
)
|
||||
|
||||
@@ -883,12 +881,16 @@ class OpenAIServingChat(GenerateBaseServing):
|
||||
enable_auto_tools=self.enable_auto_tools,
|
||||
model_output_token_ids=token_ids,
|
||||
)
|
||||
suppress_metadata = not request.include_reasoning and parser is not None
|
||||
if not request.include_reasoning:
|
||||
reasoning = None
|
||||
if suppress_metadata:
|
||||
logprobs = None
|
||||
else:
|
||||
reasoning = None
|
||||
content = output.text
|
||||
tool_calls = []
|
||||
suppress_metadata = False
|
||||
|
||||
auto_tools_called = False
|
||||
is_named_tool_choice = (
|
||||
@@ -982,7 +984,9 @@ class OpenAIServingChat(GenerateBaseServing):
|
||||
else "stop",
|
||||
stop_reason=output.stop_reason,
|
||||
token_ids=(
|
||||
as_list(output.token_ids) if request.return_token_ids else None
|
||||
as_list(output.token_ids)
|
||||
if request.return_token_ids and not suppress_metadata
|
||||
else None
|
||||
),
|
||||
routed_experts=routed_experts_b64,
|
||||
)
|
||||
|
||||
@@ -310,7 +310,9 @@ class ParsableContext(ConversationContext):
|
||||
self.finish_reason: str | None = None
|
||||
self.enable_auto_tools = enable_auto_tools
|
||||
|
||||
self.response_parser = response_parser
|
||||
self.response_parser = response_parser or (
|
||||
parser_cls(tokenizer, request.tools) if parser_cls is not None else None
|
||||
)
|
||||
self.parser_cls = parser_cls
|
||||
self.request = request
|
||||
|
||||
@@ -344,6 +346,8 @@ class ParsableContext(ConversationContext):
|
||||
enable_auto_tools=self.enable_auto_tools,
|
||||
model_output_token_ids=completion.token_ids,
|
||||
)
|
||||
if not self.request.include_reasoning:
|
||||
reasoning = None
|
||||
self.response_messages.extend(
|
||||
build_response_output_items(
|
||||
reasoning=reasoning,
|
||||
|
||||
@@ -161,6 +161,15 @@ class ResponsesRequest(OpenAIBaseModel):
|
||||
previous_response_id: str | None = None
|
||||
prompt: ResponsePrompt | None = None
|
||||
reasoning: Reasoning | None = None
|
||||
include_reasoning: bool = Field(
|
||||
default=True,
|
||||
description=(
|
||||
"Whether to include reasoning content in the response. "
|
||||
"When false, reasoning tokens are still generated but "
|
||||
"excluded from the output. This reduces network traffic "
|
||||
"without affecting model inference."
|
||||
),
|
||||
)
|
||||
service_tier: Literal["auto", "default", "flex", "scale", "priority"] = "auto"
|
||||
store: bool | None = True
|
||||
stream: bool | None = False
|
||||
|
||||
@@ -1068,6 +1068,9 @@ class OpenAIServingResponses(GenerateBaseServing):
|
||||
enable_auto_tools=self.enable_auto_tools,
|
||||
model_output_token_ids=final_output.token_ids,
|
||||
)
|
||||
if not request.include_reasoning:
|
||||
reasoning = None
|
||||
logprobs = None
|
||||
return build_response_output_items(
|
||||
reasoning=reasoning,
|
||||
content=content,
|
||||
@@ -1342,11 +1345,15 @@ class OpenAIServingResponses(GenerateBaseServing):
|
||||
) -> AsyncGenerator[StreamingResponsesResponse, None]:
|
||||
processor = SimpleStreamingEventProcessor(tools=request.tools)
|
||||
|
||||
hide_stream_metadata = not request.include_reasoning and self.parser is not None
|
||||
|
||||
def _get_logprobs(
|
||||
output: CompletionOutput,
|
||||
) -> list[response_text_delta_event.Logprob]:
|
||||
if not request.is_include_output_logprobs():
|
||||
return []
|
||||
if hide_stream_metadata:
|
||||
return []
|
||||
return self._create_stream_response_logprobs(
|
||||
token_ids=output.token_ids,
|
||||
logprobs=output.logprobs,
|
||||
|
||||
Reference in New Issue
Block a user