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

Identical logic to error 22 but in the non-Annotated tutorial008c_py310.py (uses `username: str = Depends(get_username)` instead of Annotated). Same 404 HTTPException for any item_id other than 'plumbus'. The InternalError path for 'portal-gun' is swallowed by the dependency's except block.

Solutions

  1. Request GET /items/plumbus.
  2. Recognize the tutorial's point: a yield-dependency that catches but does not re-raise will swallow InternalError, so unknown ids simply 404.
  3. Switch to the re-raise pattern (tutorial008d) if you want the InternalError to surface.

Example fix

// before
if item_id != "plumbus":
    raise HTTPException(status_code=404, detail="Item not found, there's only a plumbus here")
// after
if item_id not in ("plumbus",):
    raise HTTPException(status_code=404, detail="Item not found, there's only a plumbus here")
Defensive patterns

Strategy: validation

Validate before calling

ALLOWED = {'plumbus'}
assert item_id in ALLOWED

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: GET /items/{item_id} with item_id != 'plumbus' and != 'portal-gun'.

Common situations: Same as the Annotated variant; difference is purely the dependency-injection syntax. Hit when clients guess item ids outside the allowed single-element set.

Related errors


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

Appendix: source

Thrown at docs_src/dependencies/tutorial008c_py310.py:24

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: 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)