Back Home

代理框架

PydanticAI 2.38 Exposes Agent Context Usage and Adds Typed Custom Events to Run Streams

Agent tools can now determine context usage from `RunContext`, while applications can send custom state through the same event stream. The release also adds a provider for self-hosted vLLM and a policy for rejecting streams that lack a `finish_reason`.

W. Punt for Anefo · CC BY-SA 3.0 nl · Image source
zh-Hant

PydanticAI 2.38.0 brings several agent-control signals into the framework that previously had to be implemented at the application layer. `ModelProfile` now includes `context_window`, while `RunContext` provides `context_window_used`. During execution, tools can retrieve the amount of context already consumed and use it to stop loading large documents, switch to a summarization workflow, or avoid making another expensive model call when nearing the limit. This is better suited to long tool loops than reading token usage only after a request has finished, although its practical usefulness still depends on each model provider supplying accurate context-window specifications and usage data.

Another change allows applications and capabilities to emit typed `CustomEvent` and `CapabilityEvent` events and subscribe to them with `@on_event`. This enables progress updates, approval requests, domain-specific state, and frontend notifications to travel through the same run stream as model and tool events, eliminating the need to maintain a separate set of untyped callbacks or a message bus. For teams connecting agents to Web UIs, job queues, and observability systems, a unified event model helps preserve event ordering and enables static type checking.

Version 2.38.0 also adds `VLLMProvider`, allowing the same agent API to connect to enterprise self-hosted vLLM servers. The model catalog now also supports Gemini 3.8 Flash, Claude Fable 5.1, and Mythos 5.1. A new profile option can reject streams without a `finish_reason`, preventing upstream truncation from being mistaken for successful completion. The release also fixes issues involving tool selection, reasoning blocks, and history replay across DeepSeek, Together, Anthropic, Bedrock, and OpenAI Responses paths, highlighting the substantial semantic differences that remain in cross-provider abstractions. No throughput or reliability benchmarks were included with this release. When upgrading, teams should also test the new default `id` and duplicate-merging rules for capabilities, and verify that their self-hosted vLLM stream format, finish reasons, and token statistics match the framework’s expectations.

Sources

  1. PydanticAI v2.38.0 Release
  2. pydantic-ai 2.38.0