Add realtime speech-to-speech API support
Closed
KL
8mo ago+4104+4104+0123456789012345678901234567890123456789-7-7-0123456789
Summary
- Move realtime code from
models/realtime.py,models/openai_realtime.py, andagent/voice.pyinto a top-levelpydantic_ai/realtime/package (likeembeddings/) - Add Gemini Live as a second provider, validating the abstraction works for multiple providers
- Add docs page at
docs/realtime.md
Package structure
pydantic_ai/realtime/
__init__.py -- re-exports from _base + _session, explicit __all__
_base.py -- types + ABCs (RealtimeModel, RealtimeConnection, events, inputs)
_session.py -- RealtimeSession wrapper with automatic tool execution
openai.py -- OpenAI Realtime provider (WebSocket)
gemini.py -- Gemini Live provider (google-genai SDK)
Gemini Live provider
GeminiRealtimeModel supports both Google AI (API key) and Vertex AI (project/location). Uses client.aio.live.connect() for bidirectional streaming with audio, image, and tool support.
Test plan
- Unit tests for
map_event(OpenAI) andmap_message(Gemini) - all event types - Connection send tests for audio, images, and tool results
-
RealtimeSessionpassthrough and tool dispatch tests -
Agent.realtime_session()integration tests (tool wiring, instructions, unknown tools) - Pyright + ruff clean
- Manual integration test with real APIs
Closes #1447