DaprAgentSpecLoader¶
The main class for loading Open Agent Spec (OAS) specifications and converting them to Dapr Agents configurations.
Class Reference¶
DaprAgentSpecLoader
¶
Loader for converting OAS specifications to Dapr Agents components.
This class provides methods to load OAS (Open Agent Spec) configurations from JSON or YAML format and convert them to Dapr Agents components that can be executed.
Example
loader = DaprAgentSpecLoader(
tool_registry={
"search_tool": search_function,
"calculator": calc_function,
}
)
# Load from JSON string
agent = loader.load_json(json_string)
# Load from YAML file
workflow = loader.load_yaml_file("workflow.yaml")
# Create executable Dapr agent
dapr_agent = loader.create_agent(agent)
await dapr_agent.start()
Initialize the loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_registry
|
ToolRegistry | None
|
Dictionary mapping tool names to their callable implementations. Required for tools defined in OAS specifications to be executable. |
None
|
Source code in src/dapr_agents_oas_adapter/loader.py
Attributes¶
Functions¶
load_yaml
¶
Load an OAS specification from YAML string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yaml_content
|
str
|
YAML string containing the OAS specification |
required |
Returns:
| Type | Description |
|---|---|
DaprAgentConfig | WorkflowDefinition
|
DaprAgentConfig for Agent components, WorkflowDefinition for Flows |
Raises:
| Type | Description |
|---|---|
ConversionError
|
If the YAML cannot be parsed or converted |
Source code in src/dapr_agents_oas_adapter/loader.py
load_json
¶
Load an OAS specification from JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
json_content
|
str
|
JSON string containing the OAS specification |
required |
Returns:
| Type | Description |
|---|---|
DaprAgentConfig | WorkflowDefinition
|
DaprAgentConfig for Agent components, WorkflowDefinition for Flows |
Raises:
| Type | Description |
|---|---|
ConversionError
|
If the JSON cannot be parsed or converted |
Source code in src/dapr_agents_oas_adapter/loader.py
load_dict
¶
Load an OAS specification from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec_dict
|
dict[str, Any]
|
Dictionary containing the OAS specification |
required |
Returns:
| Type | Description |
|---|---|
DaprAgentConfig | WorkflowDefinition
|
DaprAgentConfig for Agent components, WorkflowDefinition for Flows |
Raises:
| Type | Description |
|---|---|
ConversionError
|
If the component type is not supported |
Source code in src/dapr_agents_oas_adapter/loader.py
create_agent
¶
create_agent(
config: DaprAgentConfig,
additional_tools: dict[str, Callable[..., Any]]
| None = None,
) -> Any
Create an executable Dapr Agent from configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
DaprAgentConfig
|
The agent configuration |
required |
additional_tools
|
dict[str, Callable[..., Any]] | None
|
Additional tool implementations to include |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
A Dapr Agent instance (AssistantAgent or ReActAgent) |
Raises:
| Type | Description |
|---|---|
ConversionError
|
If agent creation fails |
Source code in src/dapr_agents_oas_adapter/loader.py
create_workflow
¶
create_workflow(
workflow_def: WorkflowDefinition,
task_implementations: dict[str, Callable[..., Any]]
| None = None,
) -> NamedCallable
Create an executable Dapr workflow from definition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workflow_def
|
WorkflowDefinition
|
The workflow definition |
required |
task_implementations
|
dict[str, Callable[..., Any]] | None
|
Task implementations for workflow activities |
None
|
Returns:
| Type | Description |
|---|---|
NamedCallable
|
A workflow function that can be registered with Dapr |
Raises:
| Type | Description |
|---|---|
ConversionError
|
If workflow creation fails |
Source code in src/dapr_agents_oas_adapter/loader.py
generate_workflow_code
¶
Generate Python code for a Dapr workflow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workflow_def
|
WorkflowDefinition
|
The workflow definition |
required |
Returns:
| Type | Description |
|---|---|
str
|
Python code string that can be saved and executed |
Source code in src/dapr_agents_oas_adapter/loader.py
register_tool
¶
Register a tool implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The tool name as defined in the OAS specification |
required |
implementation
|
Callable[..., Any]
|
The callable implementation |
required |
Source code in src/dapr_agents_oas_adapter/loader.py
Usage Examples¶
Basic Loading¶
from dapr_agents_oas_adapter import DaprAgentSpecLoader
loader = DaprAgentSpecLoader()
# Load from YAML
config = loader.load_yaml("""
component_type: Agent
name: assistant
system_prompt: You are helpful.
""")
With Tool Registry¶
def my_tool(query: str) -> str:
return f"Result: {query}"
loader = DaprAgentSpecLoader(
tool_registry={"my_tool": my_tool}
)
Creating Agents¶
from dapr_agents_oas_adapter.types import DaprAgentConfig
config = loader.load_yaml(yaml_content)
if isinstance(config, DaprAgentConfig):
agent = loader.create_agent(config)
Creating Workflows¶
from dapr_agents_oas_adapter.types import WorkflowDefinition
workflow = loader.load_yaml(workflow_yaml)
if isinstance(workflow, WorkflowDefinition):
workflow_fn = loader.create_workflow(workflow)
Related¶
- StrictLoader - Loader with validation
- CachedLoader - Loader with caching
- AsyncDaprAgentSpecLoader - Async version