# PR #44965 完整报告

- 仓库：`vllm-project/vllm`
- 标题：[Bugfix] Reject out-of-range temperature values in SamplingParams
- 合并时间：2026-06-15 14:12
- 原文链接：http://prhub.com.cn/vllm-project/vllm/pull/44965

---

# 执行摘要

- 一句话：为 SamplingParams.temperature 增加上界检查
- 推荐动作：值得合并，这是一个小而正确的 bugfix，解决了前后端验证不一致的问题。变更仅 6 行，逻辑清晰，已通过 reviewer 审核。

# 功能与动机

PR body 指出 Python 前端 SamplingParams._verify_args 仅检查 temperature 下界（>= 0），而 Rust 前端通过 #[validate(range(min = 0.0, max = 2.0))] 在 ChatCompletionRequest.temperature 上已强制 OpenAI 规范 [0,2]。此 gap 导致 temperature=3 等值可静默流入引擎并产生正常响应，与规范不符。

# 实现拆解

步骤 1：在 `vllm/sampling_params.py` 的 `_verify_args` 方法中，在已有的下界检查 `if self.temperature < 0.0` 之后、top_p 检查之前，新增一个独立的上界检查分支：`if self.temperature > 2.0: raise VLLMValidationError(...)`。

步骤 2：错误消息统一为 `f"temperature must be in [0, 2], got {self.temperature}."`，与 Rust 端语义对齐，并附带 `parameter="temperature"` 和 `value=self.temperature` 参数。

步骤 3：测试验证（在 REPL 中手动完成），确认 in-range 值通过、out-of-range 值抛出异常。应 reviewer 要求，将原先包含的单元测试移除。

关键文件：
- `vllm/sampling_params.py`（模块 采样参数；类别 source；类型 core-logic；符号 _verify_args）: 这是本次 PR 唯一变更的文件，在 _verify_args 方法中新增 temperature 上界检查。

关键符号：SamplingParams._verify_args

## 关键源码片段

### `vllm/sampling_params.py`

这是本次 PR 唯一变更的文件，在 _verify_args 方法中新增 temperature 上界检查。

```python
# vllm/sampling_params.py
# 在 _verify_args 方法中，已有的下界检查之后、top_p 检查之前，新增上界检查
if self.temperature < 0.0:
    raise VLLMValidationError(
        f"temperature must be non-negative, got {self.temperature}.",
        parameter="temperature",
        value=self.temperature,
    )
# 新增：拒绝大于 2.0 的 temperature，与 Rust 端 OpenAI 规范对齐
if self.temperature > 2.0:
    raise VLLMValidationError(
        f"temperature must be in [0, 2], got {self.temperature}.",
        parameter="temperature",
        value=self.temperature,
    )

```

# 评论区精华

Reviewer DarkLight1337 评论 "Seems reasonable to me as well, but please remove the tests as they aren't really that useful"，作者据此删除了单元测试。可见 reviewer 认为该变更逻辑简单，无需额外测试覆盖。

- 单元测试是否必要 (testing): 作者删除测试，reviewer 随后批准

# 风险与影响

- 风险：低风险。变更仅新增一个简单的 if 检查，不涉及核心计算路径或性能敏感代码。唯一风险是：如果用户之前依赖 temperature>2.0 的非标准行为（例如某些自定义采样策略），应用将在升级后抛出异常。但这是预期行为，且与 OpenAI 规范对齐。
- 影响：用户层面：调用 SamplingParams(temperature=...) 时，超过 2.0 的值将立即收到明确的 VLLMValidationError，而不是静默使用越界值。系统层面：无性能影响，无配置变更需求。团队层面：修复了前后端验证不一致的问题，降低了未来因参数越界导致奇怪行为的可能性。
- 风险标记：低风险

# 关联脉络

- 暂无明显关联 PR