Skip to content

fix(tools): keep T | None arguments in BaseTool.from_config - #7841

Open
adityasingh2400 wants to merge 1 commit into
google:mainfrom
adityasingh2400:fix/tool-from-config-pep604-optional
Open

adityasingh2400 wants to merge 1 commit into
google:mainfrom
adityasingh2400:fix/tool-from-config-pep604-optional

Conversation

@adityasingh2400

Copy link
Copy Markdown
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

Problem:
BaseTool.from_config unwraps Optional[T] with if get_origin(param_type) is Union. Before Python 3.14, a PEP 604 optional (T | None) has origin types.UnionType, so the unwrap was skipped, no branch matched, and the argument was dropped with an "Unsupported parsing for argument" warning. Built-in tools that use the default from_config and annotate their arguments this way silently lost them from YAML configs. That includes VertexAiRagRetrieval (rag_corpora, similarity_top_k, …), GoogleSearchTool (model) and ExecuteBashTool (workspace).

Solution:
Treat types.UnionType like typing.Union when unwrapping the optional, so T | None maps exactly like Optional[T]. On Python 3.14, where the two are the same type, nothing changes.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added to tests/unittests/tools/test_base_tool.py:

  • test_from_config_unwraps_pep604_optional_args: int | None, list[str] | None and float | None arguments are passed through, and Optional[str] still works.
  • test_vertex_ai_rag_retrieval_from_config_keeps_pep604_args: rag_corpora, similarity_top_k and vector_distance_threshold reach the tool's VertexRagStore.
# On main (before the fix, Python 3.11)
AssertionError: assert None == 5
AssertionError: assert None == ['projects/p/locations/us-central1/ragCorpora/123']

# With the fix
$ pytest tests/unittests/tools tests/unittests/agents
4389 passed, 1 xfailed

pre-commit run passes on the changed files, and mypy reports no issues in base_tool.py.

Manual End-to-End (E2E) Tests:

I loaded a YAML agent config through config_agent_utils.from_config. The config holds a VertexAiRagRetrieval tool with rag_corpora, similarity_top_k and vector_distance_threshold, and I inspected agent.tools[0].vertex_rag_store (Python 3.11):

=== BEFORE (main) ===
Unsupported parsing for argument: rag_corpora.
Unsupported parsing for argument: similarity_top_k.
Unsupported parsing for argument: vector_distance_threshold.
rag_corpora: None
similarity_top_k: None
vector_distance_threshold: None
=== AFTER (this PR) ===
rag_corpora: ['projects/p/locations/us-central1/ragCorpora/123']
similarity_top_k: 5
vector_distance_threshold: 0.4

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

#4371 / #4406 dealt with PEP 604 unions in function-declaration parsing. This is the separate tool-config path.

Before Python 3.14, get_origin(int | None) is types.UnionType, not
typing.Union, so from_config never unwrapped PEP 604 optionals and
dropped those arguments with an "Unsupported parsing" warning. YAML
configs for VertexAiRagRetrieval, GoogleSearchTool and ExecuteBashTool
silently lost them, e.g. a RAG tool with no corpus. Unwrap both forms.

Fixes google#7840
@adityasingh2400
adityasingh2400 marked this pull request as draft October 31, 2026 01:48
@adityasingh2400
adityasingh2400 marked this pull request as ready for review October 31, 2026 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BaseTool.from_config drops T | None arguments on Python 3.11-3.13

2 participants