Skip to content

Realtime: only cancel response if necessary #1243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/agents/realtime/openai_realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def __init__(self) -> None:
self._ongoing_response: bool = False
self._tracing_config: RealtimeModelTracingConfig | Literal["auto"] | None = None
self._playback_tracker: RealtimePlaybackTracker | None = None
self._created_session: OpenAISessionObject | None = None

async def connect(self, options: RealtimeModelConfig) -> None:
"""Establish a connection to the model and keep it alive."""
Expand Down Expand Up @@ -349,7 +350,14 @@ async def _send_interrupt(self, event: RealtimeModelSendInterrupt) -> None:
int(elapsed_ms),
)
await self._send_raw_message(converted)
await self._cancel_response()

automatic_response_cancellation_enabled = (
self._created_session
and self._created_session.turn_detection
and self._created_session.turn_detection.interrupt_response
)
if not automatic_response_cancellation_enabled:
await self._cancel_response()

self._audio_state_tracker.on_interrupted()
if self._playback_tracker:
Expand Down Expand Up @@ -483,6 +491,9 @@ async def _handle_ws_event(self, event: dict[str, Any]):
await self._emit_event(RealtimeModelTurnEndedEvent())
elif parsed.type == "session.created":
await self._send_tracing_config(self._tracing_config)
self._update_created_session(parsed.session) # type: ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into the ways to make the type compatible here, but found it's way more complicated than I expect and dealing with it does not bring any benefits to the code... 😅 So, having ignore comments for these is okay for now.

elif parsed.type == "session.updated":
self._update_created_session(parsed.session) # type: ignore
elif parsed.type == "error":
await self._emit_event(RealtimeModelErrorEvent(error=parsed.error))
elif parsed.type == "conversation.item.deleted":
Expand Down Expand Up @@ -532,6 +543,13 @@ async def _handle_ws_event(self, event: dict[str, Any]):
):
await self._handle_output_item(parsed.item)

def _update_created_session(self, session: OpenAISessionObject) -> None:
self._created_session = session
if session.output_audio_format:
self._audio_state_tracker.set_audio_format(session.output_audio_format)
if self._playback_tracker:
self._playback_tracker.set_audio_format(session.output_audio_format)

async def _update_session_config(self, model_settings: RealtimeSessionModelSettings) -> None:
session_config = self._get_session_config(model_settings)
await self._send_raw_message(
Expand Down