Skip to content

Deprecated Pydantic pattern with @model_validator since pydantic 2.12 #1579

@Giuzzilla

Description

@Giuzzilla

This library is using a deprecated Pydantic pattern, which raises a deprecation warning since Pydantic 2.12.3 and an error on Pydantic 2.12.0->2.12.2.

The deprecation warning is PydanticDeprecatedSince212: Using '@model_validator' with mode='after' on a classmethod is deprecated.

The deprecated pattern can be found in google/genai/types.py within the Metric class:

  @model_validator(mode='after')  # type: ignore[arg-type]
  @classmethod
  def validate_name(cls, model: 'Metric') -> 'Metric':
    if not model.name:
      raise ValueError('Metric name cannot be empty.')
    model.name = model.name.lower()
    return model

This code should be:

  @model_validator(mode='after')
  def validate_name(self) -> 'Metric':
    if not self.name:
      raise ValueError('Metric name cannot be empty.')
    self.name = self.name.lower()
    return model

The issue is very similar to the one reported in apache/iceberg-python#2590 and solved via apache/iceberg-python#2626. There you can find more context on the issue itself.

I could easily open a PR to fix this but the header of the file says Code generated by the Google Gen AI SDK generator DO NOT EDIT. so it seems editing it directly is not permitted.

Environment details

  • Programming language: Python
  • OS: Ubuntu
  • Language runtime version: 3.14
  • Package version: 1.46.0 (also on older versions)

Steps to reproduce

  1. Have an installation with Pydantic >= 2.12 (2.12.3 for a deprecation warning instead of an error)
  2. (example) import google.genai types from google.genai import types as genai_types

Metadata

Metadata

Assignees

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

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions