Skip to content

Computed fields support breaks with mixed route types #14467

Description

@tiangolo

Creating issue for completeness.

Fixed by #14453

Discussed in #14451

Originally posted by coltonevansid December 4, 2025

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 to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from pydantic import BaseModel, computed_field

class ModelWithComputedField(BaseModel):
    name: str

    @computed_field
    @property
    def computed_name(self) -> str:
        return f"computed_{self.name}"

class SimpleModel(BaseModel):
      value: str

app = FastAPI()

@app.get("/with-computed")
def route_with_computed() -> ModelWithComputedField:
    """Route returning a model with computed fields."""
    return ModelWithComputedField(name="test")

@app.get("/without-computed")
def route_without_computed() -> SimpleModel:
    """Route returning a simple model (no computed fields)."""
    return SimpleModel(value="test")


if __name__ == "__main__":
    # This will raise KeyError when both routes are present
    schema = get_openapi(
        title="Test App",
        version="1.0.0",
        routes=app.routes,
        separate_input_output_schemas=False,  # Bug only occurs with False
    )

Description

FastAPI 0.123.4 added support for computed fields in OpenAPI schemas (#13207) when separate_input_output_schemas=False, but the implementation has a bug where the schema generation crashes with a KeyError if the FastAPI app contains both:

  1. Routes with response models that have computed fields
  2. Routes with response models that don't have computed fields (e.g., dict responses or BaseModels without computed fields)

Expected Behavior

OpenAPI schema should generate successfully with computed fields included in models that have them, regardless of mixing route types.

Current Behavior

File "../python3.13/site-packages/fastapi/_compat/v2.py", line 192, in get_schema_from_model_field
  json_schema = field_mapping[(field, override_mode or field.mode)]
                ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: (ModelField(field_info=FieldInfo(annotation=SimpleModel, required=True), name='Response_route_without_computed_without_computed_get', mode='serialization'), 'validation')

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.123.4

Pydantic Version

2.12.4

Python Version

3.13.8

Additional Context

Workaround: Generate schemas separately for routes with and without computed fields, then merge them.

Looking at the PR that introduced this (#13207), the tests don't cover the scenario of mixing routes with and without computed fields, which is why this bug wasn't caught.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions