Skip to content

15498: Enable Pydantic polymorphic_serialization flag on response serialization#15499

Open
valentinDruzhinin wants to merge 4 commits into
fastapi:masterfrom
valentinDruzhinin:feature/15498-pylymorphic_serialization
Open

15498: Enable Pydantic polymorphic_serialization flag on response serialization#15499
valentinDruzhinin wants to merge 4 commits into
fastapi:masterfrom
valentinDruzhinin:feature/15498-pylymorphic_serialization

Conversation

@valentinDruzhinin
Copy link
Copy Markdown
Contributor

@valentinDruzhinin valentinDruzhinin commented May 10, 2026

Feature: Polymorphic Response Model Serialization

Adds support for response_model_polymorphic_serialization parameter to FastAPI path operations, enabling polymorphic serialization when returning subclass instances through base-class-typed response models.
Implements: #15498

What it does:

  • Default behavior (False): When returning a subclass instance, only base class fields are serialized in the response
  • With polymorphic flag (True): Subclass-specific fields are included in the serialized response

Example:

class User(BaseModel):
    name: str

class UserLogin(User):
    password: str

@app.get("/user", response_model=User, response_model_polymorphic_serialization=True)
def get_user():
    return UserLogin(name="pydantic", password="secret")
    # Response includes: {"name": "pydantic", "password": "secret"}

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 10, 2026

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing valentinDruzhinin:feature/15498-pylymorphic_serialization (e41b2af) with master (5d5666b)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (e89a37e) during the generation of this report, so 5d5666b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@valentinDruzhinin valentinDruzhinin force-pushed the feature/15498-pylymorphic_serialization branch from 45728d8 to 91586f3 Compare May 10, 2026 04:28
@valentinDruzhinin valentinDruzhinin force-pushed the feature/15498-pylymorphic_serialization branch from 5f2e8e4 to c398e26 Compare May 10, 2026 04:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant