FastAPI Essentials

Course Content

FastAPI Essentials

1 sections · 32 lessons

What are some common error codes returned by FastAPI?


What you need to know

Status codes are grouped by first digit. 4xx means the client should change something; 5xx means the server or something behind it failed. That split tells a client whether retrying makes sense.

Codes FastAPI returns on its own

Text
GET  /v1/nope                         -> 404 {'detail': 'Not Found'}GET  /v1/chat   (route is POST-only)  -> 405 {'detail': 'Method Not Allowed'}   Allow: POSTPOST /v1/chat   body '{"prompt": "hi"'  (broken JSON)     -> 422 [{'type': 'json_invalid', 'loc': ['body', 15], 'msg': 'JSON decode error', ...}]POST /v1/chat   content-type text/plain     -> 422 [{'type': 'model_attributes_type', 'loc': ['body'], ...}]

These are real responses. Note that FastAPI uses 422 even for JSON that cannot be parsed, where many other frameworks would say 400. Each item in the 422 detail list has loc (where), msg (what) and type (a stable code clients can match on).

Codes you raise, and what they tell the client

CodeMeaningAI-service exampleRetry?
400Bad request (semantic)Prompt contains no text after cleaningNo
401Not authenticatedMissing or expired API key or tokenAfter re-auth
403Authenticated, not allowedFree plan calling a pro-only modelNo
404Not foundUnknown doc_id or run idNo
409ConflictDocument already being indexedMaybe later
413Payload too large50 MB upload (usually rejected by the proxy)No
422Validation failedtemperature: 5No
429Too many requestsRate or token quota hit; send Retry-AfterYes, after the delay
500Unhandled errorA bug in your codeMaybe
502Bad gatewayUpstream model server returned garbageYes, with backoff
503UnavailableModel overloaded or still loadingYes, with backoff
504Gateway timeoutLLM provider took too longYes, with backoff

HTTPException(status_code=429, detail="...", headers={"Retry-After": "30"}) is how you raise any of these; from fastapi import status gives readable names such as status.HTTP_429_TOO_MANY_REQUESTS.

A real-life example

An LLM gateway sits between a company's apps and three model providers. At first it returned 500 for everything that went wrong. Client apps retried every 500 immediately, so when a provider was rate-limiting, the retries tripled the traffic and made the outage worse. When a user sent an invalid temperature, the apps also retried, three times, for an error that could never succeed.

The team mapped errors honestly: provider 429 → gateway 429 with Retry-After; provider timeout → 504; content-policy block → 400 with code: content_blocked; validation → 422. The shared client library retries only 429, 502, 503 and 504, with exponential backoff. Retry traffic during provider incidents dropped sharply, and dashboards could finally separate "users sending bad input" from "we are down".

Follow-up questions to expect

  • "Why 422 and not 400 for validation?" — FastAPI's convention: 422 Unprocessable Content means the request was understood but broke the rules. If your organisation standardises on 400, override the RequestValidationError handler.
  • "What does the client see on an unhandled exception?" — A plain 500 Internal Server Error with no details; the traceback goes only to the server logs.
  • "Which codes should a client retry?" — 429 (after Retry-After), 502, 503 and 504, with backoff. Never 4xx validation or auth errors.