Skip to content

fix: wrapped TypeAdpater in string literals (for now)#43836

Open
pragnyanramtha wants to merge 7 commits intohuggingface:mainfrom
pragnyanramtha:fix/qwen2-import-error
Open

fix: wrapped TypeAdpater in string literals (for now)#43836
pragnyanramtha wants to merge 7 commits intohuggingface:mainfrom
pragnyanramtha:fix/qwen2-import-error

Conversation

@pragnyanramtha
Copy link
Copy Markdown

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

Traceback (most recent call last):
  File "/home/wbrione/.conda/envs/watt_ai/bin/transformers", line 3, in <module>
    from transformers.cli.transformers import main
  File "/home/wbrione/.conda/envs/watt_ai/lib/python3.10/site-packages/transformers/cli/transformers.py", line 22, in <module>
    from transformers.cli.serve import Serve
  File "/home/wbrione/.conda/envs/watt_ai/lib/python3.10/site-packages/transformers/cli/serve.py", line 360, in <module>
    class Serve:
  File "/home/wbrione/.conda/envs/watt_ai/lib/python3.10/site-packages/transformers/cli/serve.py", line 588, in Serve
    validator: TypeAdapter,
NameError: name 'TypeAdapter' is not defined

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 TypeAdapter type annotation in _validate_request() as a string literal to avoid eager evaluation causing NameError when pydantic is missing.

Comment thread src/transformers/cli/serve.py
Comment thread src/transformers/cli/serve.py
Copy link
Copy Markdown
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@pragnyanramtha
Copy link
Copy Markdown
Author

hey @zucchini-nlp, implemented the changes you approved and fixed the other safe imports.

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.

ImportError: cannot import name 'Qwen2_5_VLForConditionalGeneration' from 'transformers'

3 participants