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
This commit is contained in:
ryan-crabbe 2026-02-02 10:42:12 -08:00 committed by GitHub
parent ade35a3f9a
commit fadc04fbe2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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,