|
16 | 16 |
|
17 | 17 | import inspect |
18 | 18 | import logging |
19 | | -import sys |
20 | 19 | from typing import Any |
21 | 20 | from typing import Callable |
22 | | -from typing import get_args |
23 | | -from typing import get_origin |
24 | 21 | from typing import Optional |
25 | 22 | from typing import Union |
26 | 23 |
|
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 | | - |
34 | 24 | from google.genai import types |
35 | 25 | import pydantic |
36 | 26 | from typing_extensions import override |
@@ -141,22 +131,9 @@ def _preprocess_args( |
141 | 131 | continue |
142 | 132 |
|
143 | 133 | 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 |
157 | 134 |
|
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. |
160 | 137 | try: |
161 | 138 | if target_type not in self._type_adapter_cache: |
162 | 139 | self._type_adapter_cache[target_type] = pydantic.TypeAdapter( |
|
0 commit comments