|
26 | 26 |
|
27 | 27 | from . import isolated_import_utils |
28 | 28 | from .isolated_import_utils import assert_modules_unloaded |
| 29 | +from .isolated_import_utils import loaded_top_level_packages |
29 | 30 | from .isolated_import_utils import run_isolated |
30 | 31 |
|
31 | 32 | pytestmark = pytest.mark.skipif( |
|
42 | 43 | 'google.adk.workflow', |
43 | 44 | ) |
44 | 45 |
|
| 46 | +# The statements almost every ADK program starts with, and therefore the two |
| 47 | +# import graphs whose cost every user pays. |
| 48 | +_ENTRY_POINTS = ( |
| 49 | + 'from google.adk.agents import Agent', |
| 50 | + 'from google.adk.runners import Runner', |
| 51 | +) |
| 52 | + |
| 53 | +# Third-party top-level packages an entry point may load. The forbidden lists |
| 54 | +# above pin individual deferrals on the lazy package inits; this one bounds the |
| 55 | +# whole graph, because the cost that reaches users arrives as a package nobody |
| 56 | +# noticed rather than as one somebody predicted. |
| 57 | +_ENTRY_POINT_PACKAGE_ALLOWLIST = frozenset({ |
| 58 | + # Declared requirements that ADK imports at module scope. |
| 59 | + 'click', |
| 60 | + 'fastapi', |
| 61 | + 'google', |
| 62 | + 'httpx', |
| 63 | + 'opentelemetry', |
| 64 | + 'packaging', |
| 65 | + 'pydantic', |
| 66 | + 'python_multipart', |
| 67 | + 'starlette', |
| 68 | + 'tenacity', |
| 69 | + 'websockets', |
| 70 | + # Reached through pydantic and httpx rather than through ADK. |
| 71 | + 'annotated_doc', |
| 72 | + 'annotated_types', |
| 73 | + 'anyio', |
| 74 | + 'certifi', |
| 75 | + 'idna', |
| 76 | + 'orjson', |
| 77 | + 'pydantic_core', |
| 78 | + 'pygments', |
| 79 | + 'rich', |
| 80 | + 'sniffio', |
| 81 | + 'typing_extensions', |
| 82 | + 'typing_inspection', |
| 83 | + 'zstandard', |
| 84 | + # google.genai.types annotates optional fields with aiohttp and Pillow |
| 85 | + # types and imports whichever of the two the environment happens to have. |
| 86 | + # No ADK module imports either one, so these are absent in some installs. |
| 87 | + 'PIL', |
| 88 | + 'aiohappyeyeballs', |
| 89 | + 'aiohttp', |
| 90 | + 'aiosignal', |
| 91 | + 'attr', |
| 92 | + 'defusedxml', |
| 93 | + 'frozenlist', |
| 94 | + 'multidict', |
| 95 | + 'propcache', |
| 96 | + 'yarl', |
| 97 | +}) |
| 98 | + |
45 | 99 |
|
46 | 100 | @pytest.mark.parametrize( |
47 | 101 | ('module_name', 'forbidden'), |
@@ -104,6 +158,29 @@ def test_package_import_defers_unrelated_runtime( |
104 | 158 | ) |
105 | 159 |
|
106 | 160 |
|
| 161 | +@pytest.mark.parametrize('statement', _ENTRY_POINTS, ids=('agent', 'runner')) |
| 162 | +def test_entry_point_loads_only_allowlisted_packages(statement: str) -> None: |
| 163 | + """The two entry points every program uses load a reviewed set of packages. |
| 164 | +
|
| 165 | + The lazy package inits are already cheap, so a new eager dependency shows up |
| 166 | + here first: as a package nobody agreed to pay for on every ADK start. |
| 167 | +
|
| 168 | + The unit is the top-level import name, so a new eager dependency arriving |
| 169 | + under the `google` namespace, which ADK loads either way, does not show up |
| 170 | + here. |
| 171 | + """ |
| 172 | + unexpected = sorted( |
| 173 | + loaded_top_level_packages(statement) - _ENTRY_POINT_PACKAGE_ALLOWLIST |
| 174 | + ) |
| 175 | + |
| 176 | + assert not unexpected, ( |
| 177 | + f'{statement!r} now loads {", ".join(unexpected)}, which every ADK' |
| 178 | + ' process would pay for at startup. Move the import into the function' |
| 179 | + ' that needs it, or add the package to the allowlist together with the' |
| 180 | + ' reason it has to be eager.' |
| 181 | + ) |
| 182 | + |
| 183 | + |
107 | 184 | def test_constructing_agent_defers_optional_mcp_server_stack(): |
108 | 185 | """A normal Agent does not import MCP just because its extra is installed.""" |
109 | 186 | if importlib.util.find_spec('mcp') is None: |
|
0 commit comments