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_responseis set toTrueand a guardrail triggers an interrupt, the SDK was only sendingresponse.interruptbut 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=Truetells the API to auto-cancel on user voice interruptsresponse.cancelifinterrupt_response=True, incorrectly assuming the API would handle itSolution
force_cancelparameter toRealtimeModelSendInterruptforce_cancel=Truefor guardrail interruptsresponse.cancelwhenforce_cancel=True, regardless ofinterrupt_responsesettinginterrupt_responsesettingFiles Changed
src/agents/realtime/model_inputs.py: Addforce_cancelparameter with documentationsrc/agents/realtime/session.py: Passforce_cancel=Truefor guardrail interruptssrc/agents/realtime/openai_realtime.py: Checkforce_cancelin_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_canceldefaults toFalse, preserving existing behavior for user voice interruptsforce_cancel=True