Skip to content

Fix PreTrainedConfig as Pydantic field type after dataclass conversion#45080

Closed
joaquinhuigomez wants to merge 1 commit intohuggingface:mainfrom
joaquinhuigomez:fix/pretrained-config-pydantic-compat
Closed

Fix PreTrainedConfig as Pydantic field type after dataclass conversion#45080
joaquinhuigomez wants to merge 1 commit intohuggingface:mainfrom
joaquinhuigomez:fix/pretrained-config-pydantic-compat

Conversation

@joaquinhuigomez
Copy link
Copy Markdown
Contributor

Root cause

The v5.4.0 release converted PreTrainedConfig from a regular class to a @dataclass. This changes how Pydantic handles it when used as a field type in a BaseModel: instead of treating it as an opaque arbitrary type, Pydantic now introspects the dataclass fields and attempts to resolve all type annotations.

The dtype field is annotated as Union[str, "torch.dtype"] | None, where "torch.dtype" is a forward reference that depends on torch being in the namespace. Since torch is only imported under TYPE_CHECKING, Pydantic raises PydanticUndefinedAnnotation: name 'torch' is not defined.

Fix

Add __get_pydantic_core_schema__ to PreTrainedConfig that returns a core_schema.is_instance_schema(cls). This tells Pydantic to validate values by type-checking rather than introspecting the dataclass fields, restoring the pre-v5.4.0 behavior.

The method is inherited by all subclasses (e.g. BertConfig), so they also work as Pydantic field types.

Reproduction

from pydantic import BaseModel, ConfigDict, Field
from transformers import PretrainedConfig

class MyModelConfig(BaseModel):
    model_config = ConfigDict(arbitrary_types_allowed=True)
    sub_config: PretrainedConfig = Field(description="Configuration for the sub_config")

MyModelConfig.model_rebuild(force=True)  # raises PydanticSchemaGenerationError on v5.4.0

After this fix, the above passes.

Fixes #45070

The v5.4.0 conversion of PreTrainedConfig to a dataclass causes Pydantic
to introspect its field annotations when used as a field type in a
BaseModel. This fails because the dtype field uses a forward reference to
torch.dtype that is only importable under TYPE_CHECKING.

Add __get_pydantic_core_schema__ to return an is-instance schema, which
tells Pydantic to validate instances by type check rather than trying to
resolve the dataclass fields.

Fixes huggingface#45070
@github-actions
Copy link
Copy Markdown
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=45080&sha=7aa9f5

@joaquinhuigomez
Copy link
Copy Markdown
Contributor Author

Going to close this — needs more investigation than I initially thought.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v5.4.0 breaks PretrainedConfig field in pydantic model

1 participant