Implementation:Microsoft Agent framework AgentFactory Init
| Knowledge Sources | |
|---|---|
| Domains | Declarative_Systems, Agent_Architecture |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Concrete constructor for the AgentFactory class that initializes the factory with shared configuration for creating Agent instances from declarative YAML definitions.
Description
The AgentFactory.__init__ method stores the shared chat client, tool bindings, connection credentials, provider mappings, and safety settings that will be applied to every agent the factory creates. It merges any user-supplied additional_mappings with the built-in PROVIDER_TYPE_OBJECT_MAPPING table, sets the default provider key, and loads environment variables from an optional .env file via dotenv.
All parameters are keyword-only. When client is omitted, the factory will instantiate a chat client from the provider mapping at agent creation time based on the YAML model definition. When safe_mode is True (the default), PowerFx expressions in YAML definitions cannot access environment variables directly, enforcing credential management through constructor parameters.
Usage
Import AgentFactory from the declarative package and construct it with the desired shared configuration. Then call create_agent_from_yaml_path() or create_agent_from_yaml() to produce Agent instances.
Code Reference
Source Location
- Repository: agent-framework
- File: python/packages/declarative/agent_framework_declarative/_loader.py
- Lines: L119-263
Signature
class AgentFactory:
def __init__(
self,
*,
client: SupportsChatGetResponse | None = None,
bindings: Mapping[str, Any] | None = None,
connections: Mapping[str, Any] | None = None,
client_kwargs: Mapping[str, Any] | None = None,
additional_mappings: Mapping[str, ProviderTypeMapping] | None = None,
default_provider: str = "AzureAIClient",
safe_mode: bool = True,
env_file_path: str | None = None,
env_file_encoding: str | None = None,
) -> None:
Import
from agent_framework.declarative import AgentFactory
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| client | SupportsChatGetResponse or None | No | Shared chat client instance passed to every created Agent. When omitted, the factory instantiates a client from the YAML model definition's provider field. |
| bindings | Mapping[str, Any] or None | No | Dictionary mapping tool names (strings) to Python callables. Allows YAML definitions to reference tools by name. |
| connections | Mapping[str, Any] or None | No | Dictionary of connection objects used to resolve ReferenceConnection entries in YAML definitions. |
| client_kwargs | Mapping[str, Any] or None | No | Additional keyword arguments forwarded to the chat client constructor when the factory instantiates a client from a provider mapping. |
| additional_mappings | Mapping[str, ProviderTypeMapping] or None | No | Dictionary extending the built-in provider registry. Each entry maps a provider key (e.g., "CustomProvider.Chat") to a ProviderTypeMapping with package, name, and model_id_field.
|
| default_provider | str | No (default: "AzureAIClient") | Provider key used when the YAML model definition does not specify a provider field.
|
| safe_mode | bool | No (default: True) | When True, blocks environment variable access in PowerFx expressions within YAML definitions. Set to False only when the YAML source is trusted. |
| env_file_path | str or None | No | Path to a .env file from which to load environment variables via dotenv.
|
| env_file_encoding | str or None | No | Character encoding of the .env file (defaults to utf-8).
|
Outputs
| Name | Type | Description |
|---|---|---|
| instance | AgentFactory | Initialized factory instance ready to create Agent objects from YAML definitions via create_agent_from_yaml_path() or create_agent_from_yaml().
|
Instance Attributes Set
| Attribute | Type | Description |
|---|---|---|
| self.client | SupportsChatGetResponse or None | The shared chat client, or None if per-agent client creation is deferred to YAML resolution. |
| self.bindings | Mapping[str, Any] or None | The tool bindings dictionary as provided. |
| self.connections | Mapping[str, Any] or None | The connection credentials dictionary as provided. |
| self.client_kwargs | Mapping[str, Any] | Keyword arguments for client construction (defaults to empty dict). |
| self.additional_mappings | Mapping[str, ProviderTypeMapping] | Extended provider mappings (defaults to empty dict). |
| self.default_provider | str | The default provider key string. |
| self.safe_mode | bool | Whether safe mode is enabled. |
Usage Examples
from agent_framework.declarative import AgentFactory
from agent_framework.azure import AzureOpenAIResponsesClient
from azure.identity import AzureCliCredential
factory = AgentFactory(
client=AzureOpenAIResponsesClient(credential=AzureCliCredential()),
bindings={"get_weather": get_weather},
)
agent = factory.create_agent_from_yaml_path("weather_agent.yaml")
Minimal Initialization
from agent_framework.declarative import AgentFactory
# Let YAML definitions specify their own provider and model
factory = AgentFactory()
agent = factory.create_agent_from_yaml_path("agent.yaml")
With Custom Provider Mappings
from agent_framework.declarative import AgentFactory
factory = AgentFactory(
additional_mappings={
"CustomProvider.Chat": {
"package": "my_package.clients",
"name": "CustomChatClient",
"model_id_field": "model_name",
},
},
default_provider="CustomProvider.Chat",
)
With Environment File and Explicit Connections
from agent_framework.declarative import AgentFactory
factory = AgentFactory(
env_file_path=".env",
env_file_encoding="utf-8",
connections={"my_api": api_connection},
safe_mode=False, # trust the YAML source
)