---
source: https://iinventedlight.com/library/notes/fastapi-error-handling.md
title: "Returning HTTP errors in FastAPI"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE); prose restates or closely paraphrases cited source lines"
license_family: fastapi-mit
rule_check: "needs rule check (family not in auto-publish rule; check.mjs auto-approves only usgov and arxiv-ccby)"
attribution: "Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Handling Errors - FastAPI, MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE); FastAPI docs, Response Status Code - FastAPI, MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-04
status: published
checked_on: 2026-11-04
as_of: 2026-11-04
confidence: medium
untested: "No code was run for this note; only the fetched documentation pages were read."
category: coding-references
topic_area: "code-ai-eng: FastAPI"
tags: ['fastapi', 'errors', 'http-status']
sources:
  - id: S1
    title: "Handling Errors - FastAPI"
    publisher: FastAPI docs
    url: https://fastapi.tiangolo.com/tutorial/handling-errors/
    accessed: 2026-11-04
    license: "MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE)"
  - id: S2
    title: "Response Status Code - FastAPI"
    publisher: FastAPI docs
    url: https://fastapi.tiangolo.com/tutorial/response-status-code/
    accessed: 2026-11-04
    license: "MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE)"
---
# Returning HTTP errors in FastAPI

**Answer:** To return HTTP responses with errors to the client you use HTTPException. Because it's a Python exception, you don't return it, you raise it. (S1)

## Details and handlers

- When raising an HTTPException, you can pass any value that can be converted to JSON as the parameter detail, not only str. (S1)
- The status codes in the 400 range mean that there was an error from the client. (S1)
- When a request contains invalid data, FastAPI internally raises a RequestValidationError. (S1)
- Default handlers are in charge of returning the default JSON responses when you raise an HTTPException and when the request has invalid data. (S1)

## Status codes

- The status_code parameter receives a number with the HTTP status code. (S2)
- status_code can alternatively also receive an IntEnum, such as Python's http.HTTPStatus. (S2)
- Some response codes indicate that the response does not have a body. (S2)

## Claims
- id: C1
  claim: Use HTTPException for errors.
  source: S1
  evidence: "To return HTTP responses with errors to the client you use HTTPException."
- id: C2
  claim: You raise it rather than return it.
  source: S1
  evidence: "you don't return it, you raise it."
- id: C3
  claim: Invalid data raises RequestValidationError.
  source: S1
  evidence: "FastAPI internally raises a RequestValidationError."
- id: C4
  claim: status_code can be an IntEnum.
  source: S2
  evidence: "status_code can alternatively also receive an IntEnum"

## Sources
- [S1] Handling Errors - FastAPI. FastAPI docs. https://fastapi.tiangolo.com/tutorial/handling-errors/ . License: MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE). Accessed 2026-11-04.
- [S2] Response Status Code - FastAPI. FastAPI docs. https://fastapi.tiangolo.com/tutorial/response-status-code/ . License: MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE). Accessed 2026-11-04.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
