Skip to content

Fix NameError in serving CLI due to conditional import asymmetry#45641

Closed
abhiprd200 wants to merge 6 commits intohuggingface:mainfrom
abhiprd200:fix/serving-conditional-imports
Closed

Fix NameError in serving CLI due to conditional import asymmetry#45641
abhiprd200 wants to merge 6 commits intohuggingface:mainfrom
abhiprd200:fix/serving-conditional-imports

Conversation

@abhiprd200
Copy link
Copy Markdown

What does this PR do?

Fixes a fatal crash in the transformers serve CLI when booting without the [serving] extras installed.

The Bug

Files in src/transformers/cli/serving/ (such as chat_completion.py and completion.py) conditionally import OpenAI types behind the is_serve_available() check. However, they unconditionally inherit from these types in the global scope (e.g., class TransformersCompletionCreateParamsStreaming(CompletionCreateParamsStreaming)).

When a user's environment fails is_serve_available(), the base classes are not imported, triggering an immediate NameError that cascades through the serving handlers and crashes the CLI.

The Fix

Enforced symmetry by providing TypedDict or dict-based dummy fallback classes in the except ImportError (or else) blocks. This allows the child classes to safely compile into memory globally, preventing the NameError and allowing the server to boot and gracefully handle requests or prompt the user for the correct dependencies.

Files Modified

  • chat_completion.py
  • completion.py
  • response.py
  • transcription.py

The serving module conditionally imports OpenAI types behind is_serve_available(), but unconditionally inherits from them in the global scope. This causes a fatal NameError when the server boots without the serving extras installed. This patch provides dummy fallback types to allow the CLI classes to initialize safely.
The serving module conditionally imports OpenAI types behind is_serve_available(), but unconditionally inherits from them in the global scope. This causes a fatal NameError when the server boots without the serving extras installed. This patch provides dummy fallback types to allow the CLI classes to initialize safely.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants