fix: remove slow string operation (#14955)

* fix: remove slow string operation

* fix: behavior change

* fix: behavior change

* fix: avoid default call
This commit is contained in:
Alexsander Hamir 2025-09-26 13:25:59 -07:00 committed by GitHub
parent 35930d7ccb
commit 2973ff8be9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 6 deletions

View file

@ -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):

View file

@ -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 (