Skip to content

Agent import validation can fail for newly created agent packages#6948

Description

@yousefalbanna1

馃敶 Required Information

Please ensure all items in this section are completed to allow for efficient
triaging. Requests without complete information may be rejected / deprioritized.
If an item is not applicable to you - please mark it as N/A

Describe the Bug:

_validate_agent_import() can intermittently fail to import a valid newly created agent package.

The agent package contains both __init__.py and agent.py, but a subsequent dynamic import can fail with ModuleNotFoundError. This appears to happen when Python's import finder has cached the contents of the parent directory before the new package is created.

Steps to Reproduce:

  1. Check out google/adk-python at commit 8cdbbb15.
  2. Run the following two existing unit tests in this order:
python -m pytest \
  tests/unittests/cli/utils/test_cli_deploy.py::TestValidateAgentImport::test_success_with_root_agent_export \
  tests/unittests/cli/utils/test_cli_deploy.py::TestValidateAgentImport::test_success_with_app_export \
  -q
  1. The first test passes.
  2. The second test can fail while importing the newly created agent package.

Expected Behavior:
A clear and concise description of what you expected to happen.

Both tests should pass. Once the test creates agent.py and __init__.py,_validate_agent_import() should be able to import the package successfully.

Observed Behavior:
What actually happened? Include error messages or crash stack traces here.
The first test passes, but the second test fails with:

.F                                                                                                               [100%]

E       ModuleNotFoundError: No module named 'test_success_with_app_export0'

The above exception was the direct cause of the following exception:

E           click.exceptions.ClickException: Failed to import agent module:
E           No module named 'test_success_with_app_export0'

=============================================== short test summary info ================================================
FAILED tests/unittests/cli/utils/test_cli_deploy.py::TestValidateAgentImport::test_success_with_app_export - click.exceptions.ClickException: Failed to import agent module:
1 failed, 1 passed in 0.27s

The failure occurs at:
module = importlib.import_module(f'{module_name}.agent')
even though test_success_with_app_export has already created both agent.py and init.py

Environment Details:

  • ADK Library Version (pip show google-adk):2.8.0
  • Desktop OS:** [e.g., macOS, Linux, Windows] Ubuntu 24.04.4 LTS
  • Python Version (python -V):Python 3.11.16

Model Information:

-Are you using LiteLLM: N/A
-Which model is being used: N/A


馃煛 Optional Information

Providing this information greatly speeds up the resolution process.

Regression:
Did this work in a previous version of ADK? If so, which one?
Unknown.
Logs:
Please attach relevant logs. Wrap them in code blocks (```) or attach a

text file.

.F                                                                                                               [100%]

E       ModuleNotFoundError: No module named 'test_success_with_app_export0'

The above exception was the direct cause of the following exception:

E           click.exceptions.ClickException: Failed to import agent module:
E           No module named 'test_success_with_app_export0'

=============================================== short test summary info ================================================
FAILED tests/unittests/cli/utils/test_cli_deploy.py::TestValidateAgentImport::test_success_with_app_export - click.exceptions.ClickException: Failed to import agent module:
1 failed, 1 passed in 0.27s

Additional Context:
Add any other context about the problem here.

The reproduction above was run against an unmodified checkout of current upstream main at commit 8cdbbb1.

The failure appears to be related to Python's import finder retaining stale directory contents when a new agent package is created after the parent directory has already been scanned.

PR #6929 contains a proposed fix and a deterministic regression test.

Minimal Reproduction Code:
Please provide a code snippet or a link to a Gist/repo that isolates the issue.

the issue can be reproduced using the existing ADK unit tests shown in the steps above.

How often has this issue occurred?:

  • Intermittently (<50%)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions