AsyncDaprAgentSpecLoader¶
Asynchronous version of the loader for non-blocking operations.
Class Reference¶
AsyncDaprAgentSpecLoader
¶
Async loader for converting OAS specifications to Dapr Agents components.
This class provides async methods to load OAS (Open Agent Spec) configurations from JSON or YAML format and convert them to Dapr Agents components.
The async operations use asyncio.to_thread for offloading synchronous
conversion logic to a thread.
Example
Initialize the async loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_registry
|
ToolRegistry | None
|
Dictionary mapping tool names to their callable implementations. |
None
|
Source code in src/dapr_agents_oas_adapter/async_loader.py
Functions¶
__aenter__
async
¶
__aexit__
async
¶
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: Any,
) -> None
Exit async context manager.
Source code in src/dapr_agents_oas_adapter/async_loader.py
load_yaml
async
¶
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/async_loader.py
load_json
async
¶
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/async_loader.py
load_dict
async
¶
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/async_loader.py
run_sync¶
Utility function to run async code synchronously.
run_sync
¶
Run an async coroutine synchronously.
This is a utility function for running async code from sync contexts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Any
|
The coroutine to run |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The result of the coroutine |
Example
Source code in src/dapr_agents_oas_adapter/async_loader.py
Usage Examples¶
Basic Async Usage¶
from dapr_agents_oas_adapter import AsyncDaprAgentSpecLoader
loader = AsyncDaprAgentSpecLoader()
# Async load
config = await loader.load_yaml(yaml_content)
Context Manager¶
async with AsyncDaprAgentSpecLoader() as loader:
config = await loader.load_dict(spec)
# Resources cleaned up automatically
Concurrent Loading¶
import asyncio
async def load_all(yaml_contents: list[str]):
async with AsyncDaprAgentSpecLoader() as loader:
tasks = [loader.load_yaml(c) for c in yaml_contents]
return await asyncio.gather(*tasks)
configs = await load_all(yaml_list)
Synchronous Wrapper¶
from dapr_agents_oas_adapter import AsyncDaprAgentSpecLoader, run_sync
loader = AsyncDaprAgentSpecLoader()
# Run async code in sync context
config = run_sync(loader.load_dict(spec))
With Tool Registry¶
async def async_tool(query: str) -> str:
await asyncio.sleep(0.1)
return f"Result: {query}"
loader = AsyncDaprAgentSpecLoader(
tool_registry={"my_tool": async_tool}
)
Related¶
- DaprAgentSpecLoader - Synchronous loader
- Async Guide - Usage guide