Skip to content
Discussion options

You must be logged in to vote

You can solve this by adding the explicit type annotation to responses:

from typing import Any
from pydantic import BaseModel

from fastapi import FastAPI


app = FastAPI()


class Message(BaseModel):
    message: str


responses: dict[int | str, dict[str, Any]] = {
    403: {"model": Message, "description": "Not enough privileges"},
    404: {"model": Message, "description": "Item not found"},
}


@app.get("/endpoint1/{item_id}", responses={**responses})
async def endpoint1(item_id: int):
    return {"message": str(item_id)}

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@angely-dev
Comment options

Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
2 participants