Skip to content

Commit 7c69d96

Browse files
committed
fix(proxy): scope per-chunk streaming hook skip to streamable post_call pipelines
The per-chunk async_post_call_streaming_hook was filtering pipeline-managed guardrails against the full post_call step set, so a guardrail whose pipeline was non-streamable (its step lacks apply_guardrail, e.g. a legacy per-chunk override) was skipped there and also never run by _pipeline_gated_stream, which only wraps the streamable subset. That dropped default_on or policy-added per-chunk hooks from scanning streamed output. Match async_post_call_streaming_iterator_hook and filter against _streamable_post_call_pipelines instead, so non-streamable pipeline steps still run their per-chunk hook on the stream.
1 parent cecd481 commit 7c69d96

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

‎litellm/proxy/utils.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3272,7 +3272,9 @@ async def async_post_call_streaming_hook(
32723272
_cached_guardrail_data: dict | None = None
32733273
_guardrail_data_computed = False
32743274
pipeline_managed: Final = (
3275-
_pipeline_managed_guardrail_names(data, "post_call") if caps.has_guardrail else frozenset()
3275+
_pipeline_step_guardrail_names(_streamable_post_call_pipelines(data, user_api_key_dict))
3276+
if caps.has_guardrail
3277+
else frozenset()
32763278
)
32773279

32783280
for callback in litellm.callbacks:

‎tests/test_litellm/proxy/utils/proxy_logging/test_guardrail_pipeline.py‎

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,20 +2118,28 @@ async def process_output_streaming_response(
21182118

21192119

21202120
@pytest.mark.asyncio
2121-
async def test_per_chunk_streaming_hook_skips_pipeline_managed_guardrail(
2121+
async def test_per_chunk_streaming_hook_skips_streamable_pipeline_managed_guardrail(
21222122
proxy_logging, make_user_api_key_auth, monkeypatch
21232123
):
21242124
seen: Dict[str, Any] = {}
21252125

2126-
class RecordingGuardrail(CustomGuardrail):
2126+
class StreamableRecordingGuardrail(CustomGuardrail):
2127+
async def apply_guardrail(self, inputs, request_data, input_type, logging_obj=None):
2128+
return inputs
2129+
21272130
async def async_post_call_streaming_hook(self, user_api_key_dict, response):
21282131
seen[self.guardrail_name] = seen.get(self.guardrail_name, 0) + 1
21292132
return None
21302133

2131-
managed = RecordingGuardrail(
2134+
class FreeRecordingGuardrail(CustomGuardrail):
2135+
async def async_post_call_streaming_hook(self, user_api_key_dict, response):
2136+
seen[self.guardrail_name] = seen.get(self.guardrail_name, 0) + 1
2137+
return None
2138+
2139+
managed = StreamableRecordingGuardrail(
21322140
guardrail_name="gr-post", event_hook=GuardrailEventHooks.post_call, default_on=True
21332141
)
2134-
free = RecordingGuardrail(
2142+
free = FreeRecordingGuardrail(
21352143
guardrail_name="gr-free", event_hook=GuardrailEventHooks.post_call, default_on=True
21362144
)
21372145
monkeypatch.setattr(litellm, "callbacks", [managed, free])
@@ -2147,3 +2155,36 @@ async def async_post_call_streaming_hook(self, user_api_key_dict, response):
21472155
assert result is not None
21482156
assert seen.get("gr-post") is None
21492157
assert seen["gr-free"] == 1
2158+
2159+
2160+
@pytest.mark.asyncio
2161+
async def test_per_chunk_streaming_hook_runs_non_streamable_pipeline_managed_guardrail(
2162+
proxy_logging, make_user_api_key_auth, monkeypatch
2163+
):
2164+
"""Regression: a pipeline step whose guardrail only overrides the per-chunk
2165+
hook (no ``apply_guardrail``) leaves its pipeline non-streamable, so the
2166+
gated stream cannot govern it. The per-chunk hook must still run against
2167+
the stream; otherwise the guardrail is dropped entirely on streaming
2168+
responses even though it ran before pipelines existed."""
2169+
seen: Dict[str, Any] = {}
2170+
2171+
class RecordingGuardrail(CustomGuardrail):
2172+
async def async_post_call_streaming_hook(self, user_api_key_dict, response):
2173+
seen[self.guardrail_name] = seen.get(self.guardrail_name, 0) + 1
2174+
return None
2175+
2176+
managed = RecordingGuardrail(
2177+
guardrail_name="gr-post", event_hook=GuardrailEventHooks.post_call, default_on=True
2178+
)
2179+
monkeypatch.setattr(litellm, "callbacks", [managed])
2180+
monkeypatch.setattr("litellm.proxy.proxy_server.llm_router", None, raising=False)
2181+
data = _post_call_pipeline_data(stream=True)
2182+
2183+
result = await proxy_logging.async_post_call_streaming_hook(
2184+
data=data,
2185+
response=_stream_chunks()[0],
2186+
user_api_key_dict=make_user_api_key_auth(request_route="/v1/chat/completions"),
2187+
)
2188+
2189+
assert result is not None
2190+
assert seen["gr-post"] == 1

0 commit comments

Comments
 (0)