Skip to content

[#22232][fix] Reject an empty stop string in SamplingParams - #22237

Merged
lori-ren merged 1 commit into
NVIDIA:mainfrom
sohailshk:fix/22232-reject-empty-stop-string
Nov 18, 2026
Merged

lori-ren merged 1 commit into
NVIDIA:mainfrom
sohailshk:fix/22232-reject-empty-stop-string

Conversation

@sohailshk

@sohailshk sohailshk commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #22232

An empty stop string tokenizes to no tokens, but SamplingParams accepted it. By code reading: _get_stop_words() then passes a stop word with no tokens to the executor, and FinishReasonsHandler.prepare_for_new_request raises "Stop words must contain at least one token" while the PyTorch sampler sets the request up; the sampler sets new requests up inside TorchSampler.sample_async, and PyExecutor._sample_async answers the error with _handle_errors(error_msg) without a request list (_setup_sampler_step does the same for disaggregated generation requests): every active request fails and the executor's error budget is charged (an exhausted budget is treated as fatal). The raise was added by #20679 (2026-10-26); TorchSampler.validate_request, which fails only the offending request at admission, does not check stop words.

SamplingParams._validate now raises ValueError("stop must not contain an empty string") for stop="" and for an empty entry in a list, as vLLM does. The LLM API raises when the params are built, and the serve routes, which already map ValueError to 400, reject the request before it reaches the engine. None, [] and non-empty strings, including " ", are unaffected. With ignore_eos=True the stop words are not sent to the engine, but the detokenizing result handler still searches the text for each stop string and "" matches at position 0, so such a request returned an empty completion with finish_reason: "stop" (code reading); it is rejected now as well. Strings that a tokenizer encodes to no tokens are not covered; the admission hook would be the engine-side place.

Test Coverage

Added to tests/unittest/llmapi/test_sampling_params.py (cpu_only, already listed in l0_cpu.yml): test_sampling_params_rejects_empty_stop_string ("", [""], ["a", ""]), test_sampling_params_accepts_non_empty_stop_strings (None, [], "a", ["a"], ["a", "b"], [" "]; all six also pass on the base branch) and test_serve_requests_reject_empty_stop_string (completion and chat requests with "" and [""]). 13 cases; on the base branch 7 fail and 6 pass, with the change all 13 pass.

The test file itself imports BaseLLM and TorchLlmArgs and cannot be imported on macOS without the compiled bindings, so the four new tests were copied verbatim into a scratch module and run on macOS against the real SamplingParams, CompletionRequest and ChatCompletionRequest (bindings stubbed). The sampler-side failure described above is from reading the code; the executor was not run (no CUDA), and the CUDA test suite was not run.

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.

Dev Engineer Review

  • SamplingParams._validate (tensorrt_llm/sampling_params.py) now raises ValueError("stop must not contain an empty string") when stop is "" or a list with an empty entry. The check runs before logprobs_mode is normalized. The diff confirms this placement.
  • The check covers only the literal empty string. Strings that a tokenizer encodes to no tokens are not covered. The linked issue #22232 names this as a possible follow-up at engine admission.
  • The check does not alter ignore_eos handling, _setup, or the sampler. The fix is a fail-fast guard at request construction. It does not change the sampler-side failure path that the issue describes.
  • Serve routes should return HTTP 400 through the existing ValueError handling. This path is not exercised by the changed code alone and was not verified in this review.
  • No unintended scope changes appear in the two changed files.

QA Engineer Review

Test files changed in tests/unittest/llmapi/test_sampling_params.py (+26 lines):

  • test_sampling_params_rejects_empty_stop_string: parametrized over "", [""], and ["a", ""]. Expects ValueError with the new message.
  • test_sampling_params_accepts_non_empty_stop_strings: parametrized over None, [], "a", ["a"], ["a", "b"], and [" "]. Expects the value to be stored unchanged.
  • test_serve_requests_reject_empty_stop_string: parametrized over CompletionRequest and ChatCompletionRequest with stop set to "" and [""]. Expects to_sampling_params(backend="pytorch") to raise.

Coverage verdict: needs follow-up. The tests cover the empty-string paths and the accepted non-empty cases. They do not cover serve-route HTTP 400 responses, the sampler-side failure, or the tokenizer-encoded-to-no-token case. The test-db (CI) and qa list entries for this test file were not checked in this review. The PR author reports the new tests pass on the change and fail on the base branch. The author also reports the test file could not be run in full on macOS. The CUDA suite was not run.

Per-File QA Perspective

  • tensorrt_llm/sampling_params.py: Verify that SamplingParams(stop="") and SamplingParams(stop=[..., ""]) raise ValueError at construction. Verify that None, [], and whitespace-only stops such as " " remain accepted.
  • tests/unittest/llmapi/test_sampling_params.py: Covers the validation rules and the completion and chat request conversion for empty stop strings. Confirm the file is listed in the appropriate test-db/ CI list. That listing was not verified here.

An empty stop string tokenizes to no tokens. The PyTorch sampler rejects a
stop word without tokens ("Stop words must contain at least one token")
while it sets up a new request, and that error path fails all active
requests rather than only the one that sent the stop string.

Reject the empty string when SamplingParams is built, as vLLM does, so the
LLM API raises ValueError and the serve routes answer 400.

Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>
@sohailshk
sohailshk requested a review from a team as a code owner November 13, 2026 06:59
@coderabbitai

coderabbitai Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

SamplingParams now rejects empty strings in stop values, including entries in lists. Tests cover accepted values and Completion and Chat Completion request conversion with the PyTorch backend.

Changes

Stop Validation

Layer / File(s) Summary
Stop validation and request coverage
tensorrt_llm/sampling_params.py, tests/unittest/llmapi/test_sampling_params.py
SamplingParams._validate raises ValueError when a string or list contains an empty stop string. Tests cover rejected and accepted values, and verify that Completion and Chat Completion requests with empty stop values raise ValueError during PyTorch sampling parameter conversion.

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

Change: Bug fix · Severity of issue fixed: High

Merge Risk: Minimal · up to 68cdc

Empty stop strings are now rejected with a clear ValueError, and the serve request paths reach that check. Other stop values keep their current behavior. No merge-blocking risk is evident.

Security architecture risk: Minimal · up to 68cdc

Empty stop strings are rejected earlier, and the checked API paths return a client error before generation starts. The change narrows accepted input without adding access or authority. No material security regression was identified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — For empty stop inputs on the checked Completion and Chat paths, rejection is confined to the offending request before generation submission. This reduces malformed-input reachability into shared execution; it does not establish the behavior of every ingress path.

Trust Boundaries and Controls

  • observed — Client-controlled stop values cross the request-conversion boundary into constructor validation. Chat catches ValueError, Completion catches Exception, and their error-response helper defaults to HTTP 400. The new error message is fixed rather than reflecting the supplied stop value.

Resilience and Maintainability Implications

  • observed — Existing pickle restoration assigns stored fields without rerunning constructor validation. This PR does not change that path, so its new guard should not be described as validation of restored state. No attacker-controlled restore ingress was established.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: SamplingParams now rejects an empty stop string. It follows the repository template with an issue reference, a type tag, and a summary. The title does not mention the …
Description check Passed The description follows the repository template in substance. It explains the problem and the fix, and it lists the test coverage with the test file, the test names, and the results on the base branch…
Linked Issues check Passed Directly linked issue #22232 requires a clear ValueError when stop is "" or contains an empty entry, with HTTP 400 on serve routes. The whole-PR diff (merge base to head) adds a check in SamplingParam…
Out of Scope Changes check Passed The whole-PR diff changes only tensorrt_llm/sampling_params.py (+8 lines, the empty-stop check) and the test file tests/unittest/llmapi/test_sampling_params.py (new tests). Both directly implement the…
Full details: Title check

Explanation

The title names the main change: SamplingParams now rejects an empty stop string. It follows the repository template with an issue reference, a type tag, and a summary. The title does not mention the serve-route or test additions, which is acceptable.

Full details: Description check

Explanation

The description follows the repository template in substance. It explains the problem and the fix, and it lists the test coverage with the test file, the test names, and the results on the base branch and the PR branch. It also states what is not covered and what was not run. The template comment blocks are left as-is, and the checklist box is ticked. The description is mostly complete and clear.

Full details: Linked Issues check

Explanation

Directly linked issue #22232 requires a clear ValueError when stop is "" or contains an empty entry, with HTTP 400 on serve routes. The whole-PR diff (merge base to head) adds a check in SamplingParams._validate in tensorrt_llm/sampling_params.py. It raises ValueError("stop must not contain an empty string") when stop is "" or a list contains "". None, [], non-empty strings, and " " pass through. The PR adds tests in tests/unittest/llmapi/test_sampling_params.py for the empty-string rejection, the accepted cases, and completion and chat serve requests. The serve HTTP 400 path relies on the existing ValueError-to-400 mapping, as the PR description states. I did not inspect the serve route code, so that mapping is taken from the PR description. The test file was not run in CI here. The tokenizer-dependent case (strings that encode to no tokens) is excluded by #22232 and is not required here.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff changes only tensorrt_llm/sampling_params.py (+8 lines, the empty-stop check) and the test file tests/unittest/llmapi/test_sampling_params.py (new tests). Both directly implement the linked objective. No unrelated changes were found. The PR description's checklist and test notes are documentation of the change, not code changes.

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 486bf35 and 68cdc6a.

Files selected for processing: 2

  • tensorrt_llm/sampling_params.py
  • tests/unittest/llmapi/test_sampling_params.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.

@svc-trtllm-gh-bot svc-trtllm-gh-bot added the Community want to contribute PRs initiated from Community label Nov 13, 2026

Copy link
Copy Markdown
Contributor

/bot run --extra-stage "CPU-Generic-x86-1, CPU-Generic-x86-2, CPU-Generic-x86-3, CPU-Generic-x86-4"

@lori-ren
lori-ren enabled auto-merge (squash) November 18, 2026 09:25
@lori-ren lori-ren self-assigned this Nov 18, 2026
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #85747 [ run ] triggered by Bot. Commit: 68cdc6a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #85747 [ run ] completed with state FAILURE. Commit: 68cdc6a
/LLM/main/L0_MergeRequest_PR pipeline #70994 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@lori-ren

Copy link
Copy Markdown
Contributor

/bot run

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #85759 [ run ] triggered by Bot. Commit: 68cdc6a Link to invocation

@github-actions

Copy link
Copy Markdown

Automatically added "ci: full pre-merge approved" because this PR has satisfied the required GitHub review approvals. Unresolved review conversations and other required checks remain independent merge requirements.

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #85759 [ run ] completed with state SUCCESS. Commit: 68cdc6a
/LLM/main/L0_MergeRequest_PR pipeline #71005 completed with status: 'SUCCESS'

CI Report

Link to invocation

@lori-ren
lori-ren merged commit 2c14393 into NVIDIA:main Nov 18, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An empty stop string is accepted by SamplingParams and breaks sampler setup

5 participants