Fix guardrails failing when interrupt_response=True #1949
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1907
When
turn_detection.interrupt_response
is set toTrue
and a guardrail triggers an interrupt, the SDK was only sendingresponse.interrupt
but notresponse.cancel
. This caused the API to think the response was still active, leading to "Conversation already has an active response" errors on subsequent requests.Root Cause
interrupt_response=True
tells the API to auto-cancel on user voice interruptsresponse.cancel
ifinterrupt_response=True
, incorrectly assuming the API would handle itSolution
force_cancel
parameter toRealtimeModelSendInterrupt
force_cancel=True
for guardrail interruptsresponse.cancel
whenforce_cancel=True
, regardless ofinterrupt_response
settinginterrupt_response
settingFiles Changed
src/agents/realtime/model_inputs.py
: Addforce_cancel
parameter with documentationsrc/agents/realtime/session.py
: Passforce_cancel=True
for guardrail interruptssrc/agents/realtime/openai_realtime.py
: Checkforce_cancel
in_send_interrupt()
methodTesting
Comprehensive test suite included in branch:
tests/realtime/test_issue_1907_guardrail_interrupt.py
(5 test cases)Tests verify:
interrupt_response=True
(core fix)interrupt_response=False
(baseline)interrupt_response=True
(edge case)All existing tests pass:
Backward Compatibility
✅ Fully backward compatible
force_cancel
defaults toFalse
, preserving existing behavior for user voice interruptsforce_cancel=True