Add realtime speech-to-speech API support

Closed
KL
8mo ago
+4104-7

Summary

  • Move realtime code from models/realtime.py, models/openai_realtime.py, and agent/voice.py into a top-level pydantic_ai/realtime/ package (like embeddings/)
  • 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) and map_message (Gemini) - all event types
  • Connection send tests for audio, images, and tool results
  • RealtimeSession passthrough 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