First Check
Commit to Help
Example Codefrom collections.abc import AsyncIterable
from fastapi import APIRouter
from fastapi.sse import EventSourceResponse
from pydantic import BaseModel
sse_router = APIRouter(tags=["stream"])
class ExampleEvent(BaseModel):
message: str
id: int
@sse_router.post(
"/",
response_class=EventSourceResponse,
)
async def sse_endpoint() -> AsyncIterable[ExampleEvent]:
for i in range(10):
yield ExampleEvent(message=f"Hello {i}", id=i)DescriptionWith the sse endpoint above, the generated openapi definition does not contain the yielded event type Operating SystemWindows Operating System DetailsNo response FastAPI Version0.135.1 Pydantic Version2.12.5 Python Version3.14.2 Additional ContextI also tried attach the event type via it actually included the ExampleEvent in the openapi definition, but it also includes an extra |
Replies: 4 comments 1 reply
|
Update: I noticed that the |
|
I hit the same thing and I believe the root cause is from collections.abc import AsyncIterable
from fastapi import APIRouter, FastAPI
from fastapi.sse import EventSourceResponse
from pydantic import BaseModel
class ExampleEvent(BaseModel):
message: str
id: int
app = FastAPI()
sse_router = APIRouter()
@app.post("/direct", response_class=EventSourceResponse)
async def direct() -> AsyncIterable[ExampleEvent]:
for i in range(10):
yield ExampleEvent(message=f"Hello {i}", id=i)
@sse_router.post("/routed", response_class=EventSourceResponse)
async def routed() -> AsyncIterable[ExampleEvent]:
for i in range(10):
yield ExampleEvent(message=f"Hello {i}", id=i)
app.include_router(sse_router)
"itemSchema": {
"type": "object",
"properties": {
"data": {
"type": "string",
"contentMediaType": "application/json",
"contentSchema": { "$ref": "#/components/schemas/ExampleEvent" }
},
"event": { "type": "string" },
"id": { "type": "string" },
"retry": { "type": "integer", "minimum": 0 }
},
"required": ["data"]
}
"itemSchema": {
"type": "object",
"properties": {
"data": { "type": "string" },
"event": { "type": "string" },
"id": { "type": "string" },
"retry": { "type": "integer", "minimum": 0 }
}
}So the missing schema is likely not an orval issue -- it looks like FastAPI itself generates an incomplete |
I hit the same thing and I believe the root cause is
include_router(). Same endpoint, different schema depending on how it's registered: