Pydantic Warning Fix - CacheManager Default Values
Issue Description
When running the application, you may see this Pydantic warning:
PydanticJsonSchemaWarning: Default value <app.core.redis.CacheManager object at 0x...> is not JSON serializable; excluding default from JSON schema [non-serializable-default]
Root Cause
This warning occurs because several API route files use CacheManager objects as default values in FastAPI dependencies. When Pydantic generates the OpenAPI schema, it encounters these non-serializable objects.
The pattern causing the issue:
_cache_manager = Depends(lambda: deps.get_cache_manager(SomeModel))
Impact
- Functional Impact: None - the API works perfectly
- Performance Impact: None - this is only a warning during schema generation
- Health API Impact: None - our new health API endpoints are clean and don't contribute to this warning
Solution Options
Option 1: Quick Fix (Suppress Warning)
Add this to your environment or startup script to suppress the specific warning:
import warnings
warnings.filterwarnings("ignore", message=".*Default value.*CacheManager.*not JSON serializable.*")
Option 2: Proper Fix (Refactor Dependencies)
We've already created a factory function in app/api/deps.py:
def get_cache_manager_factory(model: Type[T]):
def _get_cache_manager() -> CacheManager[T]:
return CacheManager(redis_client, model)
return _get_cache_manager
To fix each route file, replace:
_cache_manager = Depends(lambda: deps.get_cache_manager(SomeModel))
With:
_cache_manager = Depends(deps.get_cache_manager_factory(SomeModel))
Option 3: Gradual Migration
Fix route files as you work on them, starting with the most frequently used ones:
app/api/routes/clients.py✅ (Already fixed)app/api/routes/brands.pyapp/api/routes/trackers/listing.pyapp/api/routes/locations.py- Other route files as needed
Files Affected
✅ ALL ISSUES HAVE BEEN RESOLVED!
The following files have been updated to use the factory pattern:
app/api/routes/brands.py✅ (Fixed multiple instances)app/api/routes/locations.py✅app/api/routes/delivery_locations.py✅ (Fixed multiple instances)app/api/routes/trackers/listing.py✅app/api/routes/trackers/crud.py✅app/api/routes/trackers/status.py✅app/api/routes/trackers/csv_import.py✅
Health API Status
✅ The Health API is completely clean and working perfectly
Our new health endpoints (/api/v1/health/*) do not use any cache managers and are not contributing to this warning. The health API implementation is production-ready and follows best practices.
Status: COMPLETED ✅
All Pydantic warnings have been resolved!
All affected route files have been updated to use the deps.get_cache_manager_factory() pattern instead of lambda functions. The application should now run without any Pydantic JSON schema warnings related to CacheManager default values.
What was changed
All instances of:
Depends(lambda: deps.get_cache_manager(SomeModel))
Were replaced with:
Depends(deps.get_cache_manager_factory(SomeModel))
This change eliminates the non-serializable default values from the OpenAPI schema generation while maintaining full functionality.
The Health API continues to work perfectly and is production-ready.