{"id":"b5545b55cd7cb826","repo":"tiangolo/fastapi","slug":"item-not-found-b5545b","errorCode":null,"errorMessage":"Item not found","messagePattern":"Item not found","errorType":"http","errorClass":"HTTPException","httpStatus":404,"severity":"error","filePath":"docs_src/handling_errors/tutorial001_py310.py","lineNumber":11,"sourceCode":"from fastapi import FastAPI, HTTPException\n\napp = FastAPI()\n\nitems = {\"foo\": \"The Foo Wrestlers\"}\n\n\n@app.get(\"/items/{item_id}\")\nasync def read_item(item_id: str):\n    if item_id not in items:\n        raise HTTPException(status_code=404, detail=\"Item not found\")\n    return {\"item\": items[item_id]}\n","sourceCodeStart":1,"sourceCodeEnd":13,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/handling_errors/tutorial001_py310.py#L1-L13","documentation":"The canonical FastAPI HTTP 404 \"Item not found\", raised when `item_id` is not a key in the in-memory `items` dict ({\"foo\": ...}). It is the simplest usage of HTTPException to signal a missing resource and is rendered by FastAPI's default exception handler as JSON {\"detail\": \"Item not found\"}.","triggerScenarios":"GET /items/<anything-except-foo> (e.g. /items/bar).","commonSituations":"Client typos; deleted resources; cached client references; replacing the dict with a DB without preserving the not-found semantics.","solutions":["Request GET /items/foo for a successful response.","If backing with a DB, keep the same 404 on lookup miss.","Use a custom exception handler if you need a different body shape.","Add a test for the 404 path."],"exampleFix":"# before\nif item_id not in items:\n    raise HTTPException(status_code=404, detail=\"Item not found\")\n\n# after (resource from DB)\nitem = await db.get(Item, item_id)\nif item is None:\n    raise HTTPException(status_code=404, detail=\"Item not found\")","handlingStrategy":"validation","validationCode":"if item_id not in {\"foo\"}:\n    raise KeyError(\"Item not found\")","typeGuard":"def is_known(item_id: str) -> bool:\n    return item_id in {\"foo\"}","tryCatchPattern":"r = client.get(f\"/items/{item_id}\")\nif r.status_code == 404:\n    # offer the valid set to the user\n    ...","preventionTips":["Cache the valid id set client-side.","Treat 404 as terminal for that id.","Refresh the id list on schema/version changes."],"tags":["fastapi","http-404","not-found","handling-errors"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}