Skip to content

fix(llm): preserve explicit zero max_completion_tokens in chat template - #12812

Closed
weizhoublue wants to merge 1 commit into
ai-dynamo:mainfrom
weizhoublue:fix/max-completion-tokens-template-zero
Closed

weizhoublue wants to merge 1 commit into
ai-dynamo:mainfrom
weizhoublue:fix/max-completion-tokens-template-zero

Conversation

@weizhoublue

@weizhoublue weizhoublue commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Overview:

Silent Validation Bypass: Explicit  max_completion_tokens: 0  Escapes Rejection When a Request Template Is Configured

Details:

When a client explicitly sent max_completion_tokens: 0 , an illegal value that should be rejected

the code request.max_completion_tokens.unwrap_or(0) == 0 silently replaced it with the template's default value (e.g. 128 ). By the time validation ran, it saw 128, not 0.

So the illegal input got laundered into a legal one, and the validation error never fired:

// lib/llm/src/protocols/openai/validate.rs:786
pub fn validate_max_completion_tokens(max_completion_tokens: Option<u32>) -> Result<(), anyhow::Error> {
    if let Some(tokens) = max_completion_tokens && tokens == 0 {
        anyhow::bail!("Max completion tokens must be greater than 0, got {}", tokens);
    }
    Ok(())
}

The --template-file feature should not turn an illegal input into a legal one. It exists to fill in omitted fields, not to override an explicit (even if invalid) value the client sent.

Fix: request.max_completion_tokens.is_none()

This only fills in the template default when the field is truly absent (omitted). An explicit 0 stays 0 , reaches validation, and is correctly rejected with "Max completion tokens must be greater than 0".

Where should the reviewer start?

Related Issues

⚠️ This section is required. Choose one path below and delete the other.

🔗 This PR is linked to an issue:

  • Closes #XXXX

🚫 This PR is NOT linked to an issue:

  • Confirmed — no related issue

Open in Devin Review

Summary by CodeRabbit

  • Bug Fixes

    • Chat completion requests now preserve an explicitly provided token limit of 0.
    • Requests without a token limit continue to use the default value.
  • Tests

    • Added regression coverage for both explicit zero values and default fallback behavior.

@weizhoublue
weizhoublue requested a review from a team as a code owner August 7, 2026 07:31
@copy-pr-bot

copy-pr-bot Bot commented Aug 7, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@weizhoublue
weizhoublue temporarily deployed to external_collaborator August 7, 2026 07:31 — with GitHub Actions Inactive
@weizhoublue
weizhoublue temporarily deployed to external_collaborator August 7, 2026 07:31 — with GitHub Actions Inactive
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

👋 Hi weizhoublue! Thank you for contributing to ai-dynamo/dynamo.

Just a reminder: The NVIDIA Test Github Validation CI runs an essential subset of the testing framework to quickly catch errors.Your PR reviewers may elect to test the changes comprehensively before approving your changes.

🚀

@github-actions github-actions Bot added external-contribution Pull request is from an external contributor fix frontend `python -m dynamo.frontend` and `dynamo-run in=http|text|grpc` labels Aug 7, 2026

@devin-ai-integration devin-ai-integration Bot 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.

Devin Review found 1 potential issue.

Open in Devin Review

Comment thread lib/llm/src/http/service/openai.rs Resolved
@coderabbitai

coderabbitai Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

The OpenAI chat completion template now applies max_completion_tokens only when the request omits the field. A regression test verifies explicit zero preservation and fallback behavior.

Changes

Completion Token Defaults

Layer / File(s) Summary
Template defaulting and regression coverage
lib/llm/src/http/service/openai.rs
The template preserves explicit max_completion_tokens: 0 values. The regression test verifies zero preservation and defaulting when the field is absent.

Estimated code review effort: 1 (Trivial) | ~3 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the bug and fix, but the reviewer-start section is empty and the required issue section still contains placeholders. Complete the reviewer-start section and select either a real linked issue or the confirmed no-related-issue option.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: preserving an explicit zero value for max_completion_tokens.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
lib/llm/src/http/service/openai.rs (1)

4479-4499: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Verify the rejection path in the regression test.

The test confirms that Some(0) survives template application and that None receives the default. It does not confirm that validation rejects the preserved zero. Add a minimally valid request and assert the expected validation error after applying the template.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lib/llm/src/http/service/openai.rs` around lines 4479 - 4499, The regression
test
test_chat_completions_template_preserves_explicit_zero_max_completion_tokens
should also exercise validation: after applying the template to a minimally
valid request with max_completion_tokens set to Some(0), invoke the existing
request-validation path and assert the expected rejection error, while
preserving the current assertions for zero preservation and None defaulting.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@lib/llm/src/http/service/openai.rs`:
- Around line 4479-4499: The regression test
test_chat_completions_template_preserves_explicit_zero_max_completion_tokens
should also exercise validation: after applying the template to a minimally
valid request with max_completion_tokens set to Some(0), invoke the existing
request-validation path and assert the expected rejection error, while
preserving the current assertions for zero preservation and None defaulting.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 92346602-2b11-42e1-8e0d-b4c1155f0570

📥 Commits

Reviewing files that changed from the base of the PR and between 027e7ad and 14b2da7.

📒 Files selected for processing (1)
  • lib/llm/src/http/service/openai.rs

@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions

Copy link
Copy Markdown
Contributor

This PR has been closed due to inactivity. If you believe this PR is still relevant, please feel free to reopen it with additional context or information.

@github-actions github-actions Bot closed this Nov 13, 2026

This branch was previously deployed

1 inactive deployment
external_collaborator — 14b2da7e Deployed Aug 7, 2026 by weizhoublue via pr_reminder #11050
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external-contribution Pull request is from an external contributor fix frontend `python -m dynamo.frontend` and `dynamo-run in=http|text|grpc` size/S Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant