Skip to content

Commit 7bd0697

Browse files
fix: handle Python 3.10+ union syntax (T | None) in Optional unwrap
1 parent cc8973a commit 7bd0697

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/google/adk/tools/function_tool.py

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

1717
import inspect
1818
import logging
19+
import sys
1920
from typing import Any
2021
from typing import Callable
2122
from typing import get_args
2223
from typing import get_origin
2324
from typing import Optional
2425
from typing import Union
2526

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+
2634
from google.genai import types
2735
import pydantic
2836
from typing_extensions import override
@@ -130,8 +138,8 @@ def _preprocess_args(
130138
target_type = param.annotation
131139
is_optional = False
132140

133-
# Handle Optional[T] (Union[T, None]) - unwrap to get inner type
134-
if get_origin(param.annotation) is Union:
141+
# Handle Optional[T] (Union[T, None]) and T | None (Python 3.10+)
142+
if get_origin(param.annotation) in _UNION_TYPES:
135143
union_args = get_args(param.annotation)
136144
non_none_types = [arg for arg in union_args if arg is not type(None)]
137145
if len(non_none_types) == 1:

0 commit comments

Comments
 (0)