From a53366a1c9aa6157f00e35025c027abd9b1b4de1 Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Fri, 23 Jan 2026 15:55:39 -0800 Subject: [PATCH 1/2] perf: Optimize strip_trailing_slash with O(1) index check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace rstrip("/") with direct index check for O(1) performance instead of O(n) string scanning. Results: - strip_trailing_slash: 311ms → 13ms (96% faster) - get_standard_logging_object_payload: 6.11s → 5.80s (5% faster) --- litellm/litellm_core_utils/litellm_logging.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index d3bcfe8200e..cdab4e73a60 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -4651,8 +4651,8 @@ def get_hidden_params( @staticmethod def strip_trailing_slash(api_base: Optional[str]) -> Optional[str]: - if api_base: - return api_base.rstrip("/") + if api_base and api_base[-1] == "/": + return api_base[:-1] return api_base @staticmethod From 6538c13d35782908bf787dc53164512672da05dc Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Fri, 23 Jan 2026 17:08:38 -0800 Subject: [PATCH 2/2] Handle multiple trailing slashes in strip_trailing_slash Use rstrip for correctness when URL ends with "//" or more, otherwise use O(1) index check for single trailing slash. --- litellm/litellm_core_utils/litellm_logging.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index cdab4e73a60..72ebced05d9 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -4651,8 +4651,11 @@ def get_hidden_params( @staticmethod def strip_trailing_slash(api_base: Optional[str]) -> Optional[str]: - if api_base and api_base[-1] == "/": - return api_base[:-1] + if api_base: + if api_base.endswith("//"): + return api_base.rstrip("/") + if api_base[-1] == "/": + return api_base[:-1] return api_base @staticmethod