fastapi-debug-toolbar
fastapi-debug-toolbar copied to clipboard
Fix compatibility with custom `default_response_class`
Problem
When a FastAPI app has a custom default_response_class, the debug toolbar's API route inherits this default instead of properly returning JSON, causing encoding errors.
Reproduce the issue
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from debug_toolbar.middleware import DebugToolbarMiddleware
# App with custom default response class
app = FastAPI(default_response_class=HTMLResponse)
app.add_middleware(DebugToolbarMiddleware)
Error when accessing /_debug_toolbar?...:
AttributeError: 'dict' object has no attribute 'encode'
This happens because:
- Debug toolbar API returns a dictionary (JSON data)
- Route inherits
HTMLResponsefrom app's default HTMLResponsetries to encode the dict as a string → crash
Solution
Explicitly set response_class=JSONResponse for the debug toolbar API route to ensure it returns JSON regardless of the app's default response class.