Additional Responses in OpenAPI

    The current page still doesn’t have a translation for this language.

    But you can help translating it: Contributing.

    Warning

    This is a rather advanced topic.

    If you are starting with FastAPI, you might not need this.

    You can declare additional responses, with additional status codes, media types, descriptions, etc.

    Those additional responses will be included in the OpenAPI schema, so they will also appear in the API docs.

    But for those additional responses you have to make sure you return a Response like JSONResponse directly, with your status code and content.

    You can pass to your path operation decorators a parameter responses.

    It receives a dict, the keys are status codes for each response, like 200, and the values are other dicts with the information for each of them.

    Each of those response dicts can have a key model, containing a Pydantic model, just like response_model.

    FastAPI will take that model, generate its JSON Schema and include it in the correct place in OpenAPI.

    For example, to declare another response with a status code 404 and a Pydantic model Message, you can write:

    Have in mind that you have to return the JSONResponse directly.

    Info

    The model key is not part of OpenAPI.

    FastAPI will take the Pydantic model from there, generate the JSON Schema, and put it in the correct place.

    The correct place is:

    • In the key content, that has as value another JSON object (dict) that contains:
      • A key with the media type, e.g. application/json, that contains as value another JSON object, that contains:
        • A key schema, that has as the value the JSON Schema from the model, here’s the correct place.
          • FastAPI adds a reference here to the global JSON Schemas in another place in your OpenAPI instead of including it directly. This way, other applications and clients can use those JSON Schemas directly, provide better code generation tools, etc.

    The generated responses in the OpenAPI for this path operation will be:

    1. {
    2. "responses": {
    3. "404": {
    4. "description": "Additional Response",
    5. "content": {
    6. "application/json": {
    7. "schema": {
    8. "$ref": "#/components/schemas/Message"
    9. }
    10. }
    11. }
    12. },
    13. "200": {
    14. "description": "Successful Response",
    15. "content": {
    16. "application/json": {
    17. "schema": {
    18. "$ref": "#/components/schemas/Item"
    19. }
    20. }
    21. }
    22. },
    23. "422": {
    24. "description": "Validation Error",
    25. "content": {
    26. "application/json": {
    27. "schema": {
    28. }
    29. }
    30. }
    31. }
    32. }

    The schemas are referenced to another place inside the OpenAPI schema:

    You can use this same responses parameter to add different media types for the same main response.

    For example, you can add an additional media type of image/png, declaring that your path operation can return a JSON object (with media type application/json) or a PNG image:

    1. from typing import Union
    2. from fastapi import FastAPI
    3. from fastapi.responses import FileResponse
    4. from pydantic import BaseModel
    5. class Item(BaseModel):
    6. id: str
    7. value: str
    8. app = FastAPI()
    9. @app.get(
    10. "/items/{item_id}",
    11. response_model=Item,
    12. responses={
    13. 200: {
    14. "content": {"image/png": {}},
    15. "description": "Return the JSON item or an image.",
    16. }
    17. },
    18. )
    19. async def read_item(item_id: str, img: Union[bool, None] = None):
    20. if img:
    21. return FileResponse("image.png", media_type="image/png")
    22. else:
    23. return {"id": "foo", "value": "there goes my hero"}

    Note

    Notice that you have to return the image using a FileResponse directly.

    Info

    Unless you specify a different media type explicitly in your responses parameter, FastAPI will assume the response has the same media type as the main response class (default application/json).

    You can also combine response information from multiple places, including the response_model, status_code, and responses parameters.

    You can declare a response_model, using the default status code 200 (or a custom one if you need), and then declare additional information for that same response in responses, directly in the OpenAPI schema.

    FastAPI will keep the additional information from responses, and combine it with the JSON Schema from your model.

    For example, you can declare a response with a status code 404 that uses a Pydantic model and has a custom .

    And a response with a status code 200 that uses your response_model, but includes a custom example:

    It will all be combined and included in your OpenAPI, and shown in the API docs:

    You might want to have some predefined responses that apply to many path operations, but you want to combine them with custom responses needed by each path operation.

    For those cases, you can use the Python technique of “unpacking” a dict with **dict_to_unpack:

    1. "old key": "old value",
    2. "second old key": "second old value",
    3. }
    4. new_dict = {**old_dict, "new key": "new value"}

    Here, new_dict will contain all the key-value pairs from old_dict plus the new key-value pair:

    You can use that technique to re-use some predefined responses in your path operations and combine them with additional custom ones.

    For example:

    1. from typing import Union
    2. from fastapi import FastAPI
    3. from fastapi.responses import FileResponse
    4. from pydantic import BaseModel
    5. class Item(BaseModel):
    6. id: str
    7. value: str
    8. responses = {
    9. 404: {"description": "Item not found"},
    10. 302: {"description": "The item was moved"},
    11. 403: {"description": "Not enough privileges"},
    12. }
    13. app = FastAPI()
    14. @app.get(
    15. "/items/{item_id}",
    16. response_model=Item,
    17. responses={**responses, 200: {"content": {"image/png": {}}}},
    18. )
    19. async def read_item(item_id: str, img: Union[bool, None] = None):
    20. if img:
    21. return FileResponse("image.png", media_type="image/png")
    22. else:
    23. return {"id": "foo", "value": "there goes my hero"}

    To see what exactly you can include in the responses, you can check these sections in the OpenAPI specification:

    • OpenAPI Response Object, you can include anything from this directly in each response inside your responses parameter. Including description, headers, content (inside of this is that you declare different media types and JSON Schemas), and .