Skip to content

Commit 018db79

Browse files
seanzhougooglecopybara-github
authored andcommitted
fix: Lazy load VertexAiCodeExecutor and ContainerCodeExecutor
VertexAiCodeExecutor and ContainerCodeExecutor request additional dependencies, lazy load them so that when users import other names, they won't get impacted if they don't had those additional dependencies installed. Original codes swallow the exception and log a debug log is wrong and awkward, should follow the same style as what we did in https://github.com/google/adk-python/blob/main/src/google/adk/tools/retrieval/__init__.py PiperOrigin-RevId: 796969332
1 parent 77ed1f5 commit 018db79

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/google/adk/code_executors/__init__.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,30 @@
2626
'BuiltInCodeExecutor',
2727
'CodeExecutorContext',
2828
'UnsafeLocalCodeExecutor',
29+
'VertexAiCodeExecutor',
30+
'ContainerCodeExecutor',
2931
]
3032

31-
try:
32-
from .vertex_ai_code_executor import VertexAiCodeExecutor
33-
34-
__all__.append('VertexAiCodeExecutor')
35-
except ImportError:
36-
logger.debug(
37-
'The Vertex sdk is not installed. If you want to use the Vertex Code'
38-
' Interpreter with agents, please install it. If not, you can ignore this'
39-
' warning.'
40-
)
41-
42-
try:
43-
from .container_code_executor import ContainerCodeExecutor
44-
45-
__all__.append('ContainerCodeExecutor')
46-
except ImportError:
47-
logger.debug(
48-
'The docker sdk is not installed. If you want to use the Container Code'
49-
' Executor with agents, please install it. If not, you can ignore this'
50-
' warning.'
51-
)
33+
34+
def __getattr__(name: str):
35+
if name == 'VertexAiCodeExecutor':
36+
try:
37+
from .vertex_ai_code_executor import VertexAiCodeExecutor
38+
39+
return VertexAiCodeExecutor
40+
except ImportError as e:
41+
raise ImportError(
42+
'VertexAiCodeExecutor requires additional dependencies. '
43+
'Please install with: pip install "google-adk[extensions]"'
44+
) from e
45+
elif name == 'ContainerCodeExecutor':
46+
try:
47+
from .container_code_executor import ContainerCodeExecutor
48+
49+
return ContainerCodeExecutor
50+
except ImportError as e:
51+
raise ImportError(
52+
'ContainerCodeExecutor requires additional dependencies. '
53+
'Please install with: pip install "google-adk[extensions]"'
54+
) from e
55+
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")

0 commit comments

Comments
 (0)