From e290f468ebe488423b1050b8a84f18e5b3d949dc Mon Sep 17 00:00:00 2001 From: Hiromasa Ihara Date: Sun, 11 Jan 2026 20:23:14 +0900 Subject: [PATCH 1/3] fix: Remove deprecated _UnionGenericAlias to support Python 3.14+ Replace usage of private typing._UnionGenericAlias (deprecated in Python 3.14, removed in 3.17) with tuple-based isinstance checks using type(Union[int, str]). This maintains compatibility with both old-style Union[T1, T2] and new-style T1 | T2 syntax while avoiding deprecation warnings. --- google/genai/_automatic_function_calling_util.py | 4 ++-- google/genai/_transformers.py | 4 ++-- google/genai/types.py | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) 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..28b58f1db 100644 --- a/google/genai/types.py +++ b/google/genai/types.py @@ -25,7 +25,7 @@ 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 @@ -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 From b080641cfb9e9b6700178cc6725f731ff525bde4 Mon Sep 17 00:00:00 2001 From: Hiromasa Ihara Date: Wed, 14 Jan 2026 16:32:51 +0900 Subject: [PATCH 2/3] fix: Expand VersionedUnionType tuple in SchemaUnion definition Fixes ImportError caused by passing a tuple to Union[]. The previous commit (e290f46) changed VersionedUnionType from a Union type to a tuple for isinstance checks, but SchemaUnion was still trying to use it as a Union argument. This expands the tuple elements individually for Python 3.10+ (UnionType + _UnionGenericAlias) and Python < 3.10 (_UnionGenericAlias only). Error: TypeError: Union[arg, ...]: each arg must be a type. Got (, ). --- google/genai/types.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/google/genai/types.py b/google/genai/types.py index 28b58f1db..76466b1f0 100644 --- a/google/genai/types.py +++ b/google/genai/types.py @@ -4251,9 +4251,14 @@ 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] -] +if sys.version_info >= (3, 10): + SchemaUnion = Union[ + dict[Any, Any], type, Schema, builtin_types.GenericAlias, builtin_types.UnionType, type(Union[int, str]) + ] +else: + SchemaUnion = Union[ + dict[Any, Any], type, Schema, builtin_types.GenericAlias, type(Union[int, str]) + ] SchemaUnionDict = Union[SchemaUnion, SchemaDict] From b44e435b47b1c53ee738f242cc7d89474da8176a Mon Sep 17 00:00:00 2001 From: Hiromasa Ihara Date: Fri, 16 Jan 2026 14:06:13 +0900 Subject: [PATCH 3/3] fix: Add TypeAlias annotations to SchemaUnion for mypy compatibility Resolves mypy errors by adding TypeAlias annotations and using TYPE_CHECKING to separate type-checking time definitions from runtime definitions. This ensures mypy recognizes SchemaUnion as a valid type alias while maintaining runtime compatibility across Python versions. Fixes mypy errors: - "Invalid type alias: expression is not a valid type" - "Variable 'genai.types.SchemaUnion' is not valid as a type" --- google/genai/types.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/google/genai/types.py b/google/genai/types.py index 76466b1f0..7ab0d8707 100644 --- a/google/genai/types.py +++ b/google/genai/types.py @@ -28,7 +28,7 @@ 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, @@ -4251,15 +4251,22 @@ class ToolDict(TypedDict, total=False): ToolListUnion = list[ToolUnion] ToolListUnionDict = list[ToolUnionDict] -if sys.version_info >= (3, 10): - SchemaUnion = Union[ +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: - SchemaUnion = Union[ - dict[Any, Any], type, Schema, builtin_types.GenericAlias, type(Union[int, str]) - ] -SchemaUnionDict = Union[SchemaUnion, SchemaDict] + # 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):