Dependencies with Yield
Part of: FastAPI Basics
Learn to create dependencies that perform cleanup operations using yield for resource management.
What You'll Learn
Theory and Concepts
Dependencies with Yield
🎯 What You'll Learn
- Create dependencies that perform cleanup operations using yield
- Use try/finally blocks for guaranteed cleanup
- Understand the execution flow of yield dependencies
- Implement database session management with proper resource cleanup
📚 Theory
FastAPI supports dependencies that do extra steps after finishing. To do this, use yield instead of return, and write the extra steps (code) after.
A Database Dependency with yield
You can use this to create a database session and close it after finishing. Only the code prior to and including the yield statement is executed before creating a response:
[Code Example]
The yielded value is what is injected into path operations and other dependencies. The code following the yield statement is executed after creating the response but before sending it.
Execution Flow
When you use yield in a dependency:
1. Setup Code: Code before yield runs before the endpoint executes
2. Value Injection: The yielded value is injected into your endpoint
3. Endpoint Execution: Your path operation function runs
4. Response Creation: FastAPI creates the response
5. Cleanup Code: Code after yield runs before sending the response
Dependencies with yield and try
If you use a try block in a dependency with yield, you'll receive any exception that was thrown when using the dependency. You can use except to handle specific exceptions and finally to ensure cleanup always happens:
[Code Example]
🔧 Key Concepts
- Resource Management: Automatically clean up resources like database connections
- Exception Handling: Use try/except blocks to handle errors during cleanup
- Execution Order: FastAPI ensures cleanup code runs in the correct order for nested dependencies
- Context Managers: Yield dependencies work similarly to Python's context managers
💡 Best Practices
- Always use yield only once per dependency function
- Use try/finally blocks to ensure cleanup code always runs
- Handle specific exceptions in except blocks when needed
- Re-raise exceptions to maintain proper error handling
- Keep setup and cleanup code minimal and focused
🚀 Real-World Applications
- Database Sessions: Create and close database connections
- File Handling: Open and close files safely
- Cache Management: Initialize and cleanup cache connections
- External API Clients: Setup and teardown HTTP clients
- Logging Context: Setup request-specific logging
🔗 Additional Resources
- FastAPI Dependencies with Yield
- Python Context Managers
Helpful Hint
Use yield instead of return to create dependencies that can perform cleanup operations. Follow the official pattern: setup code before yield, cleanup code in finally block.