Table of Contents
Version Info
| Item | Value |
|---|---|
| Framework | Pydantic AI |
| Version | v2.40.0 |
| Previous | v2.39.0 |
| Released | 2026-09-05 |
| Release Notes | GitHub Release |
| GitHub | pydantic/pydantic-ai |
| Stars | 19,742 |
Why This Version Matters
Pydantic AI takes a significant step forward for voice Agent UX. Previously, handling user barge-in (interrupting the AI mid-speech) required manual plumbing — detecting the interruption, tracking played bytes, and signaling the model to stop. v2.40.0 bakes this into the session layer with a single handle_barge_in=True flag. Meanwhile, the new @agent.on_event decorator gives Agents native event listening for cross-cutting concerns like billing, observability, and live UI updates without monkey-patching internals.
Key Changes
@agent.on_eventevent listener decorator: Register event callbacks directly on an Agent covering tool calls, token streaming, and completion events → billing and observability logic hooks directly onto the Agent without intercepting internal mechanisms- Realtime voice barge-in handling:
handle_barge_in=True+interrupt(played_bytes=...)+played_audio_bytesproperty → automatic interruption handling when users speak over the AI, with played-progress tracking built in RealtimeSession.enqueue(): Lets external code driving the session inject out-of-band prompts → enables backend-initiated messages during voice conversations (e.g., a support system pushing a promo code hint)respond=parameter:RealtimeSession.send(respond=True/False)controls whether a text message solicits a model reply → precise control over multi-turn voice conversation pacingprovider_factoryforinfer_realtime_model: Custom provider factory function → more flexible realtime model initializationprices.update_in_background(): Background model pricing data refresh → long-running Agents track latest pricing without restarts
Breaking Changes
No breaking changes in this version.
Migration Guide
Direct upgrade, no code changes required.
pip install --upgrade pydantic-ai==2.40.0
Example using the new event listener:
from pydantic_ai import Agent
agent = Agent("openai:gpt-4o")
@agent.on_event
async def log_events(event):
print(f"Agent event: {event.type}")
result = await agent.run("Hello")
Cross-Framework Observations
Pydantic AI continues to widen its lead in realtime voice capabilities. Among the 12 Agent frameworks we track, only Pydantic AI offers framework-level native barge-in and audio stream control — other frameworks (LangGraph, CrewAI, Mastra) still require manual WebSocket and audio processing for voice Agents. The @agent.on_event pattern brings the callback mechanism long available in the LangChain ecosystem into a more Pythonic form.
Takeaway
I previously assumed voice Agent barge-in handling was simply "detect user speech and stop." Seeing Pydantic AI's implementation reveals the need to track played byte counts — the model needs to know where the user interrupted to maintain conversation coherence. This is an audio-streaming-specific state management problem with no text-chat equivalent.
References
Loading...