tiangolo/fastapi · error · HTTPException

Item not found

Error message

Item not found

What it means

Raised (404) by GET /items/{item_id} in tutorial008b when item_id is not a key in the data dict ('plumbus' or 'portal-gun'). This check runs after the get_username dependency has already yielded, so the dependency's username is available but the resource simply does not exist.

Solutions

  1. GET /items/portal-gun for a fully successful read.
  2. GET /items/plumbus only if you also expect/handle the owner-error 400.
  3. Confirm you are reading the tutorial008b data set, not another file's.

Example fix

// before
GET /items/foo
// after
GET /items/portal-gun
Defensive patterns

Strategy: try-catch

Validate before calling

import httpx
KNOWN = {'plumbus', 'portal-gun'}
item_id = 'portal-gun'
if item_id in KNOWN:
    resp = httpx.get(f'http://localhost:8000/items/{item_id}')

Type guard

def is_known_tutorial008b_item(item_id: object) -> bool:
    return isinstance(item_id, str) and item_id in {'plumbus', 'portal-gun'}

Try / catch

try:
    resp = httpx.get(f'http://localhost:8000/items/{item_id}')
    resp.raise_for_status()
except httpx.HTTPStatusError as e:
    if e.response.status_code == 404:
        # item does not exist
        ...

Prevention

When it happens

Trigger: GET /items/<anything-but-plumbus-or-portal-gun>. Note 'plumbus' will pass this check but then fail the owner check (error 18); 'portal-gun' succeeds fully.

Common situations: Confusing this file's seed set ({plumbus, portal-gun}) with other tutorials' seeds; typos; expecting cross-process persistence.

Related errors


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

Appendix: source

Thrown at docs_src/dependencies/tutorial008b_an_py310.py:28

    "portal-gun": {"description": "Gun to create portals", "owner": "Rick"},
}


class OwnerError(Exception):
    pass


def get_username():
    try:
        yield "Rick"
    except OwnerError as e:
        raise HTTPException(status_code=400, detail=f"Owner error: {e}")


@app.get("/items/{item_id}")
def get_item(item_id: str, username: Annotated[str, Depends(get_username)]):
    if item_id not in data:
        raise HTTPException(status_code=404, detail="Item not found")
    item = data[item_id]
    if item["owner"] != username:
        raise OwnerError(username)
    return item

View on GitHub (pinned to 3e8d1526d8)