Implementation:BerriAI Litellm Asyncify
Appearance
| Attribute | Value |
|---|---|
| Sources | litellm/litellm_core_utils/asyncify.py |
| Domains | Async Utilities, Concurrency |
| Last Updated | 2026-02-15 16:00 GMT |
Overview
Provides utilities for converting blocking functions to async and running async functions from synchronous contexts.
Description
This module contains three utilities for bridging sync and async execution:
asyncify-- Takes a blocking (synchronous) function and wraps it so that it runs in a worker thread viaanyio.to_thread.run_sync(), returning an async-compatible callable. It handles compatibility with both older and newer versions ofanyio(thecancellablevs.abandon_on_cancelparameter change in v4.1.0).run_async_function-- Runs an async function from a synchronous context. If an event loop is already running, it spawns a new thread with its own event loop to avoid nested-loop issues. If no loop is running, it creates one in the current thread.function_has_argument-- A helper that inspects a function's signature to check whether it accepts a named parameter.
Usage
Import asyncify when you need to call a blocking I/O function from async code without blocking the event loop. Import run_async_function when you need to call an async function from synchronous code (e.g., in CLI tools or synchronous entry points).
Code Reference
Source Location
litellm/litellm_core_utils/asyncify.py (116 lines)
Signature
def function_has_argument(function: Callable, arg_name: str) -> bool
def asyncify(
function: Callable[T_ParamSpec, T_Retval],
*,
cancellable: bool = False,
limiter: Optional[anyio.CapacityLimiter] = None,
) -> Callable[T_ParamSpec, Awaitable[T_Retval]]
def run_async_function(async_function, *args, **kwargs)
Import
from litellm.litellm_core_utils.asyncify import asyncify, run_async_function, function_has_argument
I/O Contract
asyncify
| Direction | Name | Type | Description |
|---|---|---|---|
| Input | function | Callable[T_ParamSpec, T_Retval] |
Blocking function to convert |
| Input | cancellable | bool |
If True, allows cancellation (thread continues but result is discarded)
|
| Input | limiter | Optional[anyio.CapacityLimiter] |
Limits the number of concurrent worker threads |
| Output | return | Callable[T_ParamSpec, Awaitable[T_Retval]] |
Async wrapper that delegates to a worker thread |
run_async_function
| Direction | Name | Type | Description |
|---|---|---|---|
| Input | async_function | Callable |
The async function to run |
| Input | *args | positional | Positional arguments forwarded to the async function |
| Input | **kwargs | keyword | Keyword arguments forwarded to the async function |
| Output | return | Any |
Result of the async function execution |
function_has_argument
| Direction | Name | Type | Description |
|---|---|---|---|
| Input | function | Callable |
Function to inspect |
| Input | arg_name | str |
Parameter name to look for |
| Output | return | bool |
True if the function has the named parameter
|
Usage Examples
from litellm.litellm_core_utils.asyncify import asyncify, run_async_function
# Convert a blocking function to async
import time
def blocking_io(url: str) -> str:
time.sleep(1)
return f"fetched {url}"
async_fetch = asyncify(blocking_io)
# Use in async context
async def main():
result = await async_fetch("https://example.com")
print(result)
# Run an async function from sync code
async def my_async_func(x, y):
return x + y
result = run_async_function(my_async_func, 1, 2)
# result == 3
Related Pages
- BerriAI_Litellm_Fallback_Utils -- uses
run_async_functionto provide a sync wrapper aroundasync_completion_with_fallbacks
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment