From 9e69d8b6fec551722e637372c3b2c0c9219da2f6 Mon Sep 17 00:00:00 2001 From: Yuerchu Date: Sun, 26 Apr 2026 01:53:05 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20unhashable=20`Annotated`?= =?UTF-8?q?=20type=20in=20`get=5Fdefinitions`=20OpenAPI=20schema=20generat?= =?UTF-8?q?ion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `get_definitions()` uses a set comprehension to collect field annotations for deduplication. However, on Python 3.14 with Pydantic v2.13+, some `Annotated` types contain `FieldInfoMetadata` objects that are not hashable, causing a `TypeError`: TypeError: cannot use 'typing._AnnotatedAlias' as a set element (unhashable type: 'FieldInfoMetadata') This is triggered when routes use `Annotated` parameters with metadata such as `Query(max_length=..., description=...)` or similar constructs that produce unhashable Pydantic field metadata. Fix: use `id()` for identity-based deduplication instead of relying on `__hash__` of annotation objects. Since annotations are interned type objects, `id()` comparison is semantically equivalent for this filtering purpose. Co-Authored-By: Claude Opus 4.6 (1M context) --- fastapi/_compat/v2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fastapi/_compat/v2.py b/fastapi/_compat/v2.py index 3b64fba76c..a20db0ebbd 100644 --- a/fastapi/_compat/v2.py +++ b/fastapi/_compat/v2.py @@ -317,9 +317,9 @@ def get_definitions( for model in flat_serialization_models ] flat_model_fields = flat_validation_model_fields + flat_serialization_model_fields - input_types = {f.field_info.annotation for f in fields} + input_type_ids = {id(f.field_info.annotation) for f in fields} unique_flat_model_fields = { - f for f in flat_model_fields if f.field_info.annotation not in input_types + f for f in flat_model_fields if id(f.field_info.annotation) not in input_type_ids } inputs = [ (