tezvyn:

How can you provide a Swagger UI example for a Pydantic body?

AI-drafted, machine-checkedSource: fastapi.tiangolo.comintermediate

Tests whether you know FastAPI generates OpenAPI schema from Pydantic metadata. A strong answer names Field(example=...) for per-field samples and Body(example=...) for the full payload.

WHAT THIS TESTS: Your understanding of how FastAPI translates Pydantic model metadata into OpenAPI schema fields that Swagger UI consumes. The interviewer cares whether you know the difference between runtime defaults and schema-level examples, and whether you reach for native framework hooks rather than fighting the docs frontend. Specifically they want to hear how the example key is injected so that the Try it out section renders realistic pre-filled data.

A GOOD ANSWER COVERS: First, state that Pydantic's Field function is used inside the model to declare extra information and validation metadata, and that this metadata flows into the generated JSON Schema. Second, note that FastAPI's Body dependency accepts an example parameter, which is the idiomatic place to supply a complete example payload for the entire request body so it appears pre-filled in Swagger UI. Third, clarify that these schema examples are not default values; they affect only the interactive documentation without changing runtime validation logic or making fields optional. Fourth, mention that because FastAPI generates the OpenAPI schema automatically, the example values are embedded in the spec that Swagger UI renders, so no manual frontend configuration is needed. Fifth, acknowledge that per-field hints can also be set via Field while Body is preferred for a full object sample.

COMMON WRONG ANSWERS: Using default model values as if they were examples, which accidentally changes validation behavior by making fields optional. Proposing to customize Swagger UI static assets or inject JavaScript to pre-fill fields, which breaks the contract-first approach. Confusing Field from pydantic with Query or Path from fastapi, which are for different parameter types. Suggesting manual OpenAPI schema overrides in the FastAPI constructor when the question asks for the standard model-level approach. Claiming that examples must be set in a Config class without mentioning the simpler Body parameter that FastAPI documents.

LIKELY FOLLOW-UPS: How would you provide multiple named examples for a single endpoint? The interviewer might ask about the difference between the singular example and plural examples parameters in Body, or how Pydantic v2 changed field examples via json_schema_extra. They may also ask how to add descriptions that appear next to fields in Swagger UI, or how to hide internal fields from the generated schema.

ONE CONCRETE EXAMPLE: Define a model class Item with name as a string and price as a float. Inside the model, use Field to attach extra metadata to each attribute. Then in the path operation, declare the request parameter as Annotated Item wrapped in Body, passing a complete dictionary to the example parameter. When you open Swagger UI and click Try it out, the request body editor is pre-populated with those values. If you only need per-field hints, Field is sufficient. If you need a realistic full object with nested relationships, the Body parameter is more convenient and maintainable.

Read the original → fastapi.tiangolo.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.