fix: wrapped TypeAdpater in string literals (for now)#43836
Open
pragnyanramtha wants to merge 7 commits intohuggingface:mainfrom
Open
fix: wrapped TypeAdpater in string literals (for now)#43836pragnyanramtha wants to merge 7 commits intohuggingface:mainfrom
pragnyanramtha wants to merge 7 commits intohuggingface:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent transformers.cli.serve from crashing at import time when optional serving dependencies (notably pydantic) are not installed, aligning with Transformers’ lazy/optional dependency behavior for CLI features.
Changes:
- Wrapes the
TypeAdaptertype annotation in_validate_request()as a string literal to avoid eager evaluation causingNameErrorwhenpydanticis missing.
zucchini-nlp
reviewed
Feb 9, 2026
Member
zucchini-nlp
left a comment
There was a problem hiding this comment.
Pydantic is not a base requirement, so it's fine if it's not installed in user's env. Agreed about suggested change
I also see BaseModel and other safe-imports being used as type hints, can you check and fix them as well?
added 5 commits
February 10, 2026 20:23
…amtha/transformers into fix/qwen2-import-error
Author
|
hey @zucchini-nlp, implemented the changes you approved and fixed the other safe imports. |
This was referenced Apr 29, 2026
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.
Fixes #43824
what i think happened in #43824 is that waltwalt36 did not install the optional dependencies like pydantic, causing this issue.
According to the core architecture docs, transformers implements a lazy loading mechanism for imports (unless there are any exceptions). However, in this specific case, the type hint for TypeAdapter was being evaluated eagerly during the import of serve.py before the availability of pydantic could be properly asserted or gracefully handled. This resulted in a NameError for users who did not have pydantic installed, even though pydantic is an optional dependency for the serving functionality.
Error
for now i changed transformers.cli.serve where TypeAdapter was used in a type hint but fail-safe logic for missing dependencies caused NameError at import time if evaluated eagerly.
to resolve this, i could alter the definition of Serve in src/transformers/cli/serve.py as wrapped within an if is_pydantic_available(): block.
This ensures that the code referencing TypeAdapter is only parsed and executed if the pydantic library is actually available in the environment.
If pydantic is not available, a placeholder _ServeDummy class is used instead, which raises an ImportError when an attempt is made to instantiate it, providing a clear message to the user about the missing dependency.
Should i go ahead and implement this fix ?
@yonigozlan @molbap