tiangolo/fastapi · error · HTTPException

Item not found, there's only a plumbus here

Error message

Item not found, there's only a plumbus here

What it means

HTTPException (404) raised in the path operation of the Annotated-typed tutorial008c. The endpoint only accepts 'plumbus' as a valid item; any other id (except 'portal-gun', which raises InternalError) yields this 404. It demonstrates that an exception swallowed in a yield-dependency's except block (here it prints and does not re-raise) does not affect the path operation's own HTTPException flow.

Solutions

  1. Request GET /items/plumbus to get a successful response.
  2. If you expected 'portal-gun' to succeed, note that id triggers InternalError which the dependency intentionally swallows (prints, does not re-raise) — see tutorial008d for the re-raise variant.
  3. Adjust the endpoint's allowed set if new ids should be valid.

Example fix

// before
if item_id != "plumbus":
    raise HTTPException(status_code=404, detail="Item not found, there's only a plumbus here")
// after (data-driven allowed set)
ALLOWED = {"plumbus"}
if item_id not in ALLOWED:
    raise HTTPException(status_code=404, detail=f"Item not found, available: {sorted(ALLOWED)}")
Defensive patterns

Strategy: validation

Validate before calling

ALLOWED = {'plumbus'}
if item_id not in ALLOWED:
    print('will return 404; only plumbus is valid')

Type guard

def is_known_item(item_id: str) -> bool:
    return item_id == 'plumbus'

Try / catch

import requests
resp = requests.get(f'http://localhost:8000/items/{item_id}')
if resp.status_code == 404:
    print('Only plumbus is served here')

Prevention

When it happens

Trigger: GET /items/{item_id} where item_id is any value other than 'plumbus' (and other than 'portal-gun'). For example GET /items/foo or GET /items/123 returns 404.

Common situations: Whitelisted resource endpoints where only a fixed set of ids are valid. Developers hit this when the InternalError path (portal-gun) is swallowed by the dependency and they expect a 500, but instead get a 404 for ordinary unknown ids.

Related errors


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

Appendix: source

Thrown at docs_src/dependencies/tutorial008c_an_py310.py:26

class InternalError(Exception):
    pass


def get_username():
    try:
        yield "Rick"
    except InternalError:
        print("Oops, we didn't raise again, Britney 😱")


@app.get("/items/{item_id}")
def get_item(item_id: str, username: Annotated[str, Depends(get_username)]):
    if item_id == "portal-gun":
        raise InternalError(
            f"The portal gun is too dangerous to be owned by {username}"
        )
    if item_id != "plumbus":
        raise HTTPException(
            status_code=404, detail="Item not found, there's only a plumbus here"
        )
    return item_id

View on GitHub (pinned to 3e8d1526d8)