Performance: Path parameter types and validation
MEDIUM IMPACT
This affects server response time and client perceived latency by validating and parsing path parameters efficiently before processing the request.
from fastapi import FastAPI app = FastAPI() @app.get('/items/{item_id}') async def read_item(item_id: int): # FastAPI validates and converts automatically return {'item_id': item_id}
from fastapi import FastAPI app = FastAPI() @app.get('/items/{item_id}') async def read_item(item_id: str): # No type validation, manual parsing later try: item_id_int = int(item_id) except ValueError: return {'error': 'Invalid item_id'} return {'item_id': item_id_int}
| Pattern | Server Processing | Validation Speed | Error Handling | Verdict |
|---|---|---|---|---|
| Manual string parsing | High CPU use per request | Slow, done at runtime | Delayed error response | [X] Bad |
| FastAPI type annotations | Low CPU use, automatic | Fast, built-in validation | Immediate error response | [OK] Good |