Skip to content

Commit a661baf

Browse files
9chait9DrStrangent Bot
authored andcommitted
Fix #2433: Lazy load modules to improve import time
1 parent 22fc332 commit a661baf

File tree

2 files changed

+73
-16
lines changed

2 files changed

+73
-16
lines changed

src/google/adk/__init__.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,25 @@
1313
# limitations under the License.
1414

1515
from __future__ import annotations
16+
from typing import TYPE_CHECKING
1617

17-
from . import version
18-
from .agents.context import Context
19-
from .agents.llm_agent import Agent
20-
from .runners import Runner
18+
if TYPE_CHECKING:
19+
from .agents.context import Context
20+
from .agents.llm_agent import Agent
21+
from .runners import Runner
22+
else:
23+
def __getattr__(name: str):
24+
if name == "Agent":
25+
from .agents.llm_agent import Agent
26+
return Agent
27+
if name == "Context":
28+
from .agents.context import Context
29+
return Context
30+
if name == "Runner":
31+
from .runners import Runner
32+
return Runner
33+
raise AttributeError(f"module {__name__} has no attribute {name}")
2134

35+
from . import version
2236
__version__ = version.__version__
2337
__all__ = ["Agent", "Context", "Runner"]

src/google/adk/agents/__init__.py

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,61 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from .base_agent import BaseAgent
16-
from .context import Context
17-
from .invocation_context import InvocationContext
18-
from .live_request_queue import LiveRequest
19-
from .live_request_queue import LiveRequestQueue
20-
from .llm_agent import Agent
21-
from .llm_agent import LlmAgent
22-
from .loop_agent import LoopAgent
23-
from .mcp_instruction_provider import McpInstructionProvider
24-
from .parallel_agent import ParallelAgent
25-
from .run_config import RunConfig
26-
from .sequential_agent import SequentialAgent
15+
from __future__ import annotations
16+
from typing import TYPE_CHECKING
17+
18+
if TYPE_CHECKING:
19+
from .base_agent import BaseAgent
20+
from .context import Context
21+
from .invocation_context import InvocationContext
22+
from .live_request_queue import LiveRequest
23+
from .live_request_queue import LiveRequestQueue
24+
from .llm_agent import Agent
25+
from .llm_agent import LlmAgent
26+
from .loop_agent import LoopAgent
27+
from .mcp_instruction_provider import McpInstructionProvider
28+
from .parallel_agent import ParallelAgent
29+
from .run_config import RunConfig
30+
from .sequential_agent import SequentialAgent
31+
32+
def __getattr__(name: str):
33+
if name == 'BaseAgent':
34+
from .base_agent import BaseAgent
35+
return BaseAgent
36+
if name == 'Context':
37+
from .context import Context
38+
return Context
39+
if name == 'InvocationContext':
40+
from .invocation_context import InvocationContext
41+
return InvocationContext
42+
if name == 'LiveRequest':
43+
from .live_request_queue import LiveRequest
44+
return LiveRequest
45+
if name == 'LiveRequestQueue':
46+
from .live_request_queue import LiveRequestQueue
47+
return LiveRequestQueue
48+
if name == 'Agent':
49+
from .llm_agent import Agent
50+
return Agent
51+
if name == 'LlmAgent':
52+
from .llm_agent import LlmAgent
53+
return LlmAgent
54+
if name == 'LoopAgent':
55+
from .loop_agent import LoopAgent
56+
return LoopAgent
57+
if name == 'McpInstructionProvider':
58+
from .mcp_instruction_provider import McpInstructionProvider
59+
return McpInstructionProvider
60+
if name == 'ParallelAgent':
61+
from .parallel_agent import ParallelAgent
62+
return ParallelAgent
63+
if name == 'RunConfig':
64+
from .run_config import RunConfig
65+
return RunConfig
66+
if name == 'SequentialAgent':
67+
from .sequential_agent import SequentialAgent
68+
return SequentialAgent
69+
raise AttributeError(f"module {__name__} has no attribute {name}")
2770

2871
__all__ = [
2972
'Agent',

0 commit comments

Comments
 (0)