From 2973ff8be958961bb0a8f8db154fac30b3ab5e88 Mon Sep 17 00:00:00 2001 From: Alexsander Hamir Date: Fri, 26 Sep 2025 13:25:59 -0700 Subject: [PATCH] fix: remove slow string operation (#14955) * fix: remove slow string operation * fix: behavior change * fix: behavior change * fix: avoid default call --- litellm/litellm_core_utils/rules.py | 5 +++++ litellm/utils.py | 17 +++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/litellm/litellm_core_utils/rules.py b/litellm/litellm_core_utils/rules.py index beeb012d032..717ff55ab22 100644 --- a/litellm/litellm_core_utils/rules.py +++ b/litellm/litellm_core_utils/rules.py @@ -23,6 +23,11 @@ class Rules: def __init__(self) -> None: pass + @staticmethod + def has_pre_call_rules() -> bool: + """Check if any pre-call rules are configured""" + return len(litellm.pre_call_rules) > 0 + def pre_call_rules(self, input: str, model: str): for rule in litellm.pre_call_rules: if callable(rule): diff --git a/litellm/utils.py b/litellm/utils.py index 0721b023d2b..cb340735b4e 100644 --- a/litellm/utils.py +++ b/litellm/utils.py @@ -7,6 +7,7 @@ # # Thank you users! We ❤️ you! - Krrish & Ishaan +from io import StringIO import ast import asyncio import base64 @@ -724,17 +725,21 @@ def function_setup( # noqa: PLR0915 messages = kwargs["messages"] ### PRE-CALL RULES ### if ( - isinstance(messages, list) + Rules.has_pre_call_rules() + and isinstance(messages, list) and len(messages) > 0 and isinstance(messages[0], dict) and "content" in messages[0] ): + + buffer = StringIO() + for m in messages: + content = m.get("content", "") + if content is not None and isinstance(content, str): + buffer.write(content) + rules_obj.pre_call_rules( - input="".join( - m.get("content", "") - for m in messages - if "content" in m and isinstance(m["content"], str) - ), + input=buffer.getvalue(), model=model, ) elif (