Prhub

#46966 [Misc] Validate Pooling cache_salt Values

原始 PR 作者 taneem-ibrahim 合并时间 2026-07-04 22:19 文件变更 1 提交数 4 评论 1 代码增减 +15 / -0

执行摘要

为 pooling 请求添加 cache_salt 非空验证

Pooling 请求的 cache_salt 字段未做非空字符串校验,与 chat completions、completions、responses 不一致(参见 PR body 中的可重现示例)。需要统一前端校验策略,在请求进入处理前尽早拒绝非法值。

推荐阅读,可作为 Pydantic 模型校验的参考示例。变更小、风险低,可直接合并。

讨论亮点

Reviewer yewentao256 在 tests/entrypoints/pooling/embed/test_io_processor.py 中建议移除针对该改动的单元测试("We don't need a specific unit test for this small update"),认为变更较小,无需单独测试。该评论未产生进一步讨论,PR 最终未包含测试文件变更。

实现拆解

  1. vllm/entrypoints/pooling/base/protocol.pyPoolingBasicRequestMixin 类中新增 check_cache_salt_support 类方法,使用 Pydantic 的 @model_validator(mode="before") 装饰器在数据解析阶段执行校验。
  2. 校验逻辑:若 cache_salt 字段不为 None,则检查其是否为 str 类型且非空字符串;若校验失败,抛出 VLLMValidationError,错误信息为 "Parameter 'cache_salt' must be a non-empty string if provided."
  3. 该校验应用于所有继承自 PoolingBasicRequestMixin 的请求模型(EmbeddingRequest、ClassificationRequest、ScoringRequest 等),实现一次性覆盖所有 pooling 端点。
  4. 测试方面:原 PR 作者在 tests/entrypoints/pooling/embed/test_io_processor.py 中增加了测试用例,但 reviewer 建议无需独立测试,最终未合并此测试变更。
文件 模块 状态 重要度
vllm/entrypoints/pooling/base/protocol.py 前端 modified 6.44

关键符号

check_cache_salt_support

关键源码片段

vllm/entrypoints/pooling/base/protocol.py core-logic

新增 check_cache_salt_support 校验器,是本 PR 唯一改动文件,核心变更。

# vllm/entrypoints/pooling/base/protocol.py
# 在 PoolingBasicRequestMixin 类中,cache_salt 字段定义之后插入以下校验器:@model_validator(mode="before")
@classmethod
def check_cache_salt_support(cls, data):
    # model_validator(mode="before") 会在数据解析前被调用,
    # 此时 data 可以是原始字典或 Pydantic 模型实例。
    if not isinstance(data, dict):
        # 如果不是 dict(例如已经是模型实例),则跳过校验,
        # 由上层调用方保证数据合法性。
        return data
​
    # 检查 cache_salt:如果提供了值,则必须是 str 且非空
    if data.get("cache_salt") is not None and (
        not isinstance(data["cache_salt"], str) or not data["cache_salt"]
    ):
        raise VLLMValidationError(
            "Parameter 'cache_salt' must be a non-empty string if provided.",
            parameter="cache_salt",
        )
    return data

评论区精华

测试必要性 测试

Reviewer yewentao256 建议移除针对 cache_salt 校验的单元测试,认为变更较小无需单独测试。作者提交了测试代码但未合并。

结论:测试代码被移除,PR 不含测试文件变更。 · 已解决

风险与影响

低风险。新增的 model_validator 仅在有 cache_salt 字段且值为非法类型/空串时触发异常,对原有合法输入无影响。由于校验发生在请求构建阶段(Pydantic validation),不会影响推理性能。但需注意:若下游代码直接构造请求对象而非通过模型校验,则可能绕过该检查。

影响范围限定在 pooling 端点(embedding、classification、scoring/rerank 等),对用户在请求中传入空字符串或非字符串 cache_salt 时,将返回明确的 400 错误而非静默接受。对已有合法 cache_salt 的请求无行为变化。

缺少测试覆盖

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论