Skip to content

Python 3.10 compatibility issue: issubclass(expected_type, pydantic.BaseModel) raises TypeError during model validation #1735

@Soulter

Description

@Soulter

In google-genai 1.51.0 on Python 3.10, calling client.models.list() or instantiating any Pydantic model under google.genai.types (e.g., UserContent) results in the following error:

TypeError: issubclass() arg 1 must be a class

Full traceback:

 [22:48:50] [Core] [ERRO] [routes.config:613]: Traceback (most recent call last):
  File "/Users/moonshot/AstrBot/astrbot/dashboard/routes/config.py", line 606, in get_provider_model_list
    models = await provider.get_models()
  File "/Users/moonshot/AstrBot/astrbot/core/provider/sources/gemini_source.py", line 726, in get_models
    models = await self.client.models.list()
  File "/Users/moonshot/AstrBot/.venv/lib/python3.10/site-packages/google/genai/models.py", line 7402, in list
    await self._list(config=config),
  File "/Users/moonshot/AstrBot/.venv/lib/python3.10/site-packages/google/genai/models.py", line 6550, in _list
    response = await self._api_client.async_request(
  File "/Users/moonshot/AstrBot/.venv/lib/python3.10/site-packages/google/genai/_api_client.py", line 1380, in async_request
    return SdkHttpResponse(headers=result.headers, body=response_body)
  File "/Users/moonshot/AstrBot/.venv/lib/python3.10/site-packages/pydantic/main.py", line 214, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
  File "/Users/moonshot/AstrBot/.venv/lib/python3.10/site-packages/google/genai/_common.py", line 591, in _check_field_type_mismatches
    issubclass(expected_type, pydantic.BaseModel) and
  File "/Users/moonshot/.local/share/uv/python/cpython-3.10.16-macos-aarch64-none/lib/python3.10/abc.py", line 123, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class

Root Cause

This issue is caused by a well-known difference in issubclass() behavior between Python 3.10 and Python 3.11+, specifically when the first argument is a GenericAlias, such as:

  • dict[str, str]
  • list[int]
  • tuple[str, int]

In Python 3.10, these types are not treated as real classes and trigger:

TypeError: issubclass() arg 1 must be a class

This happens inside the google/genai/_common.py validator:

if (isinstance(expected_type, type) and
    issubclass(expected_type, pydantic.BaseModel) and
    isinstance(value, pydantic.BaseModel) and
    not isinstance(value, expected_type)):

When expected_type is dict[str, str], the issubclass() call raises a TypeError on Python 3.10.

This is a Python bug, not specific to google-genai.

References:

  1. ABCMeta.__subclasscheck__() doesn't support duck typing. python/cpython#89010
  2. TypeError: issubclass() arg 1 must be a class with BaseModel pydantic/pydantic#5970

Behavior difference example

Python 3.10 (fails):

>>> import pydantic
>>> issubclass(dict[str, str], pydantic.BaseModel)
Traceback (most recent call last):
  ...
TypeError: issubclass() arg 1 must be a class

Python 3.11+ (works correctly):

>>> issubclass(dict[str, str], pydantic.BaseModel)
False

Environment details

  • Programming language: Python
  • OS: macOS
  • Language runtime version: CPython 3.10
  • Package version: google-genai 1.51.0

Steps to reproduce

  1. Create a Python 3.10 environment
  2. Install google-genai==1.51.0
  3. Call either:
  • await AsyncClient.models.list(), or
  • instantiate any Pydantic model in google.genai.types (e.g., UserContent)

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions