From 1bf149f3348e4ac0283386c914c1aa15a8ce6e09 Mon Sep 17 00:00:00 2001 From: Muhammad Fawaz Date: Tue, 23 Jun 2026 18:20:50 +0500 Subject: [PATCH] Filter Pydantic-internal markers from validation error param (#46457) Signed-off-by: muhammadfawaz1 <135441198+muhammadfawaz1@users.noreply.github.com> Co-authored-by: Mahad Rehmann <114791389+mahadrehmann@users.noreply.github.com> --- .../serve/utils/test_server_utils.py | 39 +++++++++- vllm/entrypoints/serve/utils/server_utils.py | 78 ++++++++++++++++++- 2 files changed, 115 insertions(+), 2 deletions(-) diff --git a/tests/entrypoints/serve/utils/test_server_utils.py b/tests/entrypoints/serve/utils/test_server_utils.py index 9189698613..4f18e54d6e 100644 --- a/tests/entrypoints/serve/utils/test_server_utils.py +++ b/tests/entrypoints/serve/utils/test_server_utils.py @@ -16,7 +16,10 @@ from types import SimpleNamespace import pytest from fastapi.exceptions import RequestValidationError -from vllm.entrypoints.serve.utils.server_utils import validation_exception_handler +from vllm.entrypoints.serve.utils.server_utils import ( + clean_loc_for_param, + validation_exception_handler, +) def _fake_request(log_error_stack: bool = False) -> SimpleNamespace: @@ -63,3 +66,37 @@ class TestValidationErrorParamFallback: body = json.loads(response.body) assert body["error"]["param"] is None + + +class TestCleanLocForParam: + """Guards against PR #1's naive dot-joined `loc` fallback leaking + Pydantic-internal wrapper/union-branch markers into `param`, e.g. + 'body.function-wrap[__log_extra_fields__()].prompt' instead of the + clean 'body.prompt' an API consumer would recognize. + """ + + @pytest.mark.parametrize( + "loc,expected", + [ + (("body", "prompt"), "body.prompt"), + (("body", "messages", 2, "content"), "body.messages.2.content"), + ( + ("body", "function-wrap[__log_extra_fields__()]", "prompt"), + "body.prompt", + ), + (("body", "stop", "str"), "body.stop"), + (("body", "stop", "list[str]"), "body.stop"), + ( + ("body", "prompt", "list[constrained-int]"), + "body.prompt", + ), + ], + ) + def test_strips_internal_markers(self, loc, expected): + assert clean_loc_for_param(loc) == expected + + def test_all_internal_falls_back_to_raw_join(self): + """If every loc segment looks internal, fall back to the raw + dot-join rather than returning an empty string.""" + loc = ("function-wrap[__log_extra_fields__()]",) + assert clean_loc_for_param(loc) == "function-wrap[__log_extra_fields__()]" diff --git a/vllm/entrypoints/serve/utils/server_utils.py b/vllm/entrypoints/serve/utils/server_utils.py index 93a8ef757d..4479c576ad 100644 --- a/vllm/entrypoints/serve/utils/server_utils.py +++ b/vllm/entrypoints/serve/utils/server_utils.py @@ -11,6 +11,7 @@ from contextlib import asynccontextmanager from http import HTTPStatus import pydantic +import regex as re from fastapi import FastAPI, HTTPException, Request from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse @@ -409,6 +410,81 @@ async def http_exception_handler(req: Request, exc: HTTPException): return JSONResponse(err.model_dump(), status_code=exc.status_code) +_BRACKETED_INTERNAL_RE = re.compile(r"[\[\]{}()]") + +# NOTE: this list is pydantic-core's internal schema-kind vocabulary, +# not a stable public API -- it can grow when pydantic-core adds new +# wrapper/validator kinds. To refresh it after a pydantic upgrade: +# 1. Fuzz the validation-error-prone endpoints (e.g. /tokenize, +# /v1/completions, /v1/chat/completions) with deliberately +# malformed values for union-typed and wrapped fields (e.g. `stop`, +# `prompt`), and inspect the raw `loc` tuples in the response. +# 2. Any *unbracketed* segment that isn't a real field name or list +# index is a new internal marker -- add it here. Bracketed/ +# parenthesized markers (e.g. "list[...]", "function-wrap[...]") +# are already caught structurally by _BRACKETED_INTERNAL_RE and +# don't need a list entry. +# 3. pydantic-core's source (the `error.rs`/schema-kind definitions +# in the pydantic-core Rust crate) is the canonical reference if +# you want to check before it shows up in a live fuzz run. +_INTERNAL_LOC_MARKERS = frozenset( + { + "function-wrap", + "function-after", + "function-before", + "function-plain", + "json-or-python", + "lax-or-strict", + "chain", + "default", + "nullable", + "tagged-union", + "union", + "call", + "arguments", + "is-instance", + "is-subclass", + "callable", + "str", + "int", + "float", + "bool", + "bytes", + "bytearray", + "list", + "tuple", + "dict", + "set", + "frozenset", + "complex", + "none", + "nonetype", + } +) + + +def _is_internal_loc_segment(segment: str) -> bool: + """True if `segment` is a Pydantic-internal wrapper/union-branch + marker rather than a user-meaningful field name or list index.""" + if _BRACKETED_INTERNAL_RE.search(segment): + return True + return segment.lower() in _INTERNAL_LOC_MARKERS + + +def clean_loc_for_param(loc: tuple) -> str: + """Join a Pydantic error `loc` tuple into a clean dotted `param` + path, dropping internal wrapper/union-branch markers that don't + correspond to a real field name an API consumer would recognize. + + E.g. ('body', 'function-wrap[__log_extra_fields__()]', 'prompt') + -> "body.prompt", not "body.function-wrap[__log_extra_fields__()].prompt". + """ + parts = [str(p) for p in loc if not _is_internal_loc_segment(str(p))] + if not parts: + return ".".join(str(p) for p in loc) + return ".".join(parts) + + async def validation_exception_handler(req: Request, exc: RequestValidationError): if req.app.state.args.log_error_stack: logger.exception( @@ -431,7 +507,7 @@ async def validation_exception_handler(req: Request, exc: RequestValidationError first_error = errors[0] loc = first_error.get("loc") if isinstance(first_error, dict) else None if loc: - param = ".".join(str(part) for part in loc) + param = clean_loc_for_param(loc) exc_str = str(exc) errors_str = str(errors)