Creating issue for completeness.
Fixed by #14453
Discussed in #14451
Originally posted by coltonevansid December 4, 2025
First Check
Commit to Help
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:
- Routes with response models that have computed fields
- 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.
Creating issue for completeness.
Fixed by #14453
Discussed in #14451
Originally posted by coltonevansid December 4, 2025
First Check
Commit to Help
Example Code
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 aKeyErrorif the FastAPI app contains both:Expected Behavior
OpenAPI schema should generate successfully with computed fields included in models that have them, regardless of mixing route types.
Current Behavior
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.