{"id":"f2bd8c6e63b061ed","repo":"tiangolo/fastapi","slug":"item-already-exists-f2bd8c","errorCode":null,"errorMessage":"Item already exists","messagePattern":"Item already exists","errorType":"http","errorClass":"HTTPException","httpStatus":409,"severity":"error","filePath":"docs_src/app_testing/app_b_py310/main.py","lineNumber":34,"sourceCode":"    title: str\n    description: str | None = None\n\n\n@app.get(\"/items/{item_id}\", response_model=Item)\nasync def read_main(item_id: str, x_token: str = Header()):\n    if x_token != fake_secret_token:\n        raise HTTPException(status_code=400, detail=\"Invalid X-Token header\")\n    if item_id not in fake_db:\n        raise HTTPException(status_code=404, detail=\"Item not found\")\n    return fake_db[item_id]\n\n\n@app.post(\"/items/\")\nasync def create_item(item: Item, x_token: str = Header()) -> Item:\n    if x_token != fake_secret_token:\n        raise HTTPException(status_code=400, detail=\"Invalid X-Token header\")\n    if item.id in fake_db:\n        raise HTTPException(status_code=409, detail=\"Item already exists\")\n    fake_db[item.id] = item.model_dump()\n    return item\n","sourceCodeStart":16,"sourceCodeEnd":37,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/app_testing/app_b_py310/main.py#L16-L37","documentation":"Non-Annotated variant of error 3: POST /items/ raises HTTP 409 \"Item already exists\" when the body's id is already a key in fake_db.","triggerScenarios":"POST /items/ with an id colliding with an existing entry.","commonSituations":"Duplicate POST/retry; double-seeding; non-unique client ids.","solutions":["Generate a unique id (uuid4) per create.","Treat 409 as a signal to fetch/update instead of create.","Pre-check existence with a GET."],"exampleFix":"# before\npayload = {\"id\": \"foo\", ...}\n# after\npayload = {\"id\": str(uuid.uuid4()), ...}","handlingStrategy":"validation","validationCode":"import uuid\npayload = {\"id\": str(uuid.uuid4()), \"title\": \"Baz\"}","typeGuard":"def is_unique_id(new_id: str, existing: set[str]) -> bool:\n    return new_id not in existing","tryCatchPattern":"resp = client.post(\"/items/\", json=payload, headers=auth)\nif resp.status_code == 409:\n    client.get(f\"/items/{payload['id']}\", headers=auth)","preventionTips":["Use unique ids per create.","Idempotency keys on retries.","On 409, fetch instead of re-create."],"tags":["fastapi","conflict","duplicate","httpexception"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}