Subagent toolset for pydantic-ai with dual-mode execution and dynamic agent creation
Project description
subagents-pydantic-ai
Subagent delegation toolset for pydantic-ai with dual-mode execution.
Looking for a complete agent framework? Check out pydantic-deep - a full-featured agent framework with planning, subagents, and skills system.
Need file operations? Check out pydantic-ai-backend - file storage and sandbox backends for pydantic-ai agents.
Documentation
Full Documentation - Installation, concepts, examples, and API reference.
Architecture
%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#6366f1', 'primaryTextColor': '#fff', 'primaryBorderColor': '#4f46e5', 'lineColor': '#94a3b8', 'secondaryColor': '#22c55e', 'tertiaryColor': '#1e293b', 'background': '#0f172a', 'mainBkg': '#1e293b', 'textColor': '#e2e8f0', 'nodeTextColor': '#e2e8f0'}}}%%
flowchart LR
subgraph parent [" Parent Agent "]
direction TB
PA["🤖 pydantic-ai Agent"]
TS["🔧 Subagent Toolset"]
PA --> TS
end
subgraph tools [" Tools "]
direction TB
T1["task()"]
T2["check_task()"]
T3["answer_subagent()"]
T4["cancel_task()"]
end
subgraph agents [" Specialized Subagents "]
direction TB
S1["🔍 researcher"]
S2["✍️ writer"]
S3["💻 coder"]
S4["🔧 general"]
end
TS --> tools
tools -->|"sync / async"| agents
Installation
pip install subagents-pydantic-ai
Or with uv:
uv add subagents-pydantic-ai
Quick Start
from dataclasses import dataclass, field
from typing import Any
from pydantic_ai import Agent
from subagents_pydantic_ai import create_subagent_toolset, SubAgentConfig
# Dependencies must implement SubAgentDepsProtocol
@dataclass
class Deps:
subagents: dict[str, Any] = field(default_factory=dict)
def clone_for_subagent(self, max_depth: int = 0) -> "Deps":
return Deps(subagents={} if max_depth <= 0 else self.subagents.copy())
# Define specialized subagents
subagents = [
SubAgentConfig(
name="researcher",
description="Researches topics and gathers information",
instructions="You are a research assistant. Investigate thoroughly.",
),
SubAgentConfig(
name="writer",
description="Writes content based on research",
instructions="You are a technical writer. Write clear, concise content.",
),
]
# Create toolset and agent
toolset = create_subagent_toolset(subagents=subagents)
agent = Agent(
"openai:gpt-4o",
deps_type=Deps,
toolsets=[toolset],
system_prompt="You can delegate tasks to specialized subagents.",
)
# Run the agent
result = agent.run_sync(
"Research Python async patterns and write a blog post about it",
deps=Deps(),
)
print(result.output)
Dual-Mode Execution
%%{init: {'theme': 'base', 'themeVariables': { 'primaryColor': '#6366f1', 'actorBkg': '#6366f1', 'actorTextColor': '#fff', 'actorLineColor': '#94a3b8', 'signalColor': '#e2e8f0', 'signalTextColor': '#e2e8f0', 'labelBoxBkgColor': '#1e293b', 'labelBoxBorderColor': '#475569', 'labelTextColor': '#e2e8f0', 'loopTextColor': '#e2e8f0', 'noteBkgColor': '#334155', 'noteTextColor': '#e2e8f0', 'noteBorderColor': '#475569', 'activationBkgColor': '#4f46e5', 'sequenceNumberColor': '#fff'}}}%%
sequenceDiagram
participant P as 🤖 Parent
participant S as 🔧 Subagent
rect rgba(99, 102, 241, 0.2)
Note over P,S: 🔄 Sync Mode (default)
P->>+S: task(mode="sync")
S-->>S: working...
S->>-P: ✅ result
end
rect rgba(34, 197, 94, 0.2)
Note over P,S: ⚡ Async Mode
P->>+S: task(mode="async")
S-->>P: 🎫 task_id
Note over P: continues working...
S-->>S: working in background...
P->>S: check_task(id)
S->>-P: ✅ result
end
Sync Mode (Default)
Block until the subagent completes:
# The agent uses task() with mode="sync" (default)
# - Quick tasks
# - When result is needed immediately
# - Back-and-forth communication
Async Mode
Run in background, continue working:
# The agent uses task() with mode="async"
# - Long-running research
# - Parallel tasks
# - When immediate result isn't needed
Give Subagents Tools
Provide toolsets to your subagents:
from pydantic_ai_backends import create_console_toolset
def my_toolsets_factory(deps):
"""Factory that creates toolsets for subagents."""
return [
create_console_toolset(), # File operations
create_search_toolset(), # Web search
]
toolset = create_subagent_toolset(
subagents=subagents,
toolsets_factory=my_toolsets_factory,
)
Dynamic Agent Creation
Create agents at runtime:
from subagents_pydantic_ai import (
create_subagent_toolset,
create_agent_factory_toolset,
DynamicAgentRegistry,
)
registry = DynamicAgentRegistry()
# Main agent can create new specialized agents on-the-fly
agent = Agent(
"openai:gpt-4o",
deps_type=Deps,
toolsets=[
create_subagent_toolset(),
create_agent_factory_toolset(
registry=registry,
allowed_models=["openai:gpt-4o", "openai:gpt-4o-mini"],
max_agents=5,
),
],
)
Available Tools
| Tool | Description |
|---|---|
task |
Delegate a task to a subagent (sync or async) |
check_task |
Check status of a background task |
answer_subagent |
Answer a question from a subagent |
list_active_tasks |
List all running background tasks |
soft_cancel_task |
Request cooperative cancellation |
hard_cancel_task |
Immediately cancel a task |
Related Projects
- pydantic-ai - Agent framework by Pydantic
- pydantic-deep - Full agent framework (uses this library)
- pydantic-ai-backend - File storage and sandbox backends
- pydantic-ai-todo - Task planning toolset
Development
git clone https://github.com/vstorm-co/subagents-pydantic-ai.git
cd subagents-pydantic-ai
make install
make test
License
MIT License - see LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file subagents_pydantic_ai-0.0.1.tar.gz.
File metadata
- Download URL: subagents_pydantic_ai-0.0.1.tar.gz
- Upload date:
- Size: 123.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d41259074fb0c7b97e00ea99377621b4ece48269e0ee110b6ef1698ab364a76d
|
|
| MD5 |
c8c23263d300db52eb94c577e1c4d7fe
|
|
| BLAKE2b-256 |
1191a90d61bd2e6fb41ad24b52061b85834c00e6fd2d57e14877004851b07810
|
Provenance
The following attestation bundles were made for subagents_pydantic_ai-0.0.1.tar.gz:
Publisher:
publish.yml on vstorm-co/subagents-pydantic-ai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
subagents_pydantic_ai-0.0.1.tar.gz -
Subject digest:
d41259074fb0c7b97e00ea99377621b4ece48269e0ee110b6ef1698ab364a76d - Sigstore transparency entry: 839610456
- Sigstore integration time:
-
Permalink:
vstorm-co/subagents-pydantic-ai@2ef04ca83430404fa77836aa5e20793677fb5c2d -
Branch / Tag:
refs/tags/0.0.1 - Owner: https://github.com/vstorm-co
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2ef04ca83430404fa77836aa5e20793677fb5c2d -
Trigger Event:
release
-
Statement type:
File details
Details for the file subagents_pydantic_ai-0.0.1-py3-none-any.whl.
File metadata
- Download URL: subagents_pydantic_ai-0.0.1-py3-none-any.whl
- Upload date:
- Size: 27.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83c53c1c66cd82a504817e04fecccc0e245969f906a392b58ff23ddbf06dc41f
|
|
| MD5 |
03f9b547636d1ed4407b938b17f3808e
|
|
| BLAKE2b-256 |
bbd813fb70609a5962e03a28a69ed446b4d3a0beed4662b01feb5d0f2918bc20
|
Provenance
The following attestation bundles were made for subagents_pydantic_ai-0.0.1-py3-none-any.whl:
Publisher:
publish.yml on vstorm-co/subagents-pydantic-ai
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
subagents_pydantic_ai-0.0.1-py3-none-any.whl -
Subject digest:
83c53c1c66cd82a504817e04fecccc0e245969f906a392b58ff23ddbf06dc41f - Sigstore transparency entry: 839610497
- Sigstore integration time:
-
Permalink:
vstorm-co/subagents-pydantic-ai@2ef04ca83430404fa77836aa5e20793677fb5c2d -
Branch / Tag:
refs/tags/0.0.1 - Owner: https://github.com/vstorm-co
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@2ef04ca83430404fa77836aa5e20793677fb5c2d -
Trigger Event:
release
-
Statement type: