fix: properly handle custom guardrails parameters (#18978)

This commit is contained in:
Harshit Jain
2026-01-14 03:23:54 +05:30
committed by GitHub
parent ebeea46fc4
commit 181c626d83
@@ -526,10 +526,24 @@ class InMemoryGuardrailHandler:
)
default_on = litellm_params.default_on
# Extract additional params from litellm_params to pass to custom guardrail
# This matches the behavior of other guardrail initializers (e.g., initialize_lakera)
# and aligns with the documented behavior for custom guardrails
if hasattr(litellm_params, "model_dump"):
extra_params = litellm_params.model_dump(exclude_none=True)
else:
extra_params = dict(litellm_params) if litellm_params else {}
# Remove params that are handled explicitly or are internal
for key in ["guardrail", "mode", "default_on"]:
extra_params.pop(key, None)
_guardrail_callback = _guardrail_class(
guardrail_name=guardrail["guardrail_name"],
event_hook=mode,
default_on=default_on,
**extra_params,
)
litellm.logging_callback_manager.add_litellm_callback(_guardrail_callback) # type: ignore