Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(proxy): strip every TypedDict qualifier before numeric form-field detection

Yuneng Jiang2042364fc297
diff --git a/litellm/proxy/common_utils/http_parsing_utils.py b/litellm/proxy/common_utils/http_parsing_utils.py
index 552d1ea434f..a396e543e94 100644
--- a/litellm/proxy/common_utils/http_parsing_utils.py
+++ b/litellm/proxy/common_utils/http_parsing_utils.py
@@ -2,11 +2,11 @@ import json
import re
from collections.abc import Collection, Mapping
from types import MappingProxyType, UnionType
-from typing import Any, Final, Union, get_args, get_origin
+from typing import Annotated, Any, Final, Union, get_args, get_origin
import orjson
from fastapi import Request, UploadFile, status
-from typing_extensions import ReadOnly
+from typing_extensions import NotRequired, ReadOnly, Required
from litellm._logging import verbose_proxy_logger
from litellm.constants import MAX_REQUEST_BODY_SIZE_TO_REPAIR_MB
@@ -18,6 +18,8 @@ from litellm.types.router import Deployment
_FORM_CONTENT_TYPES: Final[frozenset[str]] = frozenset({"application/x-www-form-urlencoded", "multipart/form-data"})
+_ANNOTATION_QUALIFIERS: Final[frozenset[object]] = frozenset({Annotated, NotRequired, ReadOnly, Required})
+
def _normalize_media_type(content_type: str) -> str:
"""Return the bare media type per RFC 7231: strip params, trim, lowercase."""
@@ -42,9 +44,17 @@ def _is_json_content_type(content_type: str) -> bool:
return _normalize_media_type(content_type) == "application/json"
+def _unqualified(annotation: object) -> object:
+ """Which qualifiers ``get_type_hints`` already stripped varies by interpreter version, so peel them all."""
+ if get_origin(annotation) not in _ANNOTATION_QUALIFIERS:
+ return annotation
+ qualified: Final[tuple[object, ...]] = get_args(annotation)
+ return _unqualified(qualified[0])
+
+
def _numeric_form_type(annotation: object) -> type[int] | type[float] | None:
"""The scalar to parse an ``int``/``float``-typed field as, else ``None``."""
- unwrapped: Final = get_args(annotation)[0] if get_origin(annotation) is ReadOnly else annotation
+ unwrapped: Final = _unqualified(annotation)
candidates: Final = (
tuple(arg for arg in get_args(unwrapped) if arg is not type(None))
if get_origin(unwrapped) in (Union, UnionType)
diff --git a/tests/test_litellm/proxy/common_utils/test_http_parsing_utils.py b/tests/test_litellm/proxy/common_utils/test_http_parsing_utils.py
index fcfb9342176..011571a37e0 100644
--- a/tests/test_litellm/proxy/common_utils/test_http_parsing_utils.py
+++ b/tests/test_litellm/proxy/common_utils/test_http_parsing_utils.py
@@ -1053,6 +1053,8 @@ class TestNumericFormFields:
read_only: ReadOnly[int | None]
not_required: NotRequired[ReadOnly[int]]
required: Required[ReadOnly[Annotated[float, "meta"]]]
+ read_only_not_required: ReadOnly[NotRequired[int]]
+ read_only_required: ReadOnly[Required[float]]
assert dict(numeric_form_fields(get_type_hints(Schema))) == {
"plain": int,
@@ -1061,6 +1063,22 @@ class TestNumericFormFields:
"read_only": int,
"not_required": int,
"required": float,
+ "read_only_not_required": int,
+ "read_only_required": float,
+ }
+
+ def test_qualifiers_are_unwrapped_when_get_type_hints_keeps_extras(self):
+ from typing_extensions import Annotated, NotRequired, ReadOnly, Required, TypedDict
+
+ class Schema(TypedDict, total=False):
+ annotated: ReadOnly[Annotated[int, "meta"]]
+ not_required: NotRequired[ReadOnly[int]]
+ required: Required[ReadOnly[Annotated[float, "meta"]]]
+
+ assert dict(numeric_form_fields(get_type_hints(Schema, include_extras=True))) == {
+ "annotated": int,
+ "not_required": int,
+ "required": float,
}
def test_non_scalar_and_bool_fields_are_skipped(self):