Skip to content

Inference: Bring chat completions API inline with vllm/official openAI spec - #5276

Merged
sidsingh-nvidia merged 5 commits into
NVIDIA:mainfrom
sidsingh-nvidia:siddharth/fix-think-token-retention-multi-turn
Jul 14, 2026
Merged

Inference: Bring chat completions API inline with vllm/official openAI spec#5276
sidsingh-nvidia merged 5 commits into
NVIDIA:mainfrom
sidsingh-nvidia:siddharth/fix-think-token-retention-multi-turn

Conversation

@sidsingh-nvidia

@sidsingh-nvidia sidsingh-nvidia commented Jun 10, 2026

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do ?

  1. Use the chat template to retain reasoning tokens, instead of doing it in the server.
  2. Getting token IDs and raw text are now opt-in flags (default False). Opted-in in MRL, but should be removed if not being used.
  3. Additionally, this PR also adds support for ignore_eos.

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact @NVIDIA/mcore-oncall.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue:

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment @NVIDIA/mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge your PR.

@sidsingh-nvidia
sidsingh-nvidia requested review from a team as code owners June 10, 2026 22:21
@sidsingh-nvidia
sidsingh-nvidia requested a review from tdene June 10, 2026 22:21
@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft June 10, 2026 22:21
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically converted to draft because all PRs must start as drafts.

When you are ready for review, click Ready for Review to begin the review process. This will:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@copy-pr-bot

copy-pr-bot Bot commented Jun 10, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@sidsingh-nvidia
sidsingh-nvidia marked this pull request as ready for review June 10, 2026 22:23
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Final Review PR is in the "final review" stage label Jun 11, 2026
@sidsingh-nvidia

Copy link
Copy Markdown
Contributor Author

/claude strict-review

Comment on lines +738 to 740
# Small RL/debug scalars (a few bytes each); harmless to keep for
# NeMo-RL compatibility.
message["generation_log_probs"] = result.get("generated_log_probs", [])

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.

[SUGGESTION Naming] The comment "Small RL/debug scalars (a few bytes each)" accurately describes policy_epoch, kv_cache_epoch, and num_evictions, but it is placed on a block whose first member is generation_log_probs — a per-generated-token float list, not a scalar. For a long completion this is one of the larger fields in the payload, which contradicts both the "a few bytes each" wording and the PR's stated goal of cutting payload size.

Why it matters: a wrong comment is worse than none — a future reader trimming the payload may skip generation_log_probs believing it is negligible.

Suggested fix: move the comment below generation_log_probs so it only covers the three scalar fields, and note that generation_log_probs is always included because RL clients (e.g. megatron/rl/inference/megatron.py) read choice.message.generation_log_probs. If it should also be gated behind an opt-in flag like the token ids, consider doing so for consistency.

Comment on lines +760 to 769
# Choice-level prompt/generation_token_ids, generation_log_probs and
# raw_text were duplicates of message-level data (or reconstructable);
# dropped to match vLLM's response shape and cut payload size.
choice_data = {
"index": request_idx,
"message": message,
"prompt_token_ids": result["prompt_tokens"],
"generation_token_ids": result["generated_tokens"],
"generation_log_probs": result.get("generated_log_probs", []),
"raw_text": result["prompt"] + result["generated_text"],
# 'logprobs' in chat API is an object containing 'content'
# "logprobs": {"content": logprobs_content} if logprobs_content else None,
"logprobs": {"content": logprobs_content} if return_log_probs else None,
"finish_reason": finish_reason,
}

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.

[IMPORTANT Compatibility] This removes prompt_token_ids, generation_token_ids, generation_log_probs, and raw_text from the choice object. The in-repo consumer (megatron/rl/inference/megatron.py) is updated in this PR to read them from choice.message.* instead, so the internal path is fine. But this is a response-shape change on a public OpenAI-compatible endpoint: any external client that read these at the choice level (e.g. a NeMo-RL client pinned to the previous shape) will silently get None/AttributeError after this change, with no deprecation window.

Why it matters: silent breakage of an external client contract is hard to diagnose from the server side.

Suggestion: confirm NeMo-RL (and any other downstream) reads the message-level fields, and if this endpoint is externally consumed, call out the shape change in the PR description / release notes. No code change required if downstreams are already aligned.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is this deviating from the official openAI spec?

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.

No, these are not part of the spec.

@claude

claude Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Strict Review Summary

Findings: CRITICAL: 0 · IMPORTANT: 1 · SUGGESTION: 1

Small, well-scoped inference-server change: (1) reasoning-token retention delegated to the chat template (removing _reconstruct_reasoning_content), (2) prompt/generation token ids and raw text become opt-in via return_tokenized_data/return_raw_text, (3) ignore_eos support added.

What I verified (correct)

  • ignore_eos to termination_id=-1 if ignore_eos else None matches the sibling completions.py endpoint and the SamplingParams contract (None falls back to EOD). Correct.
  • raw_text reconstruction (tokenizer.detokenize(result['prompt_tokens']) + text_output) is equivalent to the removed coordinator-level result['prompt'] + generated_text; dropping the coordinator prompt detokenization is safe, no remaining reader depends on finished_request['prompt'].
  • All new identifiers (ignore_eos, prevent_retokenization, return_tokenized_data, return_raw_text) have real use paths.
  • In-repo consumer megatron/rl/inference/megatron.py correctly migrated to choice.message.*.
  • No new parallel_state.get_*_group() usage; no dtype/parallelism concerns.

Findings

  • [IMPORTANT Compatibility] Choice-level prompt_token_ids/generation_token_ids/generation_log_probs/raw_text removed from a public OpenAI-compatible endpoint. Internal caller updated, but external clients pinned to the old shape break silently. Confirm downstreams (NeMo-RL) read message-level fields and note the shape change in PR/release notes.
  • [SUGGESTION Naming] The 'Small RL/debug scalars (a few bytes each)' comment sits above generation_log_probs, a per-token float list, not a scalar; misleading given the payload-reduction goal.

Risk: Low. Logic changes are minimal and mirror existing tested patterns. The only real risk is the external response-shape compatibility item above.

Comment on lines +738 to 740
# Small RL/debug scalars (a few bytes each); harmless to keep for
# NeMo-RL compatibility.
message["generation_log_probs"] = result.get("generated_log_probs", [])

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.

[SUGGESTION Naming] The comment "Small RL/debug scalars (a few bytes each)" accurately describes policy_epoch, kv_cache_epoch, and num_evictions, but it is applied to a block whose first member is generation_log_probs — a per-generated-token float list, not a scalar. For a long completion this is one of the larger fields in the payload, which directly contradicts the "a few bytes each" characterization and the PR's stated goal of cutting payload size.

Why it matters: a wrong comment is worse than none — a future reader trimming the payload may skip generation_log_probs believing it is negligible.

Suggested fix: move the comment below generation_log_probs so it only covers the three scalar fields, and note separately that generation_log_probs is always included (it is required by RL clients such as megatron/rl/inference/megatron.py, which reads choice.message.generation_log_probs). If it should also be gated behind an opt-in flag like the token ids, consider doing so for consistency.

Comment on lines +760 to 769
# Choice-level prompt/generation_token_ids, generation_log_probs and
# raw_text were duplicates of message-level data (or reconstructable);
# dropped to match vLLM's response shape and cut payload size.
choice_data = {
"index": request_idx,
"message": message,
"prompt_token_ids": result["prompt_tokens"],
"generation_token_ids": result["generated_tokens"],
"generation_log_probs": result.get("generated_log_probs", []),
"raw_text": result["prompt"] + result["generated_text"],
# 'logprobs' in chat API is an object containing 'content'
# "logprobs": {"content": logprobs_content} if logprobs_content else None,
"logprobs": {"content": logprobs_content} if return_log_probs else None,
"finish_reason": finish_reason,
}

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.

[IMPORTANT Compatibility] This removes prompt_token_ids, generation_token_ids, generation_log_probs, and raw_text from the choice object. The in-repo consumer (megatron/rl/inference/megatron.py) is updated in this PR to read them from choice.message.* instead, so the internal path is fine. However, this is a response-shape change on a public OpenAI-compatible endpoint: any external client that read these at the choice level (e.g. a NeMo-RL client pinned to the previous shape) will silently get None/AttributeError after this change, with no deprecation window.

Why it matters: silent breakage of an external client contract is hard to diagnose from the server side.

Suggestion: confirm the NeMo-RL (and any other downstream) client reads the message-level fields, and if this endpoint is externally consumed, call out the shape change in the PR description / release notes so consumers can migrate. No code change required if downstreams are already aligned.

Comment on lines +760 to 769
# Choice-level prompt/generation_token_ids, generation_log_probs and
# raw_text were duplicates of message-level data (or reconstructable);
# dropped to match vLLM's response shape and cut payload size.
choice_data = {
"index": request_idx,
"message": message,
"prompt_token_ids": result["prompt_tokens"],
"generation_token_ids": result["generated_tokens"],
"generation_log_probs": result.get("generated_log_probs", []),
"raw_text": result["prompt"] + result["generated_text"],
# 'logprobs' in chat API is an object containing 'content'
# "logprobs": {"content": logprobs_content} if logprobs_content else None,
"logprobs": {"content": logprobs_content} if return_log_probs else None,
"finish_reason": finish_reason,
}

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.

No, these are not part of the spec.

choice_data = {
"index": request_idx,
"message": message,
"prompt_token_ids": result["prompt_tokens"],

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.

👍

@sidsingh-nvidia
sidsingh-nvidia added this pull request to the merge queue Jul 14, 2026
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/29343662639

@sidsingh-nvidia sidsingh-nvidia changed the title Delegate reasoning token retention to the chat template in multi-turn conversations Inference: Bring chat completions API inline with vllm/official openAI spec Jul 14, 2026
Merged via the queue into NVIDIA:main with commit a79f49d Jul 14, 2026
107 of 122 checks passed
@sidsingh-nvidia
sidsingh-nvidia deleted the siddharth/fix-think-token-retention-multi-turn branch July 14, 2026 15:54
chochowski pushed a commit to chochowski/Megatron-LM that referenced this pull request Jul 20, 2026
… conversations (NVIDIA#5276)

Signed-off-by: mchochowski <mchochowski@nvidia.com>
sidsingh-nvidia added a commit to sidsingh-nvidia/Megatron-LM that referenced this pull request Jul 20, 2026
terminator123 pushed a commit to 021ai/Megatron-LM that referenced this pull request Aug 3, 2026
svcnvidia-nemo-ci pushed a commit to dimapihtar/Megatron-LM that referenced this pull request Aug 4, 2026
… conversations (NVIDIA#5276)

Signed-off-by: Dmytro Pykhtar <dpykhtar@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made complexity: low

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants