Skip to content

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]

Merge branch 'litellm_internal_staging' of https://github.com/BerriAI/litellm into litellm_fix_guardrail_event_hook_resync

mateo-berrib327437d47c7
diff --cc tests/test_litellm/proxy/guardrails/test_guardrail_registry.py
index f5221578faa,24742e1bac2..3d1937c5914
--- a/tests/test_litellm/proxy/guardrails/test_guardrail_registry.py
+++ b/tests/test_litellm/proxy/guardrails/test_guardrail_registry.py
@@@ -560,6 -487,144 +561,148 @@@ def test_repeated_db_sync_does_not_accu
promote_into_request_lists()
assert distinct_runner_instances() == 1
+ finally:
+ for cb_list, snapshot in zip(lists, snapshots):
+ cb_list[:] = snapshot
+
+
+ PRESIDIO_SIBLINGS_GID = "55555555-5555-5555-5555-555555555555"
+ PRESIDIO_SIBLINGS_NAME = "presidio-siblings"
+
+
+ def _presidio_db_guardrail(pii_entities_config: dict[str, str]) -> Guardrail:
+ return Guardrail(
+ guardrail_id=PRESIDIO_SIBLINGS_GID,
+ guardrail_name=PRESIDIO_SIBLINGS_NAME,
+ litellm_params={
+ "guardrail": "presidio",
+ "mode": "pre_call",
+ "default_on": True,
+ "output_parse_pii": True,
+ "presidio_filter_scope": "both",
+ "presidio_analyzer_api_base": "https://fakelink.com/v1/presidio/analyze",
+ "presidio_anonymizer_api_base": "https://fakelink.com/v1/presidio/anonymize",
+ "pii_entities_config": pii_entities_config,
+ },
+ )
+
+
+ def _presidio_callbacks_in(cb_list: Iterable[object]) -> list[CustomGuardrail]:
+ return [
+ callback
+ for callback in cb_list
+ if isinstance(callback, CustomGuardrail) and getattr(callback, "guardrail_name", None) == PRESIDIO_SIBLINGS_NAME
+ ]
+
+
+ def test_presidio_siblings_are_tracked_and_deleted_together():
+ """
+ A presidio guardrail scoped to both stages registers the pre_call primary plus
+ the post_call unmask and mask-output siblings. Deleting the guardrail must remove
+ all three from every callback list, not just the primary.
+ """
+ import litellm
+
+ handler = InMemoryGuardrailHandler()
+ lists = _all_callback_lists()
+ snapshots = [list(cb_list) for cb_list in lists]
+ try:
+ handler.initialize_guardrail(_presidio_db_guardrail({"EMAIL_ADDRESS": "MASK"}))
+
+ registered = _presidio_callbacks_in(litellm.callbacks)
+ assert len(registered) == 3
+ primary = handler.guardrail_id_to_custom_guardrail[PRESIDIO_SIBLINGS_GID]
+ siblings = handler.guardrail_id_to_sibling_callbacks[PRESIDIO_SIBLINGS_GID]
+ assert primary is registered[0]
+ assert siblings == tuple(registered[1:])
+ assert [sibling.event_hook for sibling in siblings] == [GuardrailEventHooks.post_call] * 2
+
+ for cb_list in lists[1:]:
+ cb_list.extend(registered)
+
+ handler.delete_in_memory_guardrail(PRESIDIO_SIBLINGS_GID)
+
+ for cb_list in lists:
+ assert _presidio_callbacks_in(cb_list) == []
+ assert PRESIDIO_SIBLINGS_GID not in handler.guardrail_id_to_custom_guardrail
+ assert PRESIDIO_SIBLINGS_GID not in handler.guardrail_id_to_sibling_callbacks
+ finally:
+ for cb_list, snapshot in zip(lists, snapshots):
+ cb_list[:] = snapshot
+
+
-def test_update_in_memory_guardrail_reaches_presidio_siblings_and_keeps_their_stage():
++def test_update_in_memory_guardrail_rebuilds_presidio_siblings_and_keeps_their_stage():
+ import litellm
+
+ handler = InMemoryGuardrailHandler()
+ lists = _all_callback_lists()
+ snapshots = [list(cb_list) for cb_list in lists]
+ try:
+ handler.initialize_guardrail(_presidio_db_guardrail({"EMAIL_ADDRESS": "MASK", "IP_ADDRESS": "MASK"}))
+ tracked = _presidio_callbacks_in(litellm.callbacks)
+ roles_before = [
+ (callback.apply_to_output, callback.output_parse_pii, callback.event_hook) for callback in tracked
+ ]
+ assert roles_before == [
+ (False, True, [GuardrailEventHooks.pre_call, GuardrailEventHooks.post_call]),
+ (False, True, GuardrailEventHooks.post_call),
+ (True, False, GuardrailEventHooks.post_call),
+ ]
+
+ updated = Guardrail(
+ guardrail_id=PRESIDIO_SIBLINGS_GID,
+ guardrail_name=PRESIDIO_SIBLINGS_NAME,
+ litellm_params=LitellmParams(
+ guardrail="presidio",
+ mode="pre_call",
+ default_on=True,
+ output_parse_pii=True,
+ presidio_filter_scope="both",
+ presidio_analyzer_api_base="https://fakelink.com/v1/presidio/analyze",
+ presidio_anonymizer_api_base="https://fakelink.com/v1/presidio/anonymize",
+ pii_entities_config={"EMAIL_ADDRESS": "MASK"},
+ ),
+ )
+ handler.update_in_memory_guardrail(guardrail_id=PRESIDIO_SIBLINGS_GID, guardrail=updated)
+
- assert [callback.pii_entities_config for callback in tracked] == [{"EMAIL_ADDRESS": "MASK"}] * 3
++ rebuilt = _presidio_callbacks_in(litellm.callbacks)
++ assert len(rebuilt) == 3
++ assert [callback.pii_entities_config for callback in rebuilt] == [{"EMAIL_ADDRESS": "MASK"}] * 3
+ assert [
- (callback.apply_to_output, callback.output_parse_pii, callback.event_hook) for callback in tracked
++ (callback.apply_to_output, callback.output_parse_pii, callback.event_hook) for callback in rebuilt
+ ] == roles_before
- assert _presidio_callbacks_in(litellm.callbacks) == tracked
++ assert not any(previous in rebuilt for previous in tracked)
++ assert handler.guardrail_id_to_custom_guardrail[PRESIDIO_SIBLINGS_GID] is rebuilt[0]
++ assert handler.guardrail_id_to_sibling_callbacks[PRESIDIO_SIBLINGS_GID] == tuple(rebuilt[1:])
+ finally:
+ for cb_list, snapshot in zip(lists, snapshots):
+ cb_list[:] = snapshot
+
+
+ def test_repeated_db_sync_replaces_presidio_siblings_instead_of_leaking_stale_ones():
+ """
+ The callback manager dedupes custom loggers by their scalar attributes, so a
+ leaked post_call sibling blocks the re-initialized sibling from registering and
+ keeps serving the previous entity config. After every DB re-sync, each callback
+ list must hold exactly the three current instances, all on the latest config.
+ """
+ import litellm
+
+ handler = InMemoryGuardrailHandler()
+ lists = _all_callback_lists()
+ snapshots = [list(cb_list) for cb_list in lists]
+ try:
+ entity_configs = [{"EMAIL_ADDRESS": "MASK"}, {"EMAIL_ADDRESS": "MASK", "IP_ADDRESS": "MASK"}]
+ for cycle in range(4):
+ latest = entity_configs[cycle % 2]
+ handler.sync_guardrail_from_db(_presidio_db_guardrail(latest))
+ for cb_list in lists[1:]:
+ cb_list.extend(_presidio_callbacks_in(litellm.callbacks))
+
+ for cb_list in lists:
+ current = _presidio_callbacks_in(cb_list)
+ assert len({id(callback) for callback in current}) == 3
+ assert all(callback.pii_entities_config == latest for callback in current)
finally:
for cb_list, snapshot in zip(lists, snapshots):
cb_list[:] = snapshot