Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix: remove slow string operation (#14955)

Alexsander Hamir2973ff8be958
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 (