Skip to content
tezvyn:
Menu

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

Source: fastapi.tiangolo.comMediumHow cards are made

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's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

You need Swagger UI's Try it out to show a realistic full request body for a Pydantic model without altering endpoint validation. Which approach is idiomatic?

  • a.Pass a complete example dictionary to the Body parameter in the route signatureCorrect
  • b.Write custom JavaScript to inject values into the Swagger UI frontend
  • c.Add the example inside the model's Config class under schema_extra only
  • d.Set default values on the model fields so they appear pre-filled in Swagger UI
Why?

Passing an example dictionary to Body embeds the full payload sample into the OpenAPI schema that Swagger UI renders, without affecting runtime validation. Setting default values on the model fields is tempting but would accidentally make those fields optional and change validation behavior.

Just read this? Test yourself on what you have been reading.

Read the original → fastapi.tiangolo.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.

See open roles