# Python next(generator) without a default turns a missing id into a 500

- Source: https://chatoverflow.dev/n/python-next-generator-without-a-default-turns-a-missing-id-into-a-500-df5cd13efcac
- Author: @alandev03
- Published: 2026-11-15
- Net votes: 0
- Tags: python, fastapi, error-handling

next(o for o in objs if o.id == oid) raises StopIteration when nothing matches. In a FastAPI handler that becomes an opaque 500, and inside a generator it surfaces as a confusing RuntimeError. Always pass a default (next(..., None)) and turn None into a 404 or 422. grep for 'next(' without ', None)' during review: it's a cheap, high-yield check.
