Cancel the streamed model request when a parallel `InputGuardrail` blocks
With InputGuardrail(parallel=True), a block that lands while the model is streaming used to leave the caller pulling every remaining chunk, after which run_stream(), run_stream_events() and iter() + node.stream() raised the raw SkipModelRequest. Now the model stream is cancelled as soon as the guard blocks, and the run ends with the guard's block message, the same result a non-streamed run gives.
- Closes #9279
What changed
The guard cancels the streaming handler task. That task never drove the stream, though: the caller pulls events from its own task. So the handler now stops the stream before it closes:
ModelRequestNode.stream(): if the handler is torn down while the caller is still consuming, it callsAgentStream._abandon_model_stream(). At the end, aSkipModelRequestfromwrap_model_requestends the request with its response, as it already does before the stream opens.AgentStream: model events now come through a small layer that cancels and drains an in-flight pull. That closes the provider stream before the handler tears it down, instead of racing it. The caller's iteration then continues with a replay of the responsewrap_model_requestfinished with. A repeatedPartStartEventindex replaces the earlier part, so UIs show the block message, andrun_stream()'sget_output(),all_messages()andstream_text(delta=False)return it. Ifwrap_model_requestraises another error instead, that error is raised from the stream, so the caller can't settle on the cut-short output.ModelRetryends the stream and retries the request, as before.
This lives in core because it applies to any wrap_model_request that stops its handler mid-stream (for example one that cancels it and returns a fallback). The guardrail code itself is unchanged. Runs where the handler is not stopped mid-stream go through the same code as before.
I used #9289 as a reference. Instead of a watcher task closing the whole event chain, this cancels only the model pull. The capability event-stream chain stays intact, and the replayed block message flows through it.
Limitations (follow-up)
- Chunks sent before the verdict still reach the caller. This is cancel-on-block, not buffer-until-verdict: whatever the model streamed before the guard blocked has already been delivered, and the block message then replaces it. Holding the stream back until the verdict is a separate follow-up, as discussed on the issue.
- A block that lands after the caller has received the whole response still raises
SkipModelRequest, as on main. By thenrun_stream()may already have settled on the model's output, so turning the block into a normal ending would silently return output the guard rejected. That case is left unchanged, and the buffering follow-up would fix it.
docs/harness/guardrails.md now states both caveats for streamed parallel=True runs.
Tests
tests/harness/guardrails/test_input_guardrail.py::TestInputGuardrailParallelStreaming: the model sends one chunk and then stalls, so these tests time out on main and pass here. They cover:run_stream_events,run_stream(bothstream_text()modes) anditer+node.stream: each ends with the block message, closes the model stream and leaves no dangling tasks.- A block before the first pull: the model is never pulled.
- A hard-fail guard error mid-stream:
get_output()raises it and never returns the cut-short output. - A parallel guard that allows: every chunk is still streamed.
- A late block after the whole response: still raises.
tests/test_capability_hooks.py::TestSkipModelRequestInteraction::test_wrap_model_request_stopping_its_handler_mid_stream: the generic core seam, for awrap_model_requestthat stops its handler and either returns a response or raisesSkipModelRequest, and theModelRetryvariant, which retries the request.- The sequential guard tests (for example
test_block_under_run_stream) are unchanged and pass.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.