tiangolo/fastapi · warning · HTTPException

Nope! I don't like 3.

Error message

Nope! I don't like 3.

What it means

Same HTTPException(418) as error 35 but in tutorial006, which overrides the default handlers by delegating to FastAPI's built-in http_exception_handler and request_validation_exception_handler after logging. The 418 fires for item_id == 3 and is processed by the custom handler that prints 'OMG! An HTTP error!' before returning the standard JSON error.

Solutions

  1. Request GET /items/{n} with n != 3.
  2. Use a conventional status code if 418 is not intentional.
  3. Keep the delegation to built-in handlers so response shape stays consistent.

Example fix

// before
if item_id == 3:
    raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
// after
if item_id == 3:
    raise HTTPException(status_code=400, detail="Value 3 is not allowed")
Defensive patterns

Strategy: validation

Validate before calling

if item_id == 3:
    print('will return 418 (logged server-side)')

Type guard

def is_allowed_item(item_id: int) -> bool:
    return item_id != 3

Try / catch

resp = requests.get(f'http://localhost:8000/items/{item_id}')
if resp.status_code == 418:
    print('rejected (server logged OMG)')

Prevention

When it happens

Trigger: GET /items/3. Other integers return 200; non-integers return 400 via the logging validation handler.

Common situations: Centralized error logging via exception handlers. Developers see 418 for the sentinel value 3 and notice server-side log output for every HTTP/validation error.

Related errors


AI-assisted analysis of tiangolo/fastapi@3e8d1526d8 (2026-08-11). Data as JSON: /api/errors/2bffd453c7bc2ff9. Report an issue: GitHub.

Appendix: source

Thrown at docs_src/handling_errors/tutorial006_py310.py:27

app = FastAPI()


@app.exception_handler(StarletteHTTPException)
async def custom_http_exception_handler(request, exc):
    print(f"OMG! An HTTP error!: {repr(exc)}")
    return await http_exception_handler(request, exc)


@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
    print(f"OMG! The client sent invalid data!: {exc}")
    return await request_validation_exception_handler(request, exc)


@app.get("/items/{item_id}")
async def read_item(item_id: int):
    if item_id == 3:
        raise HTTPException(status_code=418, detail="Nope! I don't like 3.")
    return {"item_id": item_id}

View on GitHub (pinned to 3e8d1526d8)