🐛 Fix unhashable Annotated type in get_definitions OpenAPI schema generation#15429
Open
Yuerchu wants to merge 4 commits intofastapi:masterfrom
Open
🐛 Fix unhashable Annotated type in get_definitions OpenAPI schema generation#15429Yuerchu wants to merge 4 commits intofastapi:masterfrom
Yuerchu wants to merge 4 commits intofastapi:masterfrom
Conversation
… generation
`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) <noreply@anthropic.com>
Add test that directly exercises get_definitions() with an Annotated type containing unhashable metadata (simulating SQLModel extension field-info objects). The test fails on main and passes with the fix. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
YuriiMotov
reviewed
Apr 26, 2026
Comment on lines
+8
to
+9
| """Simulates third-party field metadata (e.g. from SQLModel extensions) | ||
| that does not implement ``__hash__``.""" |
Member
There was a problem hiding this comment.
SQLModel is already in test dependencies, so you can use it to make this test more realistic.
Ideally, it should be a FastAPI app, not just get_definitions
Member
|
Please, review changes carefully before opening PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
get_definitions()infastapi/_compat/v2.pyuses a set comprehension to collect field annotations for deduplicationAnnotatedtypes containFieldInfoMetadataobjects that are not hashable, causing aTypeErrorwhen generating the OpenAPI schema:Annotatedparameters with metadata such asQuery(max_length=..., description=...)or similar constructs that produce unhashable Pydantic field metadataFix
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.Changes
Environment