Skip to content

fix(protocols): accept min_tokens=0 in OpenAI requests#1851

Merged
slin1237 merged 1 commit into
lightseekorg:mainfrom
Azure99:main
Jun 27, 2026
Merged

fix(protocols): accept min_tokens=0 in OpenAI requests#1851
slin1237 merged 1 commit into
lightseekorg:mainfrom
Azure99:main

Conversation

@Azure99

@Azure99 Azure99 commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Description

Problem

OpenAI-compatible Chat Completions/Completions requests currently reject min_tokens: 0 at protocol validation time because both request types use #[validate(range(min = 1))].

min_tokens=0 is commonly used as the no-minimum-generation value. vLLM accepts min_tokens >= 0, SGLang documents min_new_tokens: int = 0, and TensorRT-LLM documents that min_tokens < 1 has no effect:

Solution

Allow min_tokens=0 in Chat Completions and legacy Completions protocol validation.

Changes

  • Relax ChatCompletionRequest.min_tokens validation from min=1 to min=0.
  • Relax CompletionRequest.min_tokens validation from min=1 to min=0.
  • Add a Chat Completions spec test covering min_tokens=0.

Test Plan

  • cargo +nightly fmt --all --check - pass
  • cargo test -p smg --test spec_test - pass (96 passed; 0 failed; 1 ignored)
  • cargo clippy --all-targets --all-features -- -D warnings - pass
Checklist
  • cargo +nightly fmt passes
  • cargo clippy --all-targets --all-features -- -D warnings passes
  • (Optional) Documentation updated

Summary by CodeRabbit

  • Bug Fixes
    • Updated request validation to accept 0 for sampling-related minimum settings in both chat and completion requests, instead of requiring values to be at least 1.
  • Tests
    • Added a unit test to cover the min_tokens = 0 edge case and confirm such requests validate successfully.

@github-actions github-actions Bot added tests Test changes protocols Protocols crate changes model-gateway Model gateway crate changes labels Jun 27, 2026
@coderabbitai

coderabbitai Bot commented Jun 27, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 844b7f96-22d9-4cbc-85f6-4244a586eb67

📥 Commits

Reviewing files that changed from the base of the PR and between 50a9c1e and 4f30593.

📒 Files selected for processing (3)
  • crates/protocols/src/chat.rs
  • crates/protocols/src/completion.rs
  • model_gateway/tests/spec/chat_completion.rs

📝 Walkthrough

Walkthrough

Validation for min_tokens and min_p was relaxed to allow zero, and a chat completion test now covers validation success for a zero-valued request field.

Changes

Zero-value sampling validation

Layer / File(s) Summary
Relax sampling validation
crates/protocols/src/chat.rs, crates/protocols/src/completion.rs
The lower bounds on min_tokens and min_p changed from 1 to 0.
Add zero-value validation test
model_gateway/tests/spec/chat_completion.rs
A new test asserts ChatCompletionRequest::validate() succeeds when min_tokens is Some(0) and max_completion_tokens is set.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A tiny zero hopped along,
The validators hummed a softer song.
🐇🥕 A test nibbled once and found it true,
Then green-lit the request right through!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main behavior change: allowing zero minimum tokens in OpenAI request validation.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@mergify

mergify Bot commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Hi @Azure99, the DCO sign-off check has failed. All commits must include a Signed-off-by line.

To fix existing commits:

# Sign off the last N commits (replace N with the number of unsigned commits)
git rebase HEAD~N --signoff
git push --force-with-lease

To sign off future commits automatically:

  • Use git commit -s every time, or
  • VSCode: enable Git: Always Sign Off in Settings
  • PyCharm: enable Sign-off commit in the Commit tool window

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request updates the validation of min_tokens in both chat and completion requests to allow a minimum value of 0 instead of 1, and adds a corresponding test to verify this behavior. The reviewer pointed out that since min_tokens is an unsigned 32-bit integer (u32), the #[validate(range(min = 0))] attribute is redundant and can be safely removed to simplify the code.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +262 to 263
#[validate(range(min = 0))]
pub min_tokens: Option<u32>,

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.

medium

Since min_tokens is of type Option<u32>, the underlying value is an unsigned 32-bit integer, which is guaranteed to be non-negative by definition. Therefore, the #[validate(range(min = 0))] attribute is redundant and can be safely removed to avoid unnecessary validation overhead and simplify the code.

Suggested change
#[validate(range(min = 0))]
pub min_tokens: Option<u32>,
pub min_tokens: Option<u32>,

Comment on lines +99 to 100
#[validate(range(min = 0))]
pub min_tokens: Option<u32>,

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.

medium

Since min_tokens is of type Option<u32>, the underlying value is an unsigned 32-bit integer, which is guaranteed to be non-negative by definition. Therefore, the #[validate(range(min = 0))] attribute is redundant and can be safely removed to avoid unnecessary validation overhead and simplify the code.

Suggested change
#[validate(range(min = 0))]
pub min_tokens: Option<u32>,
pub min_tokens: Option<u32>,

Signed-off-by: Azure99 <961523404@qq.com>
@slin1237 slin1237 merged commit 9ec311c into lightseekorg:main Jun 27, 2026
42 of 45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

model-gateway Model gateway crate changes protocols Protocols crate changes tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants