Replies: 1 comment
-
|
The hooks like If you want to use guardrails in the SDK, you’ll need to explicitly call them within your wrapper logic. For example, you can manually invoke from litellm.integrations.custom_guardrail import CustomGuardrail
from litellm.types.guardrails import GuardrailItem
import litellm
class MyGuardrail(CustomGuardrail):
async def apply_guardrail(self, inputs, request_data, input_type, logging_obj=None):
print("CALLED")
return inputs
guardrail = MyGuardrail(guardrail_name="my_guardrail")
litellm.guardrail_name_config_map = {
"my_guardrail": GuardrailItem(
guardrail_name="my_guardrail",
callbacks=["my_guardrail"]
)
}
# Custom logic to apply guardrails manually
async def guarded_completion():
inputs = {"messages": [{"role": "user", "content": "hello"}]}
preprocessed_inputs = await guardrail.apply_guardrail(inputs, {}, "input_type")
response = litellm.completion(
mode="openai/gpt-4o",
**preprocessed_inputs,
extra_body={"guardrails": ["my_guardrail"]} # This won’t trigger the guardrail
)
return responseIf this is a core feature you need, you might want to open a PR or raise an issue requesting tighter SDK integration with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
When using the liteLLM Python SDK directly (not the proxy), custom guardrails registered via
litellm.guardrail_name_config_map and litellm.callbacks are never triggered.
Expected behavior:
apply_guardrail and async_pre_call_hook should be called when passing : extra_body={"guardrails": ["my_guardrail"]} to litellm.completion
Actual behavior:
Neither apply_guardrail nor async_pre_call_hook are called. Only init is called at startup.
Reproduction :
form litellm.integrations.custom_guardrail import CustomGuardrail
from litellm.types.guardrails import GuardrailItem
import litellm
Class MyGuardrail(CustomGuardrail)
async def apply_guardrail(self, input, request_data, input_type, logging_obj=None):
print("CALLED")
return inputs
guardrail = MyGuardrail(guardrail_name="my_guardrail")
litellm.callbacks = [guardrail]
litellm.guardrail_name_config_map = {
"my_guardrail": GuardrailItem(
guardrail_name="my_guardrail",
callbacks=["my_guardrail"]
)
}
litellm.completion(
mode_"openai/gpt-4o",
messages=[{"role":"user", "content":"hello"}],
extra_body={"guardrails": ["my_guardrail"]}
)
Question:
Is apply_guardrail and other hook supported in the python SDK or only in the proxy ? What is the recommended way to apply custom guardrails with SDK ?
Beta Was this translation helpful? Give feedback.
All reactions