Dynamic agent API¶
Helpers shared by the create_agent and delegate tools and by
create_agent_factory_toolset: they validate a runtime agent request and build the
agent instance. See Dynamic agents.
AgentFactory¶
subagents_pydantic_ai.AgentFactory = Callable[[SubAgentConfig], Any]
module-attribute
¶
Builds the agent instance for a dynamically created subagent.
Receives the freshly built SubAgentConfig and returns an agent instance. The
return type is Any because the agent need not be a pydantic_ai.Agent —
consumers such as pydantic-deep return their own agent objects.
A factory must not attach an ask_parent toolset; the subagent toolset injects
one at run time for registry-backed and one-shot agents, and a second copy would
collide on the tool name.
build_dynamic_agent¶
subagents_pydantic_ai.dynamic_agent.build_dynamic_agent(ctx, *, name, description, instructions, model, can_ask_questions=True, capabilities=None, allowed_models=None, toolsets_factory=None, capabilities_map=None, default_agent_factory=None)
¶
Validate inputs and build a dynamic agent.
Returns:
| Type | Description |
|---|---|
str | tuple[Any, SubAgentConfig]
|
An error string on failure, or |
Source code in src/subagents_pydantic_ai/dynamic_agent.py
Validation helpers¶
subagents_pydantic_ai.dynamic_agent
¶
Shared helpers for building dynamic subagents at runtime.
validate_agent_name(name)
¶
Return an error message when the agent name is invalid.
Source code in src/subagents_pydantic_ai/dynamic_agent.py
validate_model(model, allowed_models)
¶
Return an error message when the model is not allowed.
Source code in src/subagents_pydantic_ai/dynamic_agent.py
validate_capabilities(capabilities, capabilities_map)
¶
Return an error message when capabilities are unknown.
Source code in src/subagents_pydantic_ai/dynamic_agent.py
validate_capabilities_with_factory(capabilities, default_agent_factory)
¶
Return an error when capabilities cannot be injected with a custom factory.
Source code in src/subagents_pydantic_ai/dynamic_agent.py
build_subagent_config(*, name, description, instructions, model, can_ask_questions=True)
¶
Build a runtime subagent configuration.
Source code in src/subagents_pydantic_ai/dynamic_agent.py
collect_agent_toolsets(ctx, capabilities, *, toolsets_factory, capabilities_map)
¶
Collect toolsets to attach to a dynamically built agent.
Source code in src/subagents_pydantic_ai/dynamic_agent.py
build_agent_instance(ctx, config, *, model, capabilities, toolsets_factory, capabilities_map, default_agent_factory)
¶
Build a pydantic-ai Agent instance for a dynamic subagent.