Skip to content

Commit f469591

Browse files
refactor: remove manual Optional unwrap, TypeAdapter handles it natively
1 parent 21beaea commit f469591

File tree

1 file changed

+2
-25
lines changed

1 file changed

+2
-25
lines changed

src/google/adk/tools/function_tool.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,11 @@
1616

1717
import inspect
1818
import logging
19-
import sys
2019
from typing import Any
2120
from typing import Callable
22-
from typing import get_args
23-
from typing import get_origin
2421
from typing import Optional
2522
from typing import Union
2623

27-
if sys.version_info >= (3, 10):
28-
from types import UnionType
29-
30-
_UNION_TYPES = (Union, UnionType)
31-
else:
32-
_UNION_TYPES = (Union,)
33-
3424
from google.genai import types
3525
import pydantic
3626
from typing_extensions import override
@@ -141,22 +131,9 @@ def _preprocess_args(
141131
continue
142132

143133
target_type = param.annotation
144-
is_optional = False
145-
146-
# Handle Optional[T] (Union[T, None]) and T | None (Python 3.10+)
147-
if get_origin(param.annotation) in _UNION_TYPES:
148-
union_args = get_args(param.annotation)
149-
non_none_types = [arg for arg in union_args if arg is not type(None)]
150-
if len(non_none_types) == 1:
151-
target_type = non_none_types[0]
152-
is_optional = len(union_args) != len(non_none_types)
153-
154-
# Skip None values only for Optional params
155-
if args[param_name] is None and is_optional:
156-
continue
157134

158-
# Validate and coerce all annotated types using TypeAdapter.
159-
# This handles primitives, enums, Pydantic models, and container types.
135+
# Validate and coerce using TypeAdapter. Handles primitives, enums,
136+
# Pydantic models, Optional[T], T | None, and container types natively.
160137
try:
161138
if target_type not in self._type_adapter_cache:
162139
self._type_adapter_cache[target_type] = pydantic.TypeAdapter(

0 commit comments

Comments
 (0)