Repository navigation
response_model_exclude_none=True no longer excludes None for Iterable since 0.134
#15089
First Check
Commit to Help
Example Codefrom collections.abc import Iterable
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
age: int | None = None
@app.get("/foo", response_model_exclude_none=True)
def read_root() -> Iterable[Item]:
return [Item(name="foo", age=42), Item(name="bar")]DescriptionRun the example and visit
If I change Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.133.1, 0.134.0, 0.135.1 Pydantic Version2.12.5 Python Version3.14.2 Additional ContextNo response |
Replies: 3 comments
|
Opened PR to fix this: https://github.com/fastapi/fastapi/pull/15093/changes |
|
I was able to fix the issue by explicitly adding the @app.get("/foo", response_model_exclude_none=True, response_model=Iterable[Item])After comparing the changes between 0.133.1 and 0.134.0 self.stream_item_type: Any | None = None
if isinstance(response_model, DefaultPlaceholder):
return_annotation = get_typed_return_annotation(endpoint)
if lenient_issubclass(return_annotation, Response):
response_model = None
else:
- response_model = return_annotation
+ stream_item = get_stream_item_type(return_annotation)
+ if stream_item is not None:
+ # Only extract item type for JSONL streaming when no
+ # explicit response_class (e.g. StreamingResponse) was set
+ if isinstance(response_class, DefaultPlaceholder):
+ self.stream_item_type = stream_item
+ response_model = None
+ else:
+ response_model = return_annotationBecause of this change, when the return annotation is You can observe this difference with the following example: from collections.abc import Iterable
from fastapi import FastAPI
from pydantic import BaseModel
from fastapi.routing import APIRoute
from fastapi.dependencies.utils import get_typed_return_annotation, get_stream_item_type
app = FastAPI()
class Item(BaseModel):
name: str
age: int | None = None
# Example 1: No explicit response_model
# Response: [{"name": "foo", "age": 42}, {"name": "bar", "age": null}]
@app.get("/foo", response_model_exclude_none=True)
def read_root() -> Iterable[Item]:
return [Item(name="foo", age=42), Item(name="bar")]
# Example 2: Explicit response_model=Iterable[Item]
# Response: [{"name": "foo", "age": 42}, {"name": "bar"}]
@app.get(
"/foo-with-response-model",
response_model_exclude_none=True,
response_model=Iterable[Item],
)
def read_root_with_response_model() -> Iterable[Item]:
return [Item(name="foo", age=42), Item(name="bar")]
# Debug route configuration
for route in app.routes:
if isinstance(route, APIRoute):
return_annotation = get_typed_return_annotation(route.endpoint)
stream_item = get_stream_item_type(return_annotation)
print(f"""
=== Route Info ===
Path: {route.path}
Return annotation: {return_annotation}
Stream item type: {stream_item}
Route stream type: {route.stream_item_type}
Response model: {route.response_model}
------------------------------
""")Output: === Route Info ===
Path: /foo
Return annotation: collections.abc.Iterable[__main__.Item]
Stream item type: <class '__main__.Item'>
Route stream type: <class '__main__.Item'>
Response model: None
------------------------------
=== Route Info ===
Path: /foo-with-response-model
Return annotation: collections.abc.Iterable[__main__.Item]
Stream item type: <class '__main__.Item'>
Route stream type: None
Response model: collections.abc.Iterable[__main__.Item]
------------------------------Edit: Sorry, I didn’t see the message just before that fixed the issue. |
|
Thanks to both! I'll mark the PR as answer since then it will be more clear the issue is (will be) resolved in the future. |
Opened PR to fix this: https://github.com/fastapi/fastapi/pull/15093/changes