AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

HuggingFacePipeline._Generate Rejects Valid Translation Tasks Due To Substring Check

The `_generate` method uses `self.pipeline.task in "translation"` instead of an equality or prefix check, causing valid transformers translation tasks like `translation_en_to_fr` to raise ValueError. It also incorrectly matches empty string.

highConfidence 95%Langchain-HuggingfaceAffected V1.2.2

Origin Analysis

The condition `elif self.pipeline.task in "translation":` performs a substring check against the string `"translation"`. Since `"translation_en_to_fr"` is not a substring of `"translation"`, it falls through to the else branch and raises ValueError. Additionally, any string that is a substring of `"translation"` (including the empty string) will erroneously enter the translation branch.
Run the provided minimal example with a MagicMock pipeline whose `task` attribute is set to `"translation_en_to_fr"`. Calling `_generate` raises ValueError: Got invalid task translation_en_to_fr.

Fixing Code Block

Edge Case Audit

This monkey patch overrides the complete `_generate` method. If the original method included additional behavior (e.g., batch processing, stop sequence handling, or newer task types), those features are not replicated here and may be lost. The extraction key for `image-text-to-text` is assumed to be `generated_text`; if the actual pipeline output uses a different key, that branch must be adjusted. Apply this patch only as a temporary fix until an official release includes the correction. To rollback, remove the assignment `HuggingFacePipeline._generate = _generate` and restart the Python process or reload the module.

Ecosystem Topology