From f902f7eaebb621c787ab45238b8fc395667fccb3 Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Thu, 19 Feb 2026 16:57:49 -0800 Subject: [PATCH] perf: short-circuit is_model_o_series_model with startswith before set lookup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reorder the check to use str.startswith(tuple) first, which immediately returns False for non-o-series models (the common case), avoiding the genexpr + 198-element set lookup. Line profiling shows 4.75x speedup (13.6µs → 2.9µs per call, 2.45s → 0.52s across 180k calls). --- litellm/llms/openai/chat/o_series_transformation.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/litellm/llms/openai/chat/o_series_transformation.py b/litellm/llms/openai/chat/o_series_transformation.py index 30647f58687..6ef43ec5bfd 100644 --- a/litellm/llms/openai/chat/o_series_transformation.py +++ b/litellm/llms/openai/chat/o_series_transformation.py @@ -131,9 +131,7 @@ class OpenAIOSeriesConfig(OpenAIGPTConfig): def is_model_o_series_model(self, model: str) -> bool: model = model.split("/")[-1] # could be "openai/o3" or "o3" - return model in litellm.open_ai_chat_completion_models and any( - model.startswith(pfx) for pfx in ("o1", "o3", "o4") - ) + return model.startswith(("o1", "o3", "o4")) and model in litellm.open_ai_chat_completion_models @overload def _transform_messages( @@ -173,4 +171,4 @@ class OpenAIOSeriesConfig(OpenAIGPTConfig): else: return super()._transform_messages( messages, model, is_async=cast(Literal[False], False) - ) + ) \ No newline at end of file