diff --git a/google/genai/_automatic_function_calling_util.py b/google/genai/_automatic_function_calling_util.py index ec7f9a702..cecb11597 100644 --- a/google/genai/_automatic_function_calling_util.py +++ b/google/genai/_automatic_function_calling_util.py @@ -26,9 +26,9 @@ if sys.version_info >= (3, 10): - VersionedUnionType = builtin_types.UnionType + VersionedUnionType = (builtin_types.UnionType, type(Union[int, str])) else: - VersionedUnionType = typing._UnionGenericAlias # type: ignore[attr-defined] + VersionedUnionType = (type(Union[int, str]),) __all__ = [ diff --git a/google/genai/_transformers.py b/google/genai/_transformers.py index be3303e6d..ce0a4b2ea 100644 --- a/google/genai/_transformers.py +++ b/google/genai/_transformers.py @@ -42,11 +42,11 @@ logger = logging.getLogger('google_genai._transformers') if sys.version_info >= (3, 10): - VersionedUnionType = builtin_types.UnionType + VersionedUnionType = (builtin_types.UnionType, type(Union[int, str])) _UNION_TYPES = (typing.Union, builtin_types.UnionType) from typing import TypeGuard else: - VersionedUnionType = typing._UnionGenericAlias # type: ignore[attr-defined] + VersionedUnionType = (type(Union[int, str]),) _UNION_TYPES = (typing.Union,) from typing_extensions import TypeGuard diff --git a/google/genai/types.py b/google/genai/types.py index fc2c51692..7ab0d8707 100644 --- a/google/genai/types.py +++ b/google/genai/types.py @@ -25,10 +25,10 @@ import sys import types as builtin_types import typing -from typing import Any, Callable, Dict, List, Literal, Optional, Sequence, Union, _UnionGenericAlias # type: ignore +from typing import Any, Callable, Dict, List, Literal, Optional, Sequence, Union import pydantic from pydantic import ConfigDict, Field, PrivateAttr, model_validator -from typing_extensions import Self, TypedDict +from typing_extensions import Self, TypeAlias, TypedDict from . import _common from ._operations_converters import ( _GenerateVideosOperation_from_mldev, @@ -40,11 +40,11 @@ if sys.version_info >= (3, 10): # Supports both Union[t1, t2] and t1 | t2 - VersionedUnionType = Union[builtin_types.UnionType, _UnionGenericAlias] + VersionedUnionType = (builtin_types.UnionType, type(Union[int, str])) _UNION_TYPES = (typing.Union, builtin_types.UnionType) else: # Supports only Union[t1, t2] - VersionedUnionType = _UnionGenericAlias + VersionedUnionType = (type(Union[int, str]),) _UNION_TYPES = (typing.Union,) _is_pillow_image_imported = False @@ -4251,10 +4251,22 @@ class ToolDict(TypedDict, total=False): ToolListUnion = list[ToolUnion] ToolListUnionDict = list[ToolUnionDict] -SchemaUnion = Union[ - dict[Any, Any], type, Schema, builtin_types.GenericAlias, VersionedUnionType # type: ignore[valid-type] -] -SchemaUnionDict = Union[SchemaUnion, SchemaDict] +if typing.TYPE_CHECKING: + # For type checkers, use the most complete type (Python 3.10+) + SchemaUnion: TypeAlias = Union[ + dict[Any, Any], type, Schema, builtin_types.GenericAlias, builtin_types.UnionType, type(Union[int, str]) + ] +else: + # At runtime, use version-appropriate types + if sys.version_info >= (3, 10): + SchemaUnion: TypeAlias = Union[ + dict[Any, Any], type, Schema, builtin_types.GenericAlias, builtin_types.UnionType, type(Union[int, str]) + ] + else: + SchemaUnion: TypeAlias = Union[ + dict[Any, Any], type, Schema, builtin_types.GenericAlias, type(Union[int, str]) + ] +SchemaUnionDict: TypeAlias = Union[SchemaUnion, SchemaDict] class LatLng(_common.BaseModel):