Skip to content

Conversation

@miettal
Copy link

@miettal miettal commented Jan 11, 2026

Summary

Fixes deprecation warnings in Python 3.14 by removing usage of private typing._UnionGenericAlias API.

Changes

  • Replaced _UnionGenericAlias with tuple-based isinstance() checks using type(Union[int, str])
  • Modified 3 files: types.py, _transformers.py, _automatic_function_calling_util.py
  • Maintains compatibility with both Union[T1, T2] and T1 | T2 syntax

Background

_UnionGenericAlias is a private API deprecated in Python 3.14 and scheduled for removal in Python 3.17. The new approach avoids private APIs while maintaining identical functionality for runtime type checking.

Testing

  • Verified the fix resolves deprecation warnings on Python 3.14
  • No changes to public API or behavior

🤖 Generated with Claude Code

@google-cla
Copy link

google-cla bot commented Jan 11, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.
@janasangeetha
Copy link
Collaborator

Hey @miettal
Thanks for contributing!
The branch is out-of-date. Kindly update the same.

@miettal
Copy link
Author

miettal commented Jan 14, 2026

I sync the branch. ( if rebase is needed, I will re-make branch)

@janasangeetha
Copy link
Collaborator

Kindly fix the failing check

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
(<class 'types.UnionType'>, <class 'typing._UnionGenericAlias'>).
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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants