Skip to content

fix(cli): use Optional[List[str]] in cleanup_unused_files to fix parser error#6190

Open
syzayd wants to merge 3 commits into
google:mainfrom
syzayd:fix/cleanup-unused-files-optional-type-hints
Open

fix(cli): use Optional[List[str]] in cleanup_unused_files to fix parser error#6190
syzayd wants to merge 3 commits into
google:mainfrom
syzayd:fix/cleanup-unused-files-optional-type-hints

Conversation

@syzayd

@syzayd syzayd commented Jun 22, 2026

Copy link
Copy Markdown

Problem

When the ADK web server runs and the LLM calls the cleanup_unused_files built-in tool, the automatic function calling system fails to parse its optional parameter type annotations on Python 3.10:

Error in event_generator: Failed to parse the parameter
file_patterns: List[str] | None = None of function
cleanup_unused_files for automatic function calling

The root cause is that file_patterns and exclude_patterns use the PEP 604 union syntax (list[str] | None) which ADK's _parse_schema_from_parameter cannot handle in all Python versions. list[str] | None evaluates to types.UnionType (Python 3.10+), while Optional[List[str]] evaluates to typing._GenericAlias — the latter has a well-tested path through the parser.

Fixes #3591

Fix

All sibling tools in src/google/adk/cli/built_in_agents/tools/ already use Optional[List[str]] from the typing module (see write_files.py, delete_files.py, read_files.py). This PR aligns cleanup_unused_files.py with that established pattern:

  • Add from typing import List and from typing import Optional
  • Replace list[str] | None with Optional[List[str]] on file_patterns and exclude_patterns
  • Change used_files: list[str] to used_files: List[str] for style consistency

Testing Plan

  • Manual reproduction (Python 3.10): Before the fix, from_function_with_options(cleanup_unused_files, variant) raises ValueError: Failed to parse the parameter file_patterns: list[str] | None = None. After the fix, it parses cleanly and returns nullable=True, type=ARRAY for both optional params.
  • Consistency check: Confirmed that all sibling tools (write_files.py, delete_files.py, read_files.py) use Optional[List[str]] — this PR makes cleanup_unused_files.py consistent with the existing pattern.
  • Existing tests: No test changes required. This is a type annotation change only; runtime behavior is identical.
  • adk web smoke test: Launch adk web and invoke an agent that uses the cleanup_unused_files built-in tool. Confirm no parser warning appears in the console output.

Reproduction script

A minimal repro (repro_3591.py) is included in this branch. It calls from_function_with_options directly on both the old and new signatures to show the before/after behavior.

…er error

ADK's automatic function calling system cannot parse the PEP 604 union
syntax `list[str] | None` when building the tool schema for Gemini.
This causes the following error every time the ADK web server runs:

    Failed to parse the parameter file_patterns: List[str] | None = None
    of function cleanup_unused_files for automatic function calling

All sibling tools in this directory (delete_files.py, read_files.py,
write_files.py, etc.) already use `Optional[List[str]]` from `typing`.
This commit aligns cleanup_unused_files.py with that established pattern.

Changes:
- Add `from typing import List` and `from typing import Optional`
- Replace `list[str] | None` with `Optional[List[str]]` on both optional
  parameters (file_patterns, exclude_patterns)
- Change `used_files: list[str]` to `used_files: List[str]` for style
  consistency with sibling tools

Fixes google#3591
@google-cla

google-cla Bot commented Jun 22, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@adk-bot adk-bot added the tools [Component] This issue is related to tools label Jun 24, 2026
@adk-bot

adk-bot commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

Response from ADK Triaging Agent

Hello @syzayd, thank you for creating this PR!

We appreciate your fix! It looks like there are a couple of things missing based on our contribution guidelines:

  1. Contributor License Agreement (CLA): The CLA check is currently failing. Please sign the Google CLA at https://cla.developers.google.com/ to proceed.
  2. Logs or Screenshots: Since this is a bug fix for a tool parse error, could you please provide a log or screenshot from adk web showing the tool runs successfully without the parser warning?
  3. Testing Plan: Please include a formal testing plan section in your PR description.

This information will help reviewers to review your PR more efficiently. Thanks!

@rohityan rohityan self-assigned this Jun 24, 2026
@rohityan

Copy link
Copy Markdown
Collaborator

Hi @syzayd, Thank you for your contribution! It appears you haven't yet signed the Contributor License Agreement (CLA). Please visit https://cla.developers.google.com/ to complete the signing process. Once the CLA is signed, we'll be able to proceed with the review of your PR. Thank you!

@rohityan rohityan added the request clarification [Status] The maintainer need clarification or more information from the author label Jun 26, 2026
repro_3591.py exercises from_function_with_options() directly on both
the old list[str] | None signature and the fixed Optional[List[str]]
signature, confirming the parser error and its resolution.

Includes Apache 2.0 license header per project convention.
@syzayd syzayd force-pushed the fix/cleanup-unused-files-optional-type-hints branch from 3e0ada7 to 6f3cf03 Compare June 26, 2026 04:44
@syzayd

syzayd commented Jun 26, 2026

Copy link
Copy Markdown
Author

Hi, I've addressed all the requested items:

  • CLA: Signed: the cla/google check is now passing
  • Testing plan: Added a formal testing plan section to the PR description
  • Reproduction: Added repro_3591.py to the branch, which exercises from_function_with_options() directly on both the old and fixed signatures

Ready for review when you get a chance. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

request clarification [Status] The maintainer need clarification or more information from the author tools [Component] This issue is related to tools

Projects

None yet

3 participants