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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
refactor: reuse get_instance_fn in initialize_custom_guardrail - allow module level import (#20917)
Otavio Britob1a67666ea4c
diff --git a/litellm/proxy/guardrails/guardrail_registry.py b/litellm/proxy/guardrails/guardrail_registry.pyindex c3da6892209..c0903a35b6d 100644--- a/litellm/proxy/guardrails/guardrail_registry.py+++ b/litellm/proxy/guardrails/guardrail_registry.py@@ -12,6 +12,7 @@ from litellm._uuid import uuidfrom litellm.integrations.custom_guardrail import CustomGuardrailfrom litellm.litellm_core_utils.safe_json_dumps import safe_dumpsfrom litellm.proxy.utils import PrismaClient+from litellm.proxy.types_utils.utils import get_instance_fnfrom litellm.secret_managers.main import get_secretfrom litellm.types.guardrails import (Guardrail,@@ -489,7 +490,7 @@ class InMemoryGuardrailHandler:config_file_path: Optional[str] = None,) -> Optional[CustomGuardrail]:"""- Initialize a Custom Guardrail from a python file+ Initialize a Custom Guardrail from a python file or module pathThis initializes it by adding it to the litellm callback manager"""@@ -498,26 +499,12 @@ class InMemoryGuardrailHandler:"GuardrailsAIException - Please pass the config_file_path to initialize_guardrails_v2")- _file_name, _class_name = guardrail_type.split(".")verbose_proxy_logger.debug(- "Initializing custom guardrail: %s, file_name: %s, class_name: %s",+ "Initializing custom guardrail: %s",guardrail_type,- _file_name,- _class_name,)- directory = os.path.dirname(config_file_path)- module_file_path = os.path.join(directory, _file_name) + ".py"-- spec = importlib.util.spec_from_file_location(_class_name, module_file_path) # type: ignore- if not spec:- raise ImportError(- f"Could not find a module specification for {module_file_path}"- )-- module = importlib.util.module_from_spec(spec) # type: ignore- spec.loader.exec_module(module) # type: ignore- _guardrail_class = getattr(module, _class_name)+ _guardrail_class = get_instance_fn(guardrail_type, config_file_path=config_file_path)mode = litellm_params.modeif mode is None: