Skip to main content

FastAPI Yieldable Response

A middleware for FastAPI that sends early responses from route handlers.

Overview

FastAPI Yieldable Response allows you to send immediate responses to clients while continuing to execute background processing in your route handlers. This is particularly useful when you want to:

  • Send quick acknowledgments to clients before performing time-consuming operations
  • Improve perceived response times by returning data immediately
  • Execute cleanup or logging tasks after the response has been sent

Installation

pip install fastapi_yieldable_response

Quick Start

from fastapi import FastAPI
from fastapi_yieldable_response import yieldable_response

app = FastAPI()

@app.get("/")
@yieldable_response
async def root():
    # This response is sent immediately to the client
    yield {"message": "Hello!"}
    
    # This code runs in the background after the response is sent
    import time
    time.sleep(2)
    print("Background task completed!")

How It Works

The @yieldable_response decorator transforms your route handler into a function that:

  1. Immediate Response: The first yield statement sends an immediate response to the client
  2. Background Execution: Any code after the yield continues executing as a background task
  3. Automatic Cleanup: Uses FastAPI's BackgroundTasks to ensure proper resource management

Features

  •  Zero Dependencies: Only requires FastAPI (no additional dependencies)
  •  Async/Sync Support: Works with both async and sync route handlers
  •  Type Safety: Maintains original function signatures and type hints
  •  FastAPI Integration: Uses FastAPI's built-in BackgroundTasks system
  •  Simple API: Just add the decorator to your existing routes

Usage Examples

Async Route Handler

@app.get("/async-example")
@yieldable_response
async def async_example():
    yield {"status": "processing", "id": 123}
    
    # Background processing
    await some_async_operation()
    await send_notification()

Sync Route Handler

@app.get("/sync-example/{item_id}")
@yieldable_response
def sync_example(item_id: str):
    yield {"message": f"Processing item {item_id}"}
    
    # Background processing
    process_item(item_id)
    log_operation(item_id)

With Path Parameters

@app.get("/users/{user_id}")
@yieldable_response
async def get_user(user_id: int):
    # Send immediate response
    yield {"user_id": user_id, "status": "found"}
    
    # Log access in background
    await log_user_access(user_id)
    await update_analytics(user_id)

Requirements

  • Python 3.10+
  • FastAPI 0.68.0+

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Repository

Release files for fastapi_yieldable_response 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi_yieldable_response 0.1.0
File Size Uploaded
fastapi_yieldable_response-0.1.0.tar.gz 15.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi_yieldable_response 0.1.0
File Interpreter ABI Platform
fastapi_yieldable_response-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.7 kB

Release files / fastapi_yieldable_response-0.1.0.tar.gz

Download URL fastapi_yieldable_response-0.1.0.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5594e722cb27a5a01a622b411bfc04a68d0b5969f9e4142b787e3b5c39c00cfc
BLAKE2b-256 checksum
How to use checksums
1862de84e991651b2ca5defb7c651d819e40dc8be041901240a3e99ac9e12fa5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.8

Release files / fastapi_yieldable_response-0.1.0-py3-none-any.whl

Download URL fastapi_yieldable_response-0.1.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aeb28dfd8e668af0451a1b9ff83ea2140e99773411b07f6772daea4a151e8c01
BLAKE2b-256 checksum
How to use checksums
717b97a2feb748678b6a646c3e11e3652b8898c0452b45402209ece0826a7b43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.8

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page