python Pydantic AI Releases · · 2.40.0

pydantic-ai v2.40.0 enhances real-time AI session control and agent events

sdkaigaengineerpython-agentic
feature patch

Pydantic-ai v2.40.0 introduces several new features aimed at improving real-time AI agent interactions and session management. Key additions include background price updates, robust barge-in handling, and an event listener system for agents. Developers can now enqueue out-of-band prompts and better control session flow, alongside numerous bug fixes that improve reliability and API mapping for services like OpenAI.

  • Add pydantic_ai.prices.update_in_background() by @adtyavrdhn in https://github.com/pydantic/pydantic-ai/pull/4841
  • Add provider_factory to infer_realtime_model by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8106
  • Add @agent.on_event for registering event listeners on an Agent by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8101
  • Map finish_reason on OpenAI Responses stream terminal events by @pydanty in https://github.com/pydantic/pydantic-ai/pull/7916
  • Stop sending reasoning to the dated gpt-realtime-2025-08-28 snapshot by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8103
Features (6)
  • Add pydantic_ai.prices.update_in_background() by @adtyavrdhn in https://github.com/pydantic/pydantic-ai/pull/4841
  • Handle barge-in in the session: handle_barge_in=True, interrupt(played_bytes=...), and played_audio_bytes by @DouweM in https://github.com/pydantic/pydantic-ai/pull/7870

  • Add provider_factory to infer_realtime_model by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8106
  • Add @agent.on_event for registering event listeners on an Agent by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8101
  • Add respond= to RealtimeSession.send() and document that a text turn solicits a reply by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8110

  • Add RealtimeSession.enqueue() for out-of-band prompts from code driving the session by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8109

Fixes (10)
  • Map finish_reason on OpenAI Responses stream terminal events by @pydanty in https://github.com/pydantic/pydantic-ai/pull/7916
  • Fix defer_loading reveal synthesis splitting a parallel batch's tool_result from its tool_use by @pydanty in https://github.com/pydantic/pydantic-ai/pull/7879

  • fix(realtime): roll back user-turn state when an audio chunk is not bytes-like by @simpleqt in https://github.com/pydantic/pydantic-ai/pull/7905

  • Let a capability cache derived state again, and refuse a collection the merge cannot rebuild by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8099

  • Make a capability's auto-assigned registry key look like the run-local handle it is by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8119

  • Stop sending reasoning to the dated gpt-realtime-2025-08-28 snapshot by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8103
  • Subscribe stream_audio() and stream_transcripts() views when they are called, not on first iteration by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8107

  • End the realtime audio and transcript views when a tool fails and nobody is iterating by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8108

  • Let a realtime tool hang up with ctx.realtime_session.close() instead of wedging itself by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8105

  • Fix the realtime "Speaking first" and relay examples, the interrupt(played_ms=) cancel race, and two provider tables by @DouweM in https://github.com/pydantic/pydantic-ai/pull/8132

Read the original announcement →

https://github.com/pydantic/pydantic-ai/releases/tag/v2.40.0

Related releases