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>
This commit is contained in:
Muhammad Fawaz
2026-06-23 13:20:50 +00:00
committed by GitHub
co-authored by Mahad Rehmann
parent 2a675a7b9f
commit 1bf149f334
2 changed files with 115 additions and 2 deletions
+77 -1
View File
@@ -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)