Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

OpenAPI generation with typing.Self with pydantic v2, regression in 0.119#14341

Unanswered
jor-rit asked this question inQuestions
Discussion options

First Check

  • I added a very descriptive title here.
  • I used the GitHub search to find a similar question and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but toPydantic.
  • I already checked if it is not related to FastAPI but toSwagger UI.
  • I already checked if it is not related to FastAPI but toReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

#!/usr/bin/env -S uv run --script# /// script# requires-python = ">=3.12"# dependencies = [#     "fastapi==0.118.3",#     "pydantic>=2.9.0",# ]# ///importjsonimporttypingfromfastapiimportFastAPIfrompydanticimportBaseModel# Model using typing.Self for recursive structureclassNode(BaseModel):id:strchildren:list[typing.Self]# FastAPI applicationapp=FastAPI()@app.get("/node",response_model=Node)defget_node():"""Get a node with children."""returnNode(id="root",children=[Node(id="child1",children=[]),Node(id="child2",children=[])        ]    )if__name__=="__main__":try:pydantic_schema=Node.model_json_schema()print(json.dumps(pydantic_schema,indent=2))exceptExceptionase:print(f"✗ Pydantic failed:{e}")openapi_schema=app.openapi()print(json.dumps(openapi_schema,indent=2))

Description

Generating the OpenAPI specification with a model that uses a field withtyping.Self raises an error:
pydantic.errors.PydanticUserError:typing.Self is invalid in this context

This used to work, in FastAPI 0.118.x . This broke in 0.119 (and still broken in 0.121.1), where pydantic v1 compat code was introduced.

Generating the JSON schema using pydantic words, but not when it gets called through the fastAPI compat code.

Not that hard to workaround, by specifying the exact type or using generics, but we loose the nice subclass-ability and readability of Self.

Operating System

Linux

Operating System Details

No response

FastAPI Version

2.12.4

Pydantic Version

0.121.0

Python Version

Python 3.14.0

Additional Context

Run the example code with FastAPI 0.118.3 and you get a working OpenAPI spec.

You must be logged in to vote

Replies: 1 comment

This comment was marked as disruptive content.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
questionQuestion or problem
2 participants
@jor-rit@engripaye

[8]ページ先頭

©2009-2025 Movatter.jp