Skip to content

Commit 2c20c5e

Browse files
authoredMar 25, 2025··
Python: lazy-loading mechanism for agent dependencies (#11180)
### Motivation and Context Consolidated agent imports initially caused issues by eagerly loading all submodules, which forced users to install every optional dependency -- even if only one agent was needed. This PR introduces a lazy-loading mechanism via `__getattr__` in the top-level `__init__.py` to defer imports until actually accessed. Additionally, a corresponding` __init__.pyi` type stub file is provided to restore full IDE autocompletion and type hints without compromising runtime behavior. <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description Lazy load agent dependencies. <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄
1 parent 17e63a7 commit 2c20c5e

File tree

3 files changed

+66
-34
lines changed

3 files changed

+66
-34
lines changed
 

‎python/semantic_kernel/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from semantic_kernel.kernel import Kernel
44

5-
__version__ = "1.26.0"
5+
__version__ = "1.26.1"
66

7-
DEFAULT_RC_VERSION = f"{__version__}-rc4"
7+
DEFAULT_RC_VERSION = f"{__version__}-rc5"
88

99
__all__ = ["DEFAULT_RC_VERSION", "Kernel", "__version__"]
+32-32
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

3-
from semantic_kernel.agents.agent import Agent, AgentResponseItem, AgentThread
4-
from semantic_kernel.agents.autogen.autogen_conversable_agent import (
5-
AutoGenConversableAgent,
6-
AutoGenConversableAgentThread,
7-
)
8-
from semantic_kernel.agents.azure_ai.azure_ai_agent import AzureAIAgent, AzureAIAgentThread
9-
from semantic_kernel.agents.azure_ai.azure_ai_agent_settings import AzureAIAgentSettings
10-
from semantic_kernel.agents.bedrock.bedrock_agent import BedrockAgent, BedrockAgentThread
11-
from semantic_kernel.agents.chat_completion.chat_completion_agent import ChatCompletionAgent, ChatHistoryAgentThread
12-
from semantic_kernel.agents.group_chat.agent_chat import AgentChat
13-
from semantic_kernel.agents.group_chat.agent_group_chat import AgentGroupChat
14-
from semantic_kernel.agents.open_ai.azure_assistant_agent import AzureAssistantAgent
15-
from semantic_kernel.agents.open_ai.open_ai_assistant_agent import AssistantAgentThread, OpenAIAssistantAgent
3+
import importlib
164

17-
__all__ = [
18-
"Agent",
19-
"AgentChat",
20-
"AgentGroupChat",
21-
"AgentResponseItem",
22-
"AgentThread",
23-
"AssistantAgentThread",
24-
"AutoGenConversableAgent",
25-
"AutoGenConversableAgentThread",
26-
"AzureAIAgent",
27-
"AzureAIAgentSettings",
28-
"AzureAIAgentThread",
29-
"AzureAssistantAgent",
30-
"BedrockAgent",
31-
"BedrockAgentThread",
32-
"ChatCompletionAgent",
33-
"ChatHistoryAgentThread",
34-
"OpenAIAssistantAgent",
35-
]
5+
_AGENTS = {
6+
"Agent": ".agent",
7+
"AgentResponseItem": ".agent",
8+
"AgentThread": ".agent",
9+
"AutoGenConversableAgent": ".autogen.autogen_conversable_agent",
10+
"AutoGenConversableAgentThread": ".autogen.autogen_conversable_agent",
11+
"AzureAIAgent": ".azure_ai.azure_ai_agent",
12+
"AzureAIAgentSettings": ".azure_ai.azure_ai_agent_settings",
13+
"AzureAIAgentThread": ".azure_ai.azure_ai_agent",
14+
"BedrockAgent": ".bedrock.bedrock_agent",
15+
"BedrockAgentThread": ".bedrock.bedrock_agent",
16+
"ChatCompletionAgent": ".chat_completion.chat_completion_agent",
17+
"ChatHistoryAgentThread": ".chat_completion.chat_completion_agent",
18+
"AgentChat": ".group_chat.agent_chat",
19+
"AgentGroupChat": ".group_chat.agent_group_chat",
20+
"AzureAssistantAgent": ".open_ai.azure_assistant_agent",
21+
"AssistantAgentThread": ".open_ai.open_ai_assistant_agent",
22+
"OpenAIAssistantAgent": ".open_ai.open_ai_assistant_agent",
23+
}
24+
25+
26+
def __getattr__(name: str):
27+
if name in _AGENTS:
28+
submod_name = _AGENTS[name]
29+
module = importlib.import_module(submod_name, package=__name__)
30+
return getattr(module, name)
31+
raise AttributeError(f"module {__name__} has no attribute {name}")
32+
33+
34+
def __dir__():
35+
return list(_AGENTS.keys())
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
from .agent import Agent, AgentResponseItem, AgentThread
4+
from .autogen.autogen_conversable_agent import AutoGenConversableAgent, AutoGenConversableAgentThread
5+
from .azure_ai.azure_ai_agent import AzureAIAgent, AzureAIAgentThread
6+
from .azure_ai.azure_ai_agent_settings import AzureAIAgentSettings
7+
from .bedrock.bedrock_agent import BedrockAgent, BedrockAgentThread
8+
from .chat_completion.chat_completion_agent import ChatCompletionAgent, ChatHistoryAgentThread
9+
from .group_chat.agent_chat import AgentChat
10+
from .group_chat.agent_group_chat import AgentGroupChat
11+
from .open_ai.azure_assistant_agent import AzureAssistantAgent
12+
from .open_ai.open_ai_assistant_agent import AssistantAgentThread, OpenAIAssistantAgent
13+
14+
__all__ = [
15+
"Agent",
16+
"AgentChat",
17+
"AgentGroupChat",
18+
"AgentResponseItem",
19+
"AgentThread",
20+
"AssistantAgentThread",
21+
"AutoGenConversableAgent",
22+
"AutoGenConversableAgentThread",
23+
"AzureAIAgent",
24+
"AzureAIAgentSettings",
25+
"AzureAIAgentThread",
26+
"AzureAssistantAgent",
27+
"BedrockAgent",
28+
"BedrockAgentThread",
29+
"ChatCompletionAgent",
30+
"ChatHistoryAgentThread",
31+
"OpenAIAssistantAgent",
32+
]

0 commit comments

Comments
 (0)
Please sign in to comment.