Skip to content

Commit 825f3b9

Browse files
fix: handle Python 3.10+ union syntax (T | None) in Optional unwrap
1 parent ab7cb7c commit 825f3b9

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

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
@@ -127,8 +135,8 @@ def _preprocess_args(
127135
target_type = param.annotation
128136
is_optional = False
129137

130-
# Handle Optional[T] (Union[T, None]) - unwrap to get inner type
131-
if get_origin(param.annotation) is Union:
138+
# Handle Optional[T] (Union[T, None]) and T | None (Python 3.10+)
139+
if get_origin(param.annotation) in _UNION_TYPES:
132140
union_args = get_args(param.annotation)
133141
non_none_types = [arg for arg in union_args if arg is not type(None)]
134142
if len(non_none_types) == 1:

0 commit comments

Comments
 (0)