Skip to main content Switch to mobile version dex-agents-pydantic-ai 0.6.19 Run Pydantic AI agents as durable Dex agent sessions
pip install dex-agents-pydantic-ai==0.6.19 Copy PIP instructions
dex-agents-pydantic-ai (import dex_agents_pydantic_ai) runs a Pydantic AI agent as a durable Dex agent session. It implements the runner contract of the Python core, dex-agents, so the session Flow, its RPCs, its live events, and its persistence are the core's. The plan is agent-framework-adapters.md.
One Flow execution is one session; the Flow ID is the session ID. The session runs in Step mode: each Pydantic AI model call is one RunModel Step, and each tool call, READ included, is its own Step. The tool calls of one model response run as parallel Steps, and ResumeTurn waits for all of them before the next model call. A crash therefore repeats at most the one call that was running, and a write never runs twice silently. The agent's code stays unchanged.
The package needs Python 3.11 or later. It requires dex-agents at the same version, dex-python-sdk==1.45.17, and pydantic-ai-slim 2.64 or a later 2.x release. Add the Pydantic AI extras for your model provider:
pip install dex-agents-pydantic-ai "pydantic-ai-slim[openai]" Develop the package inside this repository with uv 0.12.2. There it resolves dex-agents from ../core by path through [tool.uv.sources]; the published wheel requires dex-agents==
cd agents/python/pydantic-ai uv sync --locked DEXCLI=/path/to/dexcli ./ci.sh Example
This program serves one agent and runs one session against dexcli dev on localhost:8801. It approves every tool call that asks for approval.
import asyncio from dex import AsyncClient, AsyncWorker, BlobCacheConfig, ClientOptions, Registry from dex import WorkerOptions, open_blob_cache from pydantic_ai import Agent, RunContext from dex_agents import AgentSessionClient, ToolEffect, ToolPolicy from dex_agents_pydantic_ai import deferred_tool_idempotency_key, pydantic_ai_session_flow ORDERS = {"7": "delivered"} REFUNDS: dict[str, str] = {} agent = Agent("openai:gpt-5.2", instructions="You help customers with their orders.") @agent.tool_plain def find_order(order_id: str) -> str: """Return the status of an order.""" return ORDERS.get(order_id, "unknown order") @agent.tool def refund_order(ctx: RunContext[None], order_id: str) -> str: """Refund an order.""" key = deferred_tool_idempotency_key(ctx) or order_id REFUNDS.setdefault(key, f"refund-{len(REFUNDS) + 1}") return f"{REFUNDS[key]} issued for order {order_id}" @agent.tool_plain def email_customer(order_id: str, text: str) -> str: """Email the customer who placed an order.""" return f"emailed the customer of order {order_id}" flow = pydantic_ai_session_flow( agent, flow_type="OrderAgent", tool_policies={ "find_order": ToolPolicy(ToolEffect.READ), "refund_order": ToolPolicy(ToolEffect.IDEMPOTENT_WRITE, requires_approval=True), "email_customer": ToolPolicy(ToolEffect.NON_IDEMPOTENT_WRITE), }, ) async def main() -> None: registry = Registry([flow], allow_async_handlers=True) # Only one running process may use .dex-cache. Give each Worker replica its own directory. cache = open_blob_cache(BlobCacheConfig(".dex-cache", 64 * 1024 * 1024)) worker = AsyncWorker(registry, cache, WorkerOptions(server_address="localhost:8801")) client = AsyncClient(registry, cache, ClientOptions(server_address="localhost:8801")) serving = asyncio.create_task(worker.start()) try: await worker.wait_pull_registered(timeout=30) sessions = AgentSessionClient(client, flow) await sessions.start_session("order-session-7", "Refund order 7 and tell the customer.") while True: snapshot = await sessions.get_agent_snapshot("order-session-7") state = snapshot.state if state is not None and state.turn_count >= 1 and state.active_turn_id is None: break for interrupt in snapshot.pending_interrupts: print(f"approving {interrupt.tool_name} {interrupt.arguments}") await sessions.resolve_interrupt( "order-session-7", interrupt.interrupt_id, approved=True ) await asyncio.sleep(0.5) print(snapshot.messages[-1].text) finally: await worker.stop() await serving await client.close() cache.close() asyncio.run(main()) A real application serves the Worker in its own process and answers interrupts from a UI. Read live text with AgentSessionClient.read_agent_event and rebuild the view from get_agent_snapshot. Tool calls run as parallel Steps, so give the Worker enough task slots (PullWorkerOptions.max_concurrent_tasks) for the largest batch of tool calls the model makes.
agent may be a WrapperAgent. deps_factory(session_id) builds the agent's dependencies on the Worker. It runs before every model Step and every tool Step, so it can run many times per session, in parallel, and again after a crash. Every run uses the session ID as its Pydantic AI conversation_id.
Effects and policies
A call that requires approval first waits in AwaitToolApproval. The effect of a tool comes from, in order:
tool_policies, by the tool name the model calls, including any toolset prefix.
Tool metadata: MCP readOnlyHint makes read, MCP idempotentHint makes idempotent_write, and Pydantic AI's tool search (search_tools) is read.
default_tool_effect, unclassified by default.
A tool declared with Pydantic AI's requires_approval=True always waits for approval, whatever its policy says.
Tool sources and connector tools
tool_sources takes the core's tool sources, such as ConnectorToolSource, which offers reviewed Connector operations (install dex-agents[connectors]). See Connector tools for what the manifest decides.
from dex_agents.connectors import ConnectorToolSource, connector_tool stripe = new_project_connection(project, "billing-stripe") flow = pydantic_ai_session_flow( agent, flow_type="SupportAgent", tool_sources=[ ConnectorToolSource([ connector_tool(stripe.operations.create_refund(), stripe.reference), ]), ], ) Each RunModel lists every source and passes its tools to the run as an ExternalToolset, with the source's name, description, and JSON Schema. Pydantic AI never runs an external tool, so a source call ends the run.
Every call of a source tool defers, reads included, and runs once in its own tool Step through the source's execute_deferred_tool_call, never inside the framework. A connector call derives its Call ID from the Step execution ID and keeps its fence marker in the Step's heartbeat checkpoint.
The source builds each call, so it decides the effect class, the approval, and whether the call is an ordered write; tool_call_step_options gives the tool Step the operation's timeout and retry policy. A tool_policies entry for a connector tool changes only its approval.
Writes of one model response run one at a time in the model's order, as in a built-in loop agent. Once one does not succeed, the later ones record failed with skipped.
Definition fails with ValueError when two sources share a source_name, or a source's declared_tool_names repeat a function tool of the agent, or another source's tool. The same names listed at run time fail that RunModel attempt.
The runner polls no tool tasks. A source call that starts one records failed for a read call and outcome_unknown otherwise. A source whose listing fails or takes longer than tool_source_listing_timeout offers no tools in that model call. A tool the turn already called but the listing lacks is replay-only: the resumed run replays its recorded call from the source's cached_tools(), and a new call of it is refused inside the framework (Pydantic AI offers such a tool as a function tool whose body returns the refusal: " did not run: its tool source does not offer this tool now."), so it never reaches a tool Step.
A recorded result replays once, for the call that paused, and only under the tool name it was recorded for; a later call that reuses its tool call ID fails the RunModel attempt instead of reading that result.
One model call per Step. Each RunModel runs the agent with a Pydantic AI capability that wraps the run's toolset and marks every function tool as an external tool. Pydantic AI never executes an external tool, so the model call that asks for tools ends the run with DeferredToolRequests right away. The runner returns ToolCallsRequested with one call per tool call, in model order, and the session starts one Step per call. Its new_messages are the messages of this model call, which the session stores in the same commit, so a UI shows the assistant's tool calls and the earlier tool results while the tools run. The run state holds only the IDs of the requested calls. A run state that 0.6.11 or earlier wrote holds the turn's messages instead; the next RunModel resumes from it and stores them. Each run lists DeferredToolRequests in its output type, unless the agent's output_type already does.
Upgrading from 0.6.11 or earlier. A Worker of 0.6.11 or earlier cannot read the run state that 0.6.12 writes. When such a Worker takes the next RunModel of a turn that a newer Worker paused for tools, every attempt fails and the turn fails. Upgrade every Worker of the Flow type together, or drain the Workers of 0.6.11 and earlier before newer Workers start. A newer Worker resumes every run state that an older one wrote.
Because no tool runs during RunModel, the agent's own tool hooks run only in the tool Steps, where the tool really runs. The capability wraps every other capability, so tools that other capabilities add, such as search_tools, are deferred too.
A final result beside tool calls. When one model response holds both the output tool call and other tool calls, Pydantic AI would end the run with the result and drop the other calls. The capability removes the output tool call from that response instead. The tools run in their Steps, and the next model call gives the final result. If a Pydantic AI result ever answers a tool call that no Dex Step ran, the RunModel attempt fails instead of completing the turn.
Resume. When every call has a final record, the next RunModel resumes the run with the stored messages, the turn's own included, and DeferredToolResults. It stores only the messages the turn has not stored yet:
Tool Steps. execute_deferred_tool_call runs the original tool once, through the agent: Pydantic AI starts a run whose history holds only the tool call, validates the arguments, runs the agent's capabilities and the tool, and the runner stops the run before any model request. Dependencies come from deps_factory; toolsets such as MCP servers are entered as in any run. An approved call runs with RunContext.tool_call_approved set. A tool that only tool search reveals is made visible for its own Step. The tool returns text: its return value as the model would see it.
A Step failure retries a read or idempotent_write call up to three attempts and then records failed for a read and outcome_unknown for a write. A single-attempt call records outcome_unknown at once.
History. Every Pydantic AI ModelMessage is one AgentMessage. Its framework_item is a JSON array holding that one message, as ModelMessagesTypeAdapter dumps it, so ModelMessagesTypeAdapter.validate_json(framework_item) reads it back. The display projection is:
A turn replays every earlier turn as message_history and runs its own user prompt. When the run's opening request holds more than the user prompt, such as the agent's system prompts on the first turn, the runner stores that request after the user message, without display text, and replays it in place of the user message.
Unfinished turns. When the user steers or cancels a turn, or the turn fails, while tool calls are outstanding, build_unfinished_turn_messages stores one ModelRequest that answers every tool call of the turn without a result. Each ToolReturnPart comes from the call's final record, as the table above maps it: outcome success, denied for a rejection such as the steering or cancellation result, or failed. A call without a final record gets OUTCOME_UNKNOWN_TOOL_RESULT. The method derives the request from committed state only, so a retried Step stores the same request. A steered or cancelled turn therefore stores no note, and the next turn replays the exact tool calls and results. A Worker of 0.6.11 or earlier ends such a turn with only the note, even when a newer Worker already stored its tool calls. Each replay of an earlier turn therefore answers a tool call without a result with OUTCOME_UNKNOWN_TOOL_RESULT, in the model input only.
A failed turn is replayed as it is stored: its messages, the results above, and, when it ran tools, the core's turn-failure note, which build_user_message stores as one more user prompt. The next turn's model therefore sees what already ran and does not repeat a write. Every model Step also includes Pydantic AI's ReinjectSystemPrompt capability. It does nothing when the replayed history holds the agent's system prompts, and adds them when it does not, such as after a failed first turn.
Live events. The capability writes text_delta for text parts and reasoning_delta for Pydantic AI thinking parts as the model streams them, so model Steps always use the model's streaming API. Every event carries the turn ID, step_execution_id, and attempt; the core README explains how a UI groups live text by them.
Usage. Each model Step reports Pydantic AI's run usage: requests as model_calls, cache_read_tokens as cached_input_tokens, and output_reasoning_tokens as reasoning_tokens.
Tracing. Each model Step returns ModelCallMetadata, so the core's chat span is named chat
The provider becomes the well-known gen_ai.provider.name value when one applies, and any other provider_name passes through unchanged. For google, google-cloud, and openai, the response's canonical_system names the endpoint the client reached, so it decides: a Vertex AI client is gcp.vertex_ai and an Azure OpenAI client is azure.ai.openai.
AnthropicModel reports anthropic with every client, including Bedrock, Vertex AI, and Foundry clients, because its response does not say which one it used.
Why not Pydantic AI's durable execution
Pydantic AI also ships durability capabilities that run an agent inside another workflow engine, recording each model request and tool call there. This adapter does not use that interface. In a Dex session, Dex Steps are the durable boundary: every model call and every tool call is already its own Step with its own retry policy, timeout, and history entry. Step mode needs only to stop the run after one model call, store its messages, and resume it with external results, which Pydantic AI's deferred tools already do without an engine-specific runtime. Do not combine this adapter with a Pydantic AI durability capability: such an agent refuses the capabilities the runner adds to each run.
The summary enters the model input as a user message before the kept turns. It is never stored as a message, and every message stays readable until retention deletes it behind the summary.
The estimate counts three bytes of each stored message as one token, plus the instructions and tool definitions the runner can see. Set the policy's reserved input tokens for the rest, such as dynamic instructions.
The summary call gets the earlier turns as plain text, without tools.
A history processor or a compaction capability changes only Pydantic AI's model input. Dex stores every message either way and owns the record; use one of the two. The estimate reads the agent's static system prompts and instructions through private Pydantic AI fields, because there is no public accessor.
See Compaction and the storage bound.
Since 0.5.0 a session can show images to the model. Pass a media store, such as S3MediaStore or LocalDirectoryMediaStore, as media_store to pydantic_ai_session_flow or PydanticAIRunner, and use the same store in the clients that upload images:
store = S3MediaStore("agent-media", "sessions") flow = pydantic_ai_session_flow(agent, flow_type="SupportAgent", media_store=store) reference = await store.put_image(Path("chart.png").read_bytes()) await sessions.send_user_message("session-42", "What does this chart show?", images=[reference]) What the model sees. A user message is one UserPromptPart whose content is the text, then each image as BinaryContent with the image's MIME type, in order.
Tool results. A tool call record's images go into its ToolReturnPart content, after the output text, as BinaryContent. This is Pydantic AI's native form for images in a tool result; each provider sends it as its API allows. Images come from a tool source's ToolExecutionResult.images, or from a tool of the agent that returns image BinaryContent: its tool Step uploads the image and records the reference.
What is stored. Stored messages and the run state hold each image as an ImageUrl placeholder: its url is the reference's uri, and its vendor_metadata holds the reference under "dex_image_reference". The model Step capability replaces the placeholders with the bytes, read with load_images, right before each model request, so a replay after a Worker restart sends the same bytes. A failed read fails the model call, which retries.
Without a store the runner has no build_user_message_with_images, so send_user_message refuses images, as before.
Retries that Pydantic AI sends to the model itself make another model call in the same RunModel Step: invalid tool arguments, an unknown tool name, or output that fails validation. A RunModel retry repeats those calls too.
Output functions, such as output_type=send_email, run inside RunModel when the model gives the final result. They are at-least-once: a RunModel retry runs them again. Put a write in a tool instead.
Provider-run tools, such as MCPServerTool, WebSearchTool, and code execution, run at the provider during the model call. A RunModel retry repeats them.
A deferred tool returns text to the model, and with a media_store also its image BinaryContent (see Images). Other files, image URLs, ToolReturn.content, and ToolReturn.metadata are lost; typed tool parts, such as tool search results, are restored from their JSON.
In a tool Step, RunContext.messages holds only the tool call, RunContext.prompt is None, and usage starts at zero. A tool that a prepare_tools hook hides at that point is reported as unknown.
External tools, such as those in an ExternalToolset, have no function to run. After approval the model sees a failed call.
A tool that asks for approval only while it runs, by raising ApprovalRequired or through an ApprovalRequiredToolset, needs a ToolPolicy with requires_approval=True. Without it, that request fails the call in its Step. With it, the tool runs with RunContext.tool_call_approved set after the person approves.
Metadata passed to ApprovalRequired or CallDeferred is not kept.
A HandleDeferredToolCalls capability on the agent that supplies tool results itself conflicts with Dex Steps: the turn fails when Pydantic AI answers a call that no Dex Step ran.
The model Step capability wraps every other capability. An agent capability that also declares it wraps every capability, through CapabilityOrdering(wraps=(AbstractCapability,)), makes every RunModel fail with Pydantic AI's circular ordering error.
An agent with output validators must list DeferredToolRequests in its output_type, such as output_type=[Answer, DeferredToolRequests]; otherwise the constructor raises ValueError. The check reads Pydantic AI's private _output_validators, because there is no public accessor.
Tool call IDs must be unique within a turn and contain no /. Provider IDs satisfy this. They may repeat across turns.
The agent's event_stream_handler runs during model Steps, not in tool Steps. Each streamed chunk becomes one text_delta event.
UsageLimits and tool retry budgets apply per model Step and per tool Step, not per turn.
The core's limits apply too: each call adds a Step round trip. See Compaction for long sessions.
steer_user_message and cancel_turn work as the core describes. After either one the turn keeps its tool calls and their results, as How it works describes.
ci.sh installs the locked dependencies, checks that dex-python-sdk and pydantic-ai-slim come from the package registry, and runs ruff, mypy, and the integration tests. The tests run a real Pydantic AI agent whose FunctionModel follows commands in the user prompt, so no model key is needed. Each test starts a private dexcli dev --engine zd engine on free ports and runs the Worker as a child process, so it can SIGKILL the Worker during RunModel or a tool Step. Tests assert the Step shape of each turn from the Flow's history with dex_agents.testing.read_step_history, and count real model and tool calls across Worker processes. The tracing test runs its Worker in the test process, so an in-memory exporter receives the spans.
tests/test_connector_tools.py runs connector tools from a copy of the core tests' fake ledger Connector (tests/fake_ledger): a Query in its tool Step, an approved Mutation that runs once, a session policy that changes only approval, a retry: safe Mutation retried with the same Call ID, uncertain writes recorded outcome_unknown, two writes of one response in the model's order, an unknown or rejected write that skips the later write, a withdrawn tool whose new call is refused, a reused call ID, the operation's tool Step options, an unexposed operation, and refused name collisions.
tests/test_images.py runs an agent with a LocalDirectoryMediaStore shared by the test and the Worker. Its FunctionModel records the parts and image bytes it receives: user images and tool images arrive as BinaryContent, a Worker killed in the resumed model call sends the same input again, stored messages and run state hold no image bytes, and a runner without a store refuses images.
Released: Dec 1, 2026
Author: Super Durable
LicenseRef-Sustainable-Use-1.0 View SPDX License List
Development Status 3 - Alpha
Programming Language Python :: 3
Release files for dex-agents-pydantic-ai 0.6.19
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
Built distribution (wheel)
Total release size: 79.3 kB
Release files / dex_agents_pydantic_ai-0.6.19.tar.gz
Security for dex-agents-pydantic-ai 0.6.19
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.
Release history Release notifications | RSS feed
Dec 1, 2026 2 release files No provenance
Nov 30, 2026 2 release files No provenance
Nov 23, 2026 2 release files No provenance
Nov 22, 2026 2 release files No provenance
Nov 19, 2026 2 release files No provenance
Nov 16, 2026 2 release files No provenance
Nov 15, 2026 2 release files No provenance
Nov 14, 2026 2 release files No provenance
Nov 13, 2026 2 release files No provenance
Nov 12, 2026 2 release files No provenance
Nov 11, 2026 2 release files No provenance
Nov 10, 2026 2 release files No provenance
Nov 9, 2026 2 release files No provenance
Nov 6, 2026 2 release files No provenance
Nov 1, 2026 2 release files No provenance
"PyPI", "Python Package Index", and the blocks logos are registered trademarks of the Python Software Foundation.
© 2026 Python Software Foundation
Deployed from 1875753











