text: Return a Response Directly¶
  Warning
  The current page still doesn't have a translation for this language.
  But you can help translating it: Contributing.
  When you create a FastAPI path operation you can normally return any data from it: a
  dict, a
  list, a Pydantic model, a database model, etc.
  By default, FastAPI would automatically convert that return value to JSON using the
  jsonable_encoder explained in JSON Compatible Encoder.
  Then, behind the scenes, it would put that JSON-compatible data (e.g. a
  dict) inside of a
  JSONResponse that would be used to send the response to the client.
  But you can return a
  JSONResponse directly from your path operations.
  It might be useful, for example, to return custom headers or cookies.
  Return a
  Response¶
  In fact, you can return any
  Response or any sub-class of it.
  Tip
  JSONResponse itself is a sub-class of
  Response.
  And when you return a typing import Optional from fastapi import FastAPI from fastapi.encoders import jsonable_encoder from fastapi.responses import JSONResponse from pydantic import BaseModel class Item(BaseModel): title: str timestamp: datetime description: Optional[str] = None app = FastAPI() @app.put("/items/{id}") def update_item(id: str, item: Item): json_compatible_item_data = jsonable_encoder(item) return JSONResponse(content=json_compatible_item_data)
  Technical Details
  You could also use
  from starlette.responses import JSONResponse.
  FastAPI provides the same
  starlette.responses as
  fastapi.responses just as a convenience for you, the developer. But most of the available responses come directly from Starlette. that you want to return an XML response.
  You could put your XML content in a string, put it in a
  Response, and return it:
  from fastapi import FastAPI, as described in Additional Responses in OpenAPI.
  You can see in later sections how to use/declare these custom
  Responses while still having automatic data conversion, documentation, etc.