Skip to content

OpenAI Realtime

The OpenAI Realtime API is a speech-to-speech interface that enables low-latency, natural voice conversations with AI. Key features include:

  • Bidirectional Interaction: The user and the model can provide input and output at the same time.
  • Interruptibility: Allows users to interrupt the AI mid-response, like in human conversations.
  • Multimodal Streaming: The API supports streaming of text and audio data.
  • Tool Use and Function Calling: Can use external tools to perform actions and get context while maintaining a real-time connection.
  • Secure Authentication: Uses tokens for secure client-side authentication.

OpenAI Realtime is configured as an optional dependency in Strands Agents.

To install it, run:

Terminal window
pip install 'strands-agents[bidi-io,bidi-openai,bidi-pyaudio]'

Or to install all bidirectional streaming providers at once:

Terminal window
pip install 'strands-agents[bidi-all,bidi-pyaudio]'

After installing the OpenAI Realtime and local audio extras, create a voice agent:

import asyncio
from strands.experimental.bidi.agent import BidiAgent
from strands.experimental.bidi.io import BidiAudioIO
from strands.experimental.bidi.models import OpenAIRealtimeModel
from strands.experimental.tools import stop
from strands.vended_tools import notebook
async def main() -> None:
model = OpenAIRealtimeModel(
model_id="gpt-realtime",
voice="coral",
api_key="<OPENAI_API_KEY>",
)
# stop tool allows user to verbally stop agent execution.
agent = BidiAgent(model=model, tools=[notebook, stop])
audio_io = BidiAudioIO()
await agent.run(inputs=[audio_io.input()], outputs=[audio_io.output()])
if __name__ == "__main__":
asyncio.run(main())
ParameterDescriptionExampleOptions
api_keyOpenAI API key used for authenticationsk-...reference
organizationOrganization associated with the connection. Used for authentication if required.myorgreference
projectProject associated with the connection. Used for authentication if required.myprojreference
timeout_sOpenAI documents a 60 minute limit on realtime sessions (docs). However, OpenAI does not emit any warnings when approaching the limit. As a workaround, we allow users to configure a timeout (in seconds) on the client side to gracefully handle the connection closure.3000[1, 3000] (in seconds)
ParameterDescriptionExampleOptions
model_idOpenAI Realtime model identifier."gpt-realtime"OpenAI models
voiceOutput voice identifier. Defaults to "alloy"."coral"Voice options
paramsOpenAI Realtime session parameters. Audio must remain mono PCM at 24000 Hz.{"max_output_tokens": 4096}session.update
connectionReconnect timing overrides.{"auto_reconnect": false}reference

Use direct options such as voice for common settings, and pass additional OpenAI Realtime options through params.

from strands.experimental.bidi.models import OpenAIRealtimeModel
model = OpenAIRealtimeModel(
api_key="<OPENAI_API_KEY>",
voice="coral",
params={"audio": {"input": {"turn_detection": {"threshold": 0.3}}}},
)

Nested dictionaries in params merge with the existing configuration, preserving unspecified fields. If a setting overlaps with a default or direct option, params takes precedence.

Calling update_config(params=...) replaces the entire params dictionary. The new values take effect on the next start() or restart().

The OpenAI Realtime API exposes no server-side resume handle, so when a connection nears its time limit, BidiAgent restarts it and replays the accumulated conversation history into the new connection. The provider then sends a short system instruction so the model continues in the same language without re-introducing itself. The agent emits a BidiConnectionRestartEvent when this happens; treat it as informational rather than an error.

Set timeout_s in the client config to control when the client-side timeout fires ahead of OpenAI’s session limit (see the table above). For the full lifecycle, see Connection Restart.

If you encounter the error ModuleNotFoundError: No module named 'websockets', this means the WebSocket dependency hasn’t been properly installed in your environment. To fix this, run pip install 'strands-agents[bidi-openai]'.

Set the OPENAI_API_KEY environment variable or pass the key through api_key.