Skip to content

TypeError: Plain typing.TypeAlias is not valid as type argument in vertexai/_genai/_agent_engines_utils.py on Python 3.10 #6404

@osintalex

Description

@osintalex

Environment details

  • Python: 3.10.1
  • google-cloud-aiplatform: 1.140.0
  • google-adk: 1.26.0
  • OS: Linux (Kubernetes)
  • Buggy versions: 1.136.0, 1.137.0, 1.138.0, 1.139.0, 1.140.0
  • Python versions affected: 3.10.x
  • Last working version: 1.135.0

Steps to reproduce

Not sure this was encountered while using google adk which has this as a dependency.
Full traceback of error after making a POST request to create a session which triggered this error:

Traceback (most recent call last):
  File "/root/.pyenv/versions/3.10/lib/python3.10/site-packages/google/adk/cli/adk_web_server.py", line 660, in _create_session
    session = await self.session_service.create_session(
  File "/root/.pyenv/versions/3.10/lib/python3.10/site-packages/google/adk/sessions/vertex_ai_session_service.py", line 115, in create_session
    api_response = await api_client.agent_engines.sessions.create(
  File "/root/.pyenv/versions/3.10/lib/python3.10/site-packages/vertexai/_genai/client.py", line 111, in agent_engines
    self._agent_engines = importlib.import_module(
  File "/root/.pyenv/versions/3.10.1/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/root/.pyenv/versions/3.10/lib/python3.10/site-packages/vertexai/_genai/agent_engines.py", line 34, in <module>
    from . import _agent_engines_utils
  File "/root/.pyenv/versions/3.10/lib/python3.10/site-packages/vertexai/_genai/_agent_engines_utils.py", line 289, in <module>
    ADKAgent: Optional[TypeAlias] = BaseAgent
  File "/root/.pyenv/versions/3.10.1/lib/python3.10/typing.py", line 311, in inner
    return func(*args, **kwds)
  File "/root/.pyenv/versions/3.10.1/lib/python3.10/typing.py", line 402, in __getitem__
    return self._getitem(self, parameters)
  File "/root/.pyenv/versions/3.10.1/lib/python3.10/typing.py", line 528, in Optional
    arg = _type_check(parameters, f"{self} requires a single type.")
  File "/root/.pyenv/versions/3.10.1/lib/python3.10/typing.py", line 171, in _type_check
    raise TypeError(f"Plain {arg} is not valid as type argument")
TypeError: Plain typing.TypeAlias is not valid as type argument

Root Cause

File: vertexai/_genai/_agent_engines_utils.py
Line: 289

if typing.TYPE_CHECKING:
    from google.adk.agents import BaseAgent

    ADKAgent: TypeAlias = BaseAgent
else:
    try:
        from google.adk.agents import BaseAgent

        ADKAgent: Optional[TypeAlias] = BaseAgent  # this is the line causing issues
    except (ImportError, AttributeError):
        ADKAgent = None  # type: ignore[no-redef]

Not sure the syntax Optional[TypeAlias] valid 3.10? Confusing though since I think the syntax is valid unclear why it would cause this runtime error.

Introduced in: d8cc50dd67

Workaround

Pin to version 1.135.0 or earlier:

google-cloud-aiplatform = "1.135.0"

Metadata

Metadata

Assignees

No one assigned

    Labels

    api: vertex-aiIssues related to the googleapis/python-aiplatform API.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions