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
This commit is contained in:
Xiang (Sean) Zhou
2025-08-19 12:06:59 -07:00
committed by Copybara-Service
parent 77ed1f5f15
commit 018db79d13
+22 -18
View File
@@ -26,26 +26,30 @@ __all__ = [
'BuiltInCodeExecutor',
'CodeExecutorContext',
'UnsafeLocalCodeExecutor',
'VertexAiCodeExecutor',
'ContainerCodeExecutor',
]
try:
from .vertex_ai_code_executor import VertexAiCodeExecutor
__all__.append('VertexAiCodeExecutor')
except ImportError:
logger.debug(
'The Vertex sdk is not installed. If you want to use the Vertex Code'
' Interpreter with agents, please install it. If not, you can ignore this'
' warning.'
)
def __getattr__(name: str):
if name == 'VertexAiCodeExecutor':
try:
from .vertex_ai_code_executor import VertexAiCodeExecutor
try:
from .container_code_executor import ContainerCodeExecutor
return VertexAiCodeExecutor
except ImportError as e:
raise ImportError(
'VertexAiCodeExecutor requires additional dependencies. '
'Please install with: pip install "google-adk[extensions]"'
) from e
elif name == 'ContainerCodeExecutor':
try:
from .container_code_executor import ContainerCodeExecutor
__all__.append('ContainerCodeExecutor')
except ImportError:
logger.debug(
'The docker sdk is not installed. If you want to use the Container Code'
' Executor with agents, please install it. If not, you can ignore this'
' warning.'
)
return ContainerCodeExecutor
except ImportError as e:
raise ImportError(
'ContainerCodeExecutor requires additional dependencies. '
'Please install with: pip install "google-adk[extensions]"'
) from e
raise AttributeError(f"module '{__name__}' has no attribute '{name}'")