Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

TypeError: dict object is not callable in FastAPI Dependency Injection

Verified FixPython 3.10+FastAPI 0.110+Silo: fastapi

Quick Fix / Solution Rapide

Pass a callable function or class reference to Depends(get_current_user) instead of invoking it prematurely as a dictionary instance.

Root Cause Analysis

This error occurs when Python executes a FastAPI route handler where a dependency declared in Depends(...) is an instantiated dictionary (or dictionary variable) instead of a callable function or class, causing FastAPI's dependency runner to attempt calling dict() as a function.

1. Passing Dictionary Instances to Depends()

Writing user: dict = Depends(current_user_dict) where current_user_dict = {'id': 1} causes FastAPI to execute current_user_dict(). Because dictionary objects do not implement __call__, Python raises TypeError: 'dict' object is not callable.

2. Calling the Dependency Function Prematurely

Writing Depends(get_db()) with parentheses executes get_db() at module import time. If get_db() returns a dictionary or session object, FastAPI receives the returned dictionary instead of the function itself.

3. Confusing Type Hinting with Dependency Registration

Mixing up user: dict with user: Annotated[dict, Depends(get_user_callable)] leads to syntax mistakes.

4. Class-Based Dependencies Without call

Passing custom class instances into Depends(instance) where the class does not define a __call__ method.

Reproduction Code (MCVE)

Example: Bug Reproduction
# Simulating a dependency injection runner attempting to call a dictionary
def resolve_dependency(dependency_target):
    return dependency_target()  # Calling dict instance raises TypeError

user_payload = {'username': 'admin', 'role': 'superuser'}
resolve_dependency(user_payload)

Solution 1: Pass the Callable Function Reference to Depends

Pass the uncalled function name (get_current_user) to Depends() so FastAPI manages invocation lifecycle.

Example: Recommended Solution
def get_current_user():
    # Authentic callable dependency function
    return {'username': 'admin', 'role': 'superuser'}

def route_handler(user: dict = None):
    user = user or get_current_user()
    print(f'User authenticated: {user["username"]}')
    return user

route_handler()

Solution 2: Implement __call__ in Class-Based Dependencies

If using a dependency class instance, define __call__ to make the instance callable.

Example: Alternative Solution
class RoleChecker:
    def __init__(self, allowed_roles: list[str]):
        self.allowed_roles = allowed_roles

    def __call__(self, user_role: str = 'admin') -> bool:
        return user_role in self.allowed_roles

admin_only = RoleChecker(['admin', 'superuser'])
print(f'Permission check: {admin_only("admin")}')

A common mistake is using Depends(get_settings()) in config modules. Use @lru_cache on def get_settings(): and pass Depends(get_settings) without parentheses. Edge cases occur with async dependencies: if get_db is async def, passing it to Depends is fully supported by FastAPI. Contrast this error with TypeError: 'NoneType' object is not callable, which occurs when a dependency returns None and is chained.