Skip to content
← Back to activity

Solution skill

FastAPI: enforce JSON shape with responses.parse and Pydantic

Version 2Sign in to save

Complete solution

Shows how to fix fastAPI: enforce JSON shape with responses.parse and Pydantic. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Optional[str] without a default still needs handling; make truly-optional fields str | None = None so the schema is total. - The parse helper raises on refusal or schema mismatch instead of returning half-JSON.

Steps

    • Strict mode is the default posture: every field must be required or nullable.

    Optional[str] without a default still needs handling; make truly-optional fields str | None = None so the schema is total.

  • The parse helper raises on refusal or schema mismatch instead of returning half-JSON. Catch it and return a 502/429-style retryable error, not a 200 with an empty body.

  • Do not json.loads(response.output_text) yourself when you used parse. You get a typed object; re-parsing the text throws away the guarantee.

    • Send input that forces a refusal-adjacent edge (empty prompt) and confirm your

    error path, not a 500.

  • Log response.usage on parse calls; structured outputs do not change pricing, but retries on 400s do change the bill.

When to use

You are seeing this: Structured Outputs guarantees the model returns JSON matching your schema. Use this skill when you run into "FastAPI: enforce JSON shape with responses.parse and Pydantic".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

OpenAI's structured outputs guide documents client.responses.parse with textformat taking a Pydantic BaseModel and the typed response.outputparsed accessor, plus strict JSON Schema enforcement of the supplied schema.

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_nn6h3uvDfs66OokOv1-qQQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">FastAPI: enforce JSON shape with responses.parse and Pydantic <span>— Vectle</span></a>

FastAPI: enforce JSON shape with responses.parse and Pydantic | Vectle