refactor(guardrails): inject the http handler into the agent 365 initializer instead of assigning it after construction
Some checks failed
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
yucheng 2026-09-27 01:04:44 +00:00
parent a16073b2aa
commit ea1fc1986b
2 changed files with 11 additions and 5 deletions

View file

@ -6,10 +6,16 @@ from litellm.types.guardrails import SupportedGuardrailIntegrations
from .agent_365 import Agent365Guardrail
if TYPE_CHECKING:
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
from litellm.types.guardrails import Guardrail, LitellmParams
def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail") -> Agent365Guardrail:
def initialize_guardrail(
litellm_params: "LitellmParams",
guardrail: "Guardrail",
*,
async_handler: "AsyncHTTPHandler | None" = None,
) -> Agent365Guardrail:
import litellm
from litellm.secret_managers.main import get_secret_str
@ -57,6 +63,7 @@ def initialize_guardrail(litellm_params: "LitellmParams", guardrail: "Guardrail"
client_secret=client_secret,
request_timeout=litellm_params.timeout if litellm_params.timeout is not None else 10.0,
unreachable_fallback=litellm_params.unreachable_fallback,
async_handler=async_handler,
event_hook=litellm_params.mode,
default_on=litellm_params.default_on,
)

View file

@ -257,11 +257,10 @@ class TestInitializeGuardrail:
resource_app_id="00000000-0000-0000-0000-000000000000",
agent_id="yaml-agent",
)
with caplog.at_level(logging.WARNING, logger="LiteLLM Proxy"):
guardrail: Final = initialize_guardrail(params, {"guardrail_name": "a365-stale"})
assert "ignoring api_base, resource_app_id, agent_id" in caplog.text
handler: Final = FakeHandler([_token_response(), _allow_response()])
guardrail.async_handler = handler
with caplog.at_level(logging.WARNING, logger="LiteLLM Proxy"):
guardrail: Final = initialize_guardrail(params, {"guardrail_name": "a365-stale"}, async_handler=handler)
assert "ignoring api_base, resource_app_id, agent_id" in caplog.text
await _run(guardrail, _mcp_data())
token_call, evaluate_call = handler.calls
assert token_call.url == TOKEN_URL