fix(config): annotate PreTrainedConfig.dtype as Any to fix pydantic schema generation (#45070)#45129
Closed
IgnazioDS wants to merge 1 commit intohuggingface:mainfrom
Closed
Conversation
…chema generation Fixes huggingface#45070. PreTrainedConfig.dtype was annotated as Union[str, "torch.dtype"] | None. When torch is only imported under TYPE_CHECKING, pydantic's schema builder encounters the "torch.dtype" forward reference at runtime and fails with PydanticUndefinedAnnotation: name 'torch' is not defined. The annotation is changed to Any, which is semantically correct for pydantic's purposes (the field accepts arbitrary values) and avoids the forward-reference resolution failure. The runtime behaviour is unchanged — dtype can still hold str or torch.dtype values. As noted by @zucchini-nlp in the issue, this is the minimal fix.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fixes #45070.
PreTrainedConfig.dtypewas annotated asUnion[str, "torch.dtype"] | None. Sincetorchis only imported underTYPE_CHECKING, pydantic's schema builder encounters the"torch.dtype"forward reference at runtime and fails with:This breaks any user code that includes
PreTrainedConfigas a field in a pydantic model (e.g. vLLM speculator configs, config validators).The repro from the issue:
Fix
Change
dtype: Union[str, "torch.dtype"] | None = Nonetodtype: Any = Noneas suggested by @zucchini-nlp in the issue.Anyis semantically correct for pydantic's purposes — the field accepts string ortorch.dtypevalues at runtime. The forward reference is now only needed for static type checkers (which still see the correct type throughTYPE_CHECKING-gated imports in user code). The runtime behaviour is unchanged.Test Plan
MyModelConfig.model_rebuild(force=True)no longer raisesPydanticUndefinedAnnotationPreTrainedConfigcan be used as a pydantic field witharbitrary_types_allowed=True__post_init__still works (assignstorch.dtypeobjects at runtime)🤖 Generated with Claude Code