Repository navigation
[#22232][fix] Reject an empty stop string in SamplingParams - #22237
Conversation
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>
|
No actionable comments were generated in the recent review. Walkthrough
ChangesStop Validation
Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix · Severity of issue fixed: High Merge Risk:
|
|
/bot run --extra-stage "CPU-Generic-x86-1, CPU-Generic-x86-2, CPU-Generic-x86-3, CPU-Generic-x86-4" |
|
PR_Github #85747 [ run ] triggered by Bot. Commit: |
|
PR_Github #85747 [ run ] completed with state
|
|
/bot run |
|
PR_Github #85759 [ run ] triggered by Bot. Commit: |
|
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. |
|
PR_Github #85759 [ run ] completed with state |
Description
Fixes #22232
An empty stop string tokenizes to no tokens, but
SamplingParamsaccepted it. By code reading:_get_stop_words()then passes a stop word with no tokens to the executor, andFinishReasonsHandler.prepare_for_new_requestraises "Stop words must contain at least one token" while the PyTorch sampler sets the request up; the sampler sets new requests up insideTorchSampler.sample_async, andPyExecutor._sample_asyncanswers the error with_handle_errors(error_msg)without a request list (_setup_sampler_stepdoes 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._validatenow raisesValueError("stop must not contain an empty string")forstop=""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. Withignore_eos=Truethe 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 withfinish_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 inl0_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) andtest_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
BaseLLMandTorchLlmArgsand 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 realSamplingParams,CompletionRequestandChatCompletionRequest(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-compatibleorapi-breaking. Forapi-breaking, includeBREAKINGin 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 raisesValueError("stop must not contain an empty string")whenstopis""or a list with an empty entry. The check runs beforelogprobs_modeis normalized. The diff confirms this placement.#22232names this as a possible follow-up at engine admission.ignore_eoshandling,_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.ValueErrorhandling. This path is not exercised by the changed code alone and was not verified in this review.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", ""]. ExpectsValueErrorwith the new message.test_sampling_params_accepts_non_empty_stop_strings: parametrized overNone,[],"a",["a"],["a", "b"], and[" "]. Expects the value to be stored unchanged.test_serve_requests_reject_empty_stop_string: parametrized overCompletionRequestandChatCompletionRequestwithstopset to""and[""]. Expectsto_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 thatSamplingParams(stop="")andSamplingParams(stop=[..., ""])raiseValueErrorat construction. Verify thatNone,[], 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 appropriatetest-db/CI list. That listing was not verified here.