Schema Extra - Example

Warning

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

But you can help translating it: Contributing.

You can define extra information to go in JSON Schema.

A common use case is to add an example that will be shown in the docs.

There are several ways you can declare extra JSON Schema information.

Pydantic schema_extra

You can declare an example for a Pydantic model using Config and schema_extra, as described in Pydantic’s docs: Schema customization:

  1. from typing import Optional
  2. from fastapi import FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: Optional[str] = None
  8. price: float
  9. tax: Optional[float] = None
  10. class Config:
  11. schema_extra = {
  12. "example": {
  13. "name": "Foo",
  14. "description": "A very nice Item",
  15. "price": 35.4,
  16. "tax": 3.2,
  17. }
  18. }
  19. @app.put("/items/{item_id}")
  20. async def update_item(item_id: int, item: Item):
  21. results = {"item_id": item_id, "item": item}
  22. return results

That extra info will be added as-is to the output JSON Schema.

Field additional arguments

In Field, Path, Query, Body and others you’ll see later, you can also declare extra info for the JSON Schema by passing any other arbitrary arguments to the function, for example, to add an example:

  1. from typing import Optional
  2. from fastapi import FastAPI
  3. from pydantic import BaseModel, Field
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str = Field(..., example="Foo")
  7. description: Optional[str] = Field(None, example="A very nice Item")
  8. price: float = Field(..., example=35.4)
  9. tax: Optional[float] = Field(None, example=3.2)
  10. @app.put("/items/{item_id}")
  11. async def update_item(item_id: int, item: Item):
  12. results = {"item_id": item_id, "item": item}
  13. return results

Warning

Have in mind that those extra arguments passed won’t add any validation, only annotation, for documentation purposes.

Body additional arguments

The same way you can pass extra info to Field, you can do the same with Path, Query, Body, etc.

For example, you can pass an example for a body request to Body:

  1. from typing import Optional
  2. from fastapi import Body, FastAPI
  3. from pydantic import BaseModel
  4. app = FastAPI()
  5. class Item(BaseModel):
  6. name: str
  7. description: Optional[str] = None
  8. price: float
  9. tax: Optional[float] = None
  10. @app.put("/items/{item_id}")
  11. async def update_item(
  12. item_id: int,
  13. item: Item = Body(
  14. ...,
  15. example={
  16. "name": "Foo",
  17. "description": "A very nice Item",
  18. "price": 35.4,
  19. "tax": 3.2,
  20. },
  21. ),
  22. ):
  23. results = {"item_id": item_id, "item": item}
  24. return results

Example in the docs UI

With any of the methods above it would look like this in the /docs:

Schema Extra - Example - 图1

Technical Details

About example vs examples

JSON Schema defines a field examples in the most recent versions, but OpenAPI is based on an older version of JSON Schema that didn’t have examples.

So, OpenAPI defined its own example for the same purpose (as example, not examples), and that’s what is used by the docs UI (using Swagger UI).

So, although example is not part of JSON Schema, it is part of OpenAPI, and that’s what will be used by the docs UI.

Other info

The same way, you could add your own custom extra info that would be added to the JSON Schema for each model, for example to customize a frontend user interface, etc.