perf: skip Pydantic model construction in get_api_base when api_base is in dict

Add fast path to check optional_params.get("api_base") directly before
constructing a full LiteLLM_Params Pydantic model. When api_base is present
(the common case via router), return it immediately — avoiding ~29µs of
Pydantic validation overhead per request.

Profiled: get_api_base 30.5µs/call → 1.2µs/call (-96%)
This commit is contained in:
Ryan Crabbe 2026-01-28 17:30:41 -08:00
parent d12ce3cd5d
commit f655f47d84

View file

@ -28,6 +28,13 @@ def get_api_base(
```
"""
# Fast path: if api_base is already in the dict, return it directly
# without constructing a full LiteLLM_Params Pydantic model.
if isinstance(optional_params, dict):
_api_base = optional_params.get("api_base")
if _api_base is not None:
return _api_base
try:
if isinstance(optional_params, LiteLLM_Params):
_optional_params = optional_params