Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix: complete asyncio.iscoroutinefunction deprecation fix across codebase

Julio Quinteros Pro480974e0f9eb
diff --git a/litellm/integrations/custom_guardrail.py b/litellm/integrations/custom_guardrail.py
index a8f1ba7ced0..56288ec5653 100644
--- a/litellm/integrations/custom_guardrail.py
+++ b/litellm/integrations/custom_guardrail.py
@@ -824,6 +824,7 @@ def log_guardrail_information(func):
"""
import asyncio
import functools
+ import inspect
def _infer_event_type_from_function_name(
func_name: str,
@@ -904,7 +905,7 @@ def log_guardrail_information(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
- if asyncio.iscoroutinefunction(func):
+ if inspect.iscoroutinefunction(func):
return async_wrapper(*args, **kwargs)
return sync_wrapper(*args, **kwargs)
diff --git a/litellm/litellm_core_utils/redact_messages.py b/litellm/litellm_core_utils/redact_messages.py
index 5d6d1fbc1c5..ad68f3851a8 100644
--- a/litellm/litellm_core_utils/redact_messages.py
+++ b/litellm/litellm_core_utils/redact_messages.py
@@ -9,6 +9,7 @@
import asyncio
import copy
+import inspect
from typing import TYPE_CHECKING, Any, Optional
import litellm
@@ -101,8 +102,8 @@ def perform_redaction(model_call_details: dict, result):
# Redact result
if result is not None:
# Check if result is a coroutine, async generator, or other async object - these cannot be deepcopied
- if (asyncio.iscoroutine(result) or
- asyncio.iscoroutinefunction(result) or
+ if (asyncio.iscoroutine(result) or
+ inspect.iscoroutinefunction(result) or
hasattr(result, '__aiter__') or # async generator
hasattr(result, '__anext__')): # async iterator
# For async objects, return a simple redacted response without deepcopy
diff --git a/litellm/proxy/common_utils/performance_utils.py b/litellm/proxy/common_utils/performance_utils.py
index f9537f85e2b..ec36f865cf1 100644
--- a/litellm/proxy/common_utils/performance_utils.py
+++ b/litellm/proxy/common_utils/performance_utils.py
@@ -12,6 +12,7 @@ import asyncio
import atexit
import cProfile
import functools
+import inspect
import threading
from pathlib import Path as PathLib
from typing import Any, Callable, Optional
@@ -100,7 +101,7 @@ def profile_endpoint(sampling_rate: float = 1.0):
global _last_profile_file_path
_last_profile_file_path = path
- if asyncio.iscoroutinefunction(func):
+ if inspect.iscoroutinefunction(func):
@functools.wraps(func)
async def async_wrapper(*args, **kwargs):
is_sampling = _start_profiling_for_request(sampling_rate)
diff --git a/litellm/proxy/management_endpoints/key_management_endpoints.py b/litellm/proxy/management_endpoints/key_management_endpoints.py
index 6ce586b6cd8..acdb4efc21b 100644
--- a/litellm/proxy/management_endpoints/key_management_endpoints.py
+++ b/litellm/proxy/management_endpoints/key_management_endpoints.py
@@ -11,6 +11,7 @@ All /key management endpoints
import asyncio
import copy
+import inspect
import json
import os
import secrets
@@ -1105,7 +1106,7 @@ async def generate_key_fn(
)
if user_custom_key_generate is not None:
- if asyncio.iscoroutinefunction(user_custom_key_generate):
+ if inspect.iscoroutinefunction(user_custom_key_generate):
result = await user_custom_key_generate(data) # type: ignore
else:
raise ValueError("user_custom_key_generate must be a coroutine")
@@ -1257,7 +1258,7 @@ async def generate_service_account_key_fn(
verbose_proxy_logger.debug("entered /key/generate")
if user_custom_key_generate is not None:
- if asyncio.iscoroutinefunction(user_custom_key_generate):
+ if inspect.iscoroutinefunction(user_custom_key_generate):
result = await user_custom_key_generate(data) # type: ignore
else:
raise ValueError("user_custom_key_generate must be a coroutine")
diff --git a/litellm/proxy/management_endpoints/ui_sso.py b/litellm/proxy/management_endpoints/ui_sso.py
index a57dafd1f00..d6d42019da5 100644
--- a/litellm/proxy/management_endpoints/ui_sso.py
+++ b/litellm/proxy/management_endpoints/ui_sso.py
@@ -11,6 +11,7 @@ Has all /sso/* routes
import asyncio
import base64
import hashlib
+import inspect
import os
import secrets
from copy import deepcopy
@@ -2258,7 +2259,7 @@ class SSOAuthenticationHandler:
user_defined_values: Optional[SSOUserDefinedValues] = None
if user_custom_sso is not None:
- if asyncio.iscoroutinefunction(user_custom_sso):
+ if inspect.iscoroutinefunction(user_custom_sso):
user_defined_values = await user_custom_sso(result) # type: ignore
else:
raise ValueError("user_custom_sso must be a coroutine function")
diff --git a/tests/proxy_unit_tests/test_response_polling_handler.py b/tests/proxy_unit_tests/test_response_polling_handler.py
index 26f8ac24adc..49624ddf505 100644
--- a/tests/proxy_unit_tests/test_response_polling_handler.py
+++ b/tests/proxy_unit_tests/test_response_polling_handler.py
@@ -650,12 +650,12 @@ class TestBackgroundStreamingModule:
def test_background_streaming_task_is_async(self):
"""Test that background_streaming_task is an async function"""
- import asyncio
+ import inspect
from litellm.proxy.response_polling.background_streaming import (
background_streaming_task,
)
-
- assert asyncio.iscoroutinefunction(background_streaming_task)
+
+ assert inspect.iscoroutinefunction(background_streaming_task)
class TestProviderResolutionForPolling: