InputGuardrail(parallel=True) doesn't cancel a streamed run
Author: yanekwdCreated Sep 28, 2026Updated Oct 1, 2026
Labelsbugpydanty:bugp:1-highestneeds-maintainer-action
Capability
Guardrails
Bug Description
Hey,
we're building a chatbot that enforces company policies on user prompts. The check uses OpenAI's gpt-oss-safeguard model inside an InputGuardrail guard. That call takes about a second, so we use parallel=True.
The guard correctly returns GuardrailResult.block(...), but in streamed runs the model call isn't cancelled. The first model response keeps streaming to the client until it's complete, and only then does the run end, with SkipModelRequest raised out of the stream rather than the block message. The docs say "The model call is cancelled the moment the guard reports a violation." tho.
Minimal Reproduction
asyncio
from pydantic_ai import Agent
from pydantic_ai.models.function import FunctionModel
from pydantic_ai_harness import GuardrailResult, InputGuardrail
async def stream(messages, info):
for i in range(20):
await asyncio.sleep(0.1)
yield f"chunk{i} "
async def guard(ctx, prompt):
await asyncio.sleep(0.2)
return GuardrailResult.block("blocked")
agent = Agent(
FunctionModel(stream_function=stream),
capabilities=[InputGuardrail(guard=guard, parallel=True)],
)
async def main():
async for event in agent.run_stream_events("hi"):
print(event) # all 20 chunks print before the block surfaces
asyncio.run(main())
Expected: The stream stops shortly after the guard blocks (after about 2 chunks here), and the run ends with the block message.
Actual: All 20 chunks stream first. Then the run fails with SkipModelRequest instead of ending with the block message.pydantic-ai-harness version
0.34.0
pydantic-ai version
2.49
Source: pydantic/pydantic-ai