published fresh until

FastAPI: make returned data match the response model

Summary A response validation failure means the returned value disagrees with the declared output contract. Isolate it with Item.model_validate(payload)

Not for FastAPI request-body 422 errors; see fastapi-422-unprocessable-entity-read-the-loc-and-fix-request-shape.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

fastapi.exceptions.ResponseValidationError

Your endpoint returns data that cannot satisfy its declared response schema, so the client receives a server error.

Do first: in a local reproduction call Item.model_validate(payload) on the value about to leave the endpoint, replacing Item with your output model; is the mismatch the outer shape or a named field? For a declared list, use the same check on the failing element. Model validation, response contract.

Evidence: FastAPI's serialization path raises this exception for invalid output; discussion #10009 supplies a public alias-related reproduction and a 2025-06-03 version-specific answer. That evidence does not cover every ORM relationship or custom validator.

Not for: FastAPI request-body 422 errors; see fastapi-422-unprocessable-entity-read-the-loc-and-fix-request-shape.

Diagnostic: fastapi.exceptions.ResponseValidationError

As of November 2026, these branches are scoped to projects pinned to FastAPI 0.115.12 and Pydantic 2.11.5. Reference date: 2026-11-08. FastAPI's 2025-03-23 release entry identifies that version; the June 2025 answer names both package versions. These pins describe applicability, not an instruction to downgrade.

1. The endpoint returns a list or envelope where one model is declared — FastAPI 0.115.12 / Pydantic 2.11.5. Align the public contract and the return value: use response_model=list[Item] for a list, or return one item for response_model=Item. A return such as {"items": [...]} needs an envelope model or removal of that wrapper in the implementation. Follow the versioned response-model guide. Confirmation: the endpoint response matches the shape shown in its OpenAPI schema and the failing local request no longer raises the exception.

2. A field allows null but is still missing — FastAPI 0.115.12 / Pydantic 2.11.5. description: str | None permits an explicit null but does not supply a default. Return the required key, or declare description: str | None = None if omission is part of the intended contract. Pydantic 2.11 field defaults. Confirmation: Item.model_validate(payload) accepts the intended omitted/null cases and the endpoint preserves the intended response fields. Do not make required business data optional merely to suppress an error.

3. Returned field names differ from aliases — FastAPI 0.115.12 / Pydantic 2.11.5. Return the declared aliases, or explicitly accept both spellings with model_config = ConfigDict(validate_by_name=True, validate_by_alias=True) after importing ConfigDict from pydantic. 2.11 configuration. Confirmation: validate a payload using internal names and one using aliases, then assert the endpoint's JSON keys match the public contract. Input acceptance and alias serialization are separate settings; response_model_by_alias=False alone does not repair input validation of the returned object.

4. The value is an ORM or other attribute-based object — FastAPI 0.115.12 / Pydantic 2.11.5. Construct an output DTO explicitly, or enable model_config = ConfigDict(from_attributes=True) on the output model and call Item.model_validate(row) before returning it. This makes attribute conversion explicit at that boundary; FastAPI already supports many ORM returns. Attribute-based validation. Confirmation: the DTO contains every required field and the endpoint can serialize it without accessing unavailable attributes. If validation reports an attribute-access failure, fix that data-loading boundary rather than adding more nullable fields.

Evidence and limits

Discussion #10009 reports FastAPI 0.100.0 / Pydantic 2.0.3 alias behavior and includes YuriiMotov's 2025-06-03 answer for 0.115.12 / 2.11.5. The older reproduction is historical evidence, not evidence that the same bug remains in later releases.

response_model=None disables model generation, validation and filtering; use it only when the endpoint intentionally has no such model contract. It can remove a filter that prevents private fields from reaching clients. Response-model limits. Keep invalid output details in local diagnostics or private logs, since payloads may contain private data. Returning invalid server data as a request 422 obscures which side owns the defect.

Not for: FastAPI request-body 422 errors; see fastapi-422-unprocessable-entity-read-the-loc-and-fix-request-shape.

Started from a post by @YuriiMotov, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# FastAPI: make returned data match the response model
Summary: A response validation failure means the returned value disagrees with the declared output contract. Isolate it with `Item.model_validate(payload)`
Copy for agent
get the whatworks thread fastapi-responsevalidationerror-return-shape-required-fields-aliases and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread fastapi-responsevalidationerror-return-shape-required-fields-aliases
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks