Skip to content

[#22962][fix] Report tool_calls as the finish reason of streamed gpt-oss tool call turns - #22963

Open
BachNg311 wants to merge 1 commit into
NVIDIA:mainfrom
BachNg311:fix/harmony-stream-tool-calls-finish-reason
Open

BachNg311 wants to merge 1 commit into
NVIDIA:mainfrom
BachNg311:fix/harmony-stream-tool-calls-finish-reason

Conversation

@BachNg311

@BachNg311 BachNg311 commented Nov 26, 2026 •

Copy link
Copy Markdown

Dev Engineer Review

  • tensorrt_llm/serve/harmony_adapter.py (+19/-2): Adds _determine_stream_finish_reason. It changes "stop" to "tool_calls" only when the stream state holds a tool call that passed filtering. It applies to both streaming finish chunks.
  • Risk: The change depends on HarmonyStreamState.tool_calls holding only filtered calls. Suppressed calls must keep "stop". The tests cover this case.
  • Scope: The "length" behavior and the non-streaming path are unchanged.

QA Engineer Review

  • tests/unittest/llmapi/apps/test_harmony_parsing.py (+106): Adds TestStreamingToolCallFinishReason.
    • test_final_chunk_finish_reason[tool_call] and test_stream_stopped_on_repeated_channel_after_tool_call assert "tool_calls".
    • [answer] and [suppressed_tool_call] assert "stop".
    • The author reports the two "tool_calls" tests fail on main. This is not independently verified.
  • Validation: The author ran test_harmony_parsing.py and test_harmony_channel_validation.py on CPU with real Harmony encoding. The full suite has not run.
  • Test-list placement in test-db/ or qa/: not available from the supplied evidence.
  • Coverage verdict: needs follow-up. Run the full suite and confirm test-list placement.

Per-File QA Perspective

  • tensorrt_llm/serve/harmony_adapter.py: Streamed responses that include a tool call now report finish_reason: "tool_calls". Verify the final chunk and the cut-short (should_stop) chunk. Verify that suppressed calls still report "stop".
  • tests/unittest/llmapi/apps/test_harmony_parsing.py: Covers the streaming finish-reason cases listed above. Confirm it is listed in the appropriate CI list. Placement is not available.

Description

Fixes #22962.

A streamed gpt-oss (Harmony) chat turn that makes a tool call ended with finish_reason: "stop": the final chunk of handle_streaming_response passed on the engine's finish reason, which is "stop" because a tool call ends at the <|call|> stop token, and the chunk create_openai_streaming_response emits when it cuts a stream short was hard-coded to "stop". The same request without streaming, and the chat stream of every other model, report "tool_calls", which is what OpenAI-compatible clients run the tool loop on.

This PR adds _determine_stream_finish_reason, the streaming counterpart of _determine_finish_reason: a "stop" becomes "tool_calls" when the stream state has streamed a tool call. Both streaming finish chunks use it. HarmonyStreamState.tool_calls only holds calls that passed tool filtering, so a call suppressed by tool_choice="none" or by the tool list still ends with "stop".

Like the chat stream of other models, only "stop" is changed. The non-streaming Harmony path also turns "length" into "tool_calls"; this PR leaves that as it is.

Draft PR #21735 keeps chat_harmony_streaming_post_processor and handle_streaming_response for gpt-oss, so the fix also applies after it.

Test Coverage

tests/unittest/llmapi/apps/test_harmony_parsing.py (cpu_only, already in l0_cpu.yml), TestStreamingToolCallFinishReason. The tests encode Harmony text with the real encoding and stream it through handle_streaming_response:

  • test_final_chunk_finish_reason[tool_call]: a tool call turn streams the call and ends with "tool_calls".
  • test_final_chunk_finish_reason[answer] and [suppressed_tool_call]: an answer turn, and a tool call suppressed by tool_choice="none", still end with "stop".
  • test_stream_stopped_on_repeated_channel_after_tool_call: a stream cut short by the channel validation after a streamed tool call ends with "tool_calls".

The tool_call and repeated_channel cases fail on main ('stop' != 'tool_calls') and pass with this change. They are fast CPU checks in a file that already runs in the pre-merge CPU stage.

Local validation was CPU-only: test_harmony_parsing.py and test_harmony_channel_validation.py ran against the repository's harmony_adapter.py with the real openai-harmony encoding and copies of the protocol models it builds (no test was skipped); the full suite has not run yet.

PR Checklist

Please review the following before submitting your PR:

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • If PR introduces API changes, an appropriate PR label is added - either api-compatible or api-breaking. For api-breaking, include BREAKING in the PR title.

  • Any new dependencies have been scanned for license and vulnerabilities

  • CODEOWNERS updated if ownership changes

  • Documentation updated as needed

  • Update tava architecture diagram if there is a significant design change in PR.

  • The reviewers assigned automatically/manually are appropriate for the PR.

  • Please check this after reviewing the above items as appropriate for this PR.

GitHub Bot Help

To see a list of available CI bot commands, please comment /bot help.

…d gpt-oss tool call turns

A streamed gpt-oss (Harmony) chat turn that made a tool call ended with
finish_reason "stop": the final chunk passed on the engine's finish
reason, which is "stop" because a tool call ends at the <|call|> stop
token, and the chunk sent when a stream is cut short was hard-coded to
"stop". The same request without streaming, and the chat stream of other
models, report "tool_calls".

Turn "stop" into "tool_calls" in both streaming finish chunks when the
stream state has streamed a tool call.

Signed-off-by: BachNg311 <nguyentatbach605@gmail.com>
@BachNg311
BachNg311 requested a review from a team as a code owner November 26, 2026 14:36
@BachNg311
BachNg311 requested review from cascade812 and mikeiovine and a balanced review from Copilot November 26, 2026 14:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@coderabbitai

coderabbitai Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Streaming Harmony responses now return tool_calls when the finish reason is stop and the stream state contains tool calls. Other finish reasons remain unchanged. Tests cover tool calls, ordinary answers, suppressed calls, and a repeated-channel stop.

Changes

Harmony streaming finish reasons

Layer / File(s) Summary
Map and test streaming finish reasons
tensorrt_llm/serve/harmony_adapter.py, tests/unittest/llmapi/apps/test_harmony_parsing.py
The adapter applies stream-state-based finish reasons to per-delta and final responses. Tests cover tool calls, ordinary answers, suppressed calls, and a repeated-channel stop. The adapter also adds a 2026 NVIDIA copyright notice.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: junyixu-nv

Merge Risk: Minimal · up to 00880

Streamed gpt-oss tool-call turns now end with finish_reason "tool_calls", so agent clients can run their tool loop. Ordinary answers and suppressed tool calls still end with "stop". The change is small and tested, and no merge-blocking risk was found.

Security architecture risk: Low · up to 00880

The change makes streamed tool calls easier for clients to recognize. There is one narrow failure case: if parsing fails after recording a tool call, the response can tell the client to continue its tool loop even though no call was delivered. The change does not give the server new authority to run tools.

Retained concerns

  • Low · reliability · inferred: If parsing fails after the batch records its first tool call, the recovery handler can discard all deltas for that batch while retaining the call in parser state. If no call is emitted later, the new terminal mapping can report tool_calls with no corresponding call in the stream, causing clients that rely on the finish reason to enter a tool loop with no call to process.
Security review details

Security Blast Radius

  • observed — The changed signal is returned to clients of the existing Harmony chat stream. The changed path serializes tool-call data and a finish reason; it does not invoke a server-side tool or grant credentials.

Trust Boundaries and Controls

  • observed — The production postprocessor keys stream state on the executor-assigned result ID rather than a public request field. Existing tool filtering, including explicit suppression, applies before a model-generated call is recorded and can influence the new terminal signal.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: streamed gpt-oss (Harmony) tool-call turns now report "tool_calls" as the finish reason. It follows the required [ticket][type] Summary format with the GitHub issue #2…
Description check Passed The description covers the required template parts. It explains the issue and the fix in short form, and it names the test file, test class, and test cases that cover the change. It links issue #22962…
Linked Issues check Passed Directly linked issue #22962 requires that streamed gpt-oss chat tool-call turns end with finish_reason "tool_calls", that the cut-short stream chunk follows the same rule, and that suppressed calls d…
Out of Scope Changes check Passed All changes relate to the objective. The new helper _determine_stream_finish_reason and its two call sites implement the fix. The test additions in test_harmony_parsing.py support the fix. The only ch…
Full details: Title check

Explanation

The title names the main change: streamed gpt-oss (Harmony) tool-call turns now report "tool_calls" as the finish reason. It follows the required [ticket][type] Summary format with the GitHub issue #22962 and the fix type. The raw summary and objectives confirm the change adds _determine_stream_finish_reason and applies it to the streaming finish chunks.

Full details: Description check

Explanation

The description covers the required template parts. It explains the issue and the fix in short form, and it names the test file, test class, and test cases that cover the change. It links issue #22962, and the checklist is present with the final box checked. The template's @coderabbitai summary header is present, and the section on the fix explains the cause of the bug, the new helper, and the scope of the change, including what is intentionally left unchanged. The description does not state an API label decision, but the change does not alter public declarations, so the omission is minor.

Full details: Linked Issues check

Explanation

Directly linked issue #22962 requires that streamed gpt-oss chat tool-call turns end with finish_reason "tool_calls", that the cut-short stream chunk follows the same rule, and that suppressed calls do not change the reason. The diff to tensorrt_llm/serve/harmony_adapter.py (merge base 6c18d93 to head 008801a) meets these points: (1) the final chunk in handle_streaming_response now passes output.finish_reason through the new _determine_stream_finish_reason, using harmony_adapter.get_stream_state(request_id); (2) the should_stop chunk in create_openai_streaming_response uses the same helper instead of a hard-coded "stop"; (3) the helper returns "tool_calls" only when reason is "stop" and stream_state.tool_calls is non-empty. HarmonyStreamState.tool_calls holds only calls that passed tool filtering, so suppressed calls keep "stop". Test coverage is in tests/unittest/llmapi/apps/test_harmony_parsing.py (TestStreamingToolCallFinishReason), per the PR description and the summary. Not verified: I did not read the full harmony_adapter.py to confirm the get_stream_state method signature and the stream_state variable scope in create_openai_streaming_response; the reviewed diff uses both consistently. No coding requirement from the issue is unmet on the evidence available.

Full details: Out of Scope Changes check

Explanation

All changes relate to the objective. The new helper _determine_stream_finish_reason and its two call sites implement the fix. The test additions in test_harmony_parsing.py support the fix. The only change not directly tied to behavior is the added 2026 NVIDIA SPDX copyright line in harmony_adapter.py. It is a repository header convention and does not alter runtime behavior, so it is not a demonstrated unrelated change. The PR does not change the non-streaming "length" handling, which the PR description states is intentionally left unchanged.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 6c18d93 and 008801a.

Files selected for processing: 2

  • tensorrt_llm/serve/harmony_adapter.py
  • tests/unittest/llmapi/apps/test_harmony_parsing.py

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: gpt-oss streaming chat ends tool-call turns with finish_reason "stop" instead of "tool_calls"

2 participants