API Error Response Schema
A consistent API error response schema makes failures predictable for clients and easier to diagnose. It gives consumers stable fields for understanding what happened while allowing server-side details to remain internal.
An API error response schema defines the structure returned when a request cannot be processed successfully. A typical envelope contains an HTTP status, a stable application error code, a human-readable message, and optional structured details.
HTTP status codes communicate the broad category of failure, while an application-level `code` gives clients a stable identifier for programmatic handling. The message should be useful to humans but should not expose stack traces, database errors, secrets, or other internal implementation details.
For validation failures, an optional `details` field can identify individual invalid fields and explain their constraints. Keeping this structure consistent across endpoints prevents clients from having to parse different error shapes for different resources.
In a Python backend, the schema is usually enforced at a framework boundary or through response models. Centralized exception handling can convert unexpected exceptions into the same public envelope while logging the full diagnostic information privately.
Key points
- Use one predictable error envelope across API endpoints.
- Separate HTTP status from a stable application error code.
- Keep client-facing messages safe and actionable; log sensitive diagnostics server-side.
- Use structured validation details when clients need field-level errors.
- Centralize exception-to-response conversion where practical.
Interview questions
Why should an API use a consistent error response schema?
Consistency lets API clients handle failures without endpoint-specific parsing logic. A stable structure also makes logs, tests, and documentation easier to reason about. Clients can branch on a machine-readable error code while displaying an appropriate message to users. The server can change internal implementation details without changing the public error contract.
What would you include in a typical API error response?
I would typically include an application-specific `code` and a safe human-readable `message`. The response would also use an appropriate HTTP status code. For validation errors, I might add structured `details` containing affected fields and constraint information. A request or correlation identifier can also help connect a client-visible error with server-side logs.
How would you prevent internal exceptions from leaking through the API?
I would put exception handling at a centralized boundary, such as framework middleware or an exception handler. Known application exceptions would map to documented error codes and appropriate HTTP statuses. Unexpected exceptions would produce a generic server-error response while the detailed exception and traceback are logged securely. This separates the public API contract from internal diagnostics.
How would you design validation errors for a Python API?
I would keep the top-level envelope stable and make validation details a structured collection of field or location errors. Each detail could identify the input location, field path, and a stable reason or code, with a safe human-readable message. The exact representation should be documented so clients can reliably render or process multiple validation failures. Framework-specific validation objects should be translated at the API boundary rather than becoming an accidental public contract.
What is the difference between an HTTP status and an application error code?
An HTTP status is standardized and communicates the broad outcome of the request. An application error code is controlled by the API and can distinguish cases that share the same HTTP status. For example, several validation conditions might use one client-error status while having different application codes. Clients should generally use the documented application semantics rather than parsing free-form messages.