Is there any way to have multiple response models in FastAPI/OpenAPI?

Viewed 279

I am writing an app where I need to have two completely different set of response structures depending on logic.

Is there any way to handle this so that I can have two different response models serialized, validated and returned and reflect in OpenAPI JSON?

I am using pydantic to write models.

Thank you!

1 Answers

Yes this is possible. You can use Union for that in the response_model= parameter in your path decorator (I used the new python 3.10 style below). Here is a full example, this will work as is.

from typing import Union

from fastapi import FastAPI, Query
from pydantic import BaseModel

class responseA(BaseModel):
    name: str

class responseB(BaseModel):
    id: int

app = FastAPI()

@app.get("/", response_model=responseA|responseB )
def base(q: int|str = Query(None)):
    if q and isinstance(q, str):
        return responseA(name=q)
    if q and isinstance(q, int):
        return responseB(id=q)
    raise HTTPException(status_code=400, detail="No q param provided")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000,  )

Result in your documentation: enter image description here

Related