{"id":3997,"library":"fast-depends","title":"FastDepends","description":"FastDepends is a lightweight Python library providing a dependency injection system, extracted and refined from FastAPI's core logic. It allows developers to use FastAPI-like dependency resolution and type casting in any Python project, whether synchronous or asynchronous, without the HTTP-specific components. Currently at version 3.0.8, the library maintains an active development pace with frequent updates and bug fixes.","status":"active","version":"3.0.8","language":"en","source_language":"en","source_url":"https://github.com/Lancetnik/FastDepends","tags":["dependency injection","fastapi","framework-agnostic","type-casting","validation","async","sync"],"install":[{"cmd":"pip install fast-depends","lang":"bash","label":"Install core library"},{"cmd":"pip install fast-depends[pydantic]","lang":"bash","label":"Install with Pydantic serializer"},{"cmd":"pip install fast-depends[msgspec]","lang":"bash","label":"Install with Msgspec serializer"}],"dependencies":[{"reason":"Required for older Python versions to support advanced type hints.","package":"typing-extensions","optional":false},{"reason":"Used for asynchronous operations and compatibility.","package":"anyio","optional":false},{"reason":"Optional, for Pydantic-based validation and serialization (e.g., PydanticSerializer, Field).","package":"pydantic","optional":true},{"reason":"Optional, for high-performance Msgspec-based serialization (e.g., MsgSpecSerializer, field).","package":"msgspec","optional":true}],"imports":[{"note":"The primary decorator for applying dependency injection to functions.","symbol":"inject","correct":"from fast_depends import inject"},{"note":"Used to declare a dependency on another callable.","symbol":"Depends","correct":"from fast_depends import Depends"},{"note":"Used for injecting context-aware dependencies (e.g., current request, user).","symbol":"ContextDepends","correct":"from fast_depends import ContextDepends"},{"note":"Required when explicitly using Pydantic for serialization/validation with @inject.","symbol":"PydanticSerializer","correct":"from fast_depends.pydantic import PydanticSerializer"},{"note":"Required when explicitly using Msgspec for high-performance serialization/validation with @inject.","symbol":"MsgSpecSerializer","correct":"from fast_depends.msgspec import MsgSpecSerializer"}],"quickstart":{"code":"from fast_depends import inject, Depends\n\ndef get_user_id() -> int:\n    return 42\n\n@inject\ndef process_data(data: str, user_id: int = Depends(get_user_id)) -> str:\n    return f\"Processing '{data}' for user {user_id}\"\n\nresult = process_data(\"hello world\")\nprint(result)\n\n# Async example (requires an event loop)\nimport asyncio\n\nasync def async_get_greeting() -> str:\n    await asyncio.sleep(0.1)\n    return \"Hello\"\n\n@inject\nasync def greet_user(name: str, greeting: str = Depends(async_get_greeting)) -> str:\n    return f\"{greeting}, {name}!\"\n\nasync def main_async():\n    async_result = await greet_user(\"Alice\")\n    print(async_result)\n\n# To run the async example in a synchronous environment:\n# asyncio.run(main_async())","lang":"python","description":"This quickstart demonstrates basic synchronous and asynchronous dependency injection using the `@inject` decorator and `Depends` for declaring dependencies. The `get_user_id` dependency is resolved and injected into `process_data`, while `async_get_greeting` is resolved into `greet_user`."},"warnings":[{"fix":"Update `@inject` calls to specify `serializer=PydanticSerializer()` or `serializer=MsgSpecSerializer()` if custom validation or serialization fields (like `Pydantic.Field` or `Msgspec.field`) are used. For basic type casting, no explicit serializer might be needed.","message":"Version 3.0.0 introduced explicit serializer selection. If you were implicitly relying on Pydantic v1 behavior, you now need to explicitly pass a serializer instance (e.g., `PydanticSerializer()`) to the `@inject` decorator.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure that `async def` dependencies are only used within `async def` functions decorated with `@inject`, and that these functions are called within an active asyncio event loop. For synchronous `inject`'ed functions, use `def` dependencies.","message":"When running in a synchronous context, only synchronous dependencies are available. Asynchronous dependencies can only be resolved when the `@inject`'ed function itself is asynchronous and an event loop is running.","severity":"gotcha","affected_versions":"All"},{"fix":"If a dependency needs to be executed every time it's referenced (e.g., for side effects or fresh data), set `cache=False` when declaring it: `Depends(my_dependency, cache=False)`.","message":"Dependencies are cached by default within a single call to an `@inject`'ed function. This means if a dependency is called multiple times by different sub-dependencies within the same top-level `@inject` call, it will only execute once and return the cached result.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure consistent Pydantic versions across your project. When using Pydantic-specific features with FastDepends, explicitly use `fast_depends.pydantic.PydanticSerializer` and ensure your Pydantic model definitions align with the installed Pydantic version (v1 or v2). FastDepends v3.x is generally compatible with Pydantic v2.","message":"FastDepends leverages Pydantic for validation and type casting by default. Mixing FastDepends with projects or dependencies that are strictly tied to Pydantic v1 might lead to compatibility issues, especially if the new explicit serializer system is not used correctly.","severity":"gotcha","affected_versions":"All (especially with Pydantic v1/v2 transition)"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}