From fadc04fbe2790f417f474c9a15b4d75ebb9935b2 Mon Sep 17 00:00:00 2001 From: ryan-crabbe <128659760+ryan-crabbe@users.noreply.github.com> Date: Mon, 2 Feb 2026 10:42:12 -0800 Subject: [PATCH] perf: optimize wrapper_async with CallTypes caching and reduced lookups (#20204) - Cache CallTypes enum values as module-level dict to avoid repeated list comprehension and enum construction on every call - Hoist update_response_metadata getattr lookup to top of function - Guard verbose print_verbose call behind _is_debugging_on() check --- litellm/utils.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/litellm/utils.py b/litellm/utils.py index e67b967d75a..f3d14b455cd 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -199,6 +199,8 @@ from litellm.types.utils import ( all_litellm_params, ) +_CALL_TYPE_ENUM_MAP: dict = {ct.value: ct for ct in CallTypes} + # +-----------------------------------------------+ # | | # | Give Feedback / Get Help | @@ -1746,6 +1748,7 @@ def client(original_function): # noqa: PLR0915 print_args_passed_to_litellm(original_function, args, kwargs) start_time = datetime.datetime.now() result = None + _update_response_metadata = getattr(sys.modules[__name__], "update_response_metadata") logging_obj: Optional[LiteLLMLoggingObject] = kwargs.get( "litellm_logging_obj", None ) @@ -1786,9 +1789,10 @@ def client(original_function): # noqa: PLR0915 ) # [OPTIONAL] CHECK CACHE - print_verbose( - f"ASYNC kwargs[caching]: {kwargs.get('caching', False)}; litellm.cache: {litellm.cache}; kwargs.get('cache'): {kwargs.get('cache', None)}" - ) + if _is_debugging_on(): + print_verbose( + f"ASYNC kwargs[caching]: {kwargs.get('caching', False)}; litellm.cache: {litellm.cache}; kwargs.get('cache'): {kwargs.get('cache', None)}" + ) _caching_handler_response: "Optional[CachingHandlerResponse]" = ( await _llm_caching_handler._async_get_cache( model=model or "", @@ -1864,10 +1868,7 @@ def client(original_function): # noqa: PLR0915 chunks, messages=kwargs.get("messages", None) ) else: - update_response_metadata = getattr( - sys.modules[__name__], "update_response_metadata" - ) - update_response_metadata( + _update_response_metadata( result=result, logging_obj=logging_obj, model=model, @@ -1887,11 +1888,12 @@ def client(original_function): # noqa: PLR0915 rules_obj=rules_obj, ) # Only run if call_type is a valid value in CallTypes - if call_type in [ct.value for ct in CallTypes]: + _call_type_enum = _CALL_TYPE_ENUM_MAP.get(call_type) + if _call_type_enum is not None: result = await async_post_call_success_deployment_hook( request_data=kwargs, response=result, - call_type=CallTypes(call_type), + call_type=_call_type_enum, ) ## Add response to cache @@ -1931,10 +1933,7 @@ def client(original_function): # noqa: PLR0915 end_time=end_time, ) - update_response_metadata = getattr( - sys.modules[__name__], "update_response_metadata" - ) - update_response_metadata( + _update_response_metadata( result=result, logging_obj=logging_obj, model=model,