{"record":{"id":"3de0f2b0485ea8b7","repo":"apache/beam","slug":"unable-to-import-huggingfacepipelinemodelhandler-please","errorCode":null,"errorMessage":"Unable to import HuggingFacePipelineModelHandler. Please install transformers dependencies.","messagePattern":"Unable to import HuggingFacePipelineModelHandler\\. Please install transformers dependencies\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/yaml/yaml_ml.py","lineNumber":343,"sourceCode":"      model: The model name on Hugging Face hub or a path to a local directory.\n        If the model already defines the task, no need to specify the task.\n      preprocess: A python callable, defined either inline, or using a file,\n        that is invoked on the input row before sending to the model to be\n        loaded by this ModelHandler.\n      postprocess: A python callable, defined either inline, or using a file,\n        that is invoked on the PredictionResult output by the ModelHandler\n        before parsing into the output Beam Row.\n      device: The device to run the pipeline on (e.g., 'cpu', 'cuda', 'cuda:0').\n        Defaults to CPU.\n      inference_fn: The custom inference function to use.\n      load_pipeline_args: Extra arguments to pass to the Hugging Face pipeline\n        loader (e.g. `transformers.pipeline`).\n      **kwargs: Extra arguments to pass to the model handler.\n    \"\"\"\n    try:\n      from apache_beam.ml.inference.huggingface_inference import HuggingFacePipelineModelHandler\n    except ImportError:\n      raise ValueError(\n          'Unable to import HuggingFacePipelineModelHandler. Please '\n          'install transformers dependencies.')\n\n    kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}\n\n    inference_fn_obj = self.parse_processing_transform(\n        inference_fn, 'inference_fn') if inference_fn else None\n\n    handler_kwargs = {}\n    if inference_fn_obj:\n      handler_kwargs['inference_fn'] = inference_fn_obj\n\n    _handler = HuggingFacePipelineModelHandler(\n        task=task,\n        model=model,\n        device=device,\n        load_pipeline_args=load_pipeline_args,\n        **handler_kwargs,","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/yaml/yaml_ml.py#L325-L361","documentation":"Raised when HuggingFacePipelineModelHandler cannot be imported because the transformers (and related torch) dependencies are not installed. yaml_ml imports it lazily in __init__ and re-raises the ImportError as a ValueError with guidance.","triggerScenarios":"Configuring a YAML RunInference transform with a HuggingFace pipeline handler while apache_beam was installed without the transformers extra, e.g. missing `apache_beam[transformers]` or a standalone install of transformers/torch.","commonSituations":"Running Beam YAML pipelines in slim containers; forgetting to add transformers/torch to worker requirements; CPU-only environments where torch failed to install; Airflow/CI runners without ML deps.","solutions":["pip install apache_beam[transformers] (or pip install transformers torch)","Add transformers and torch to requirements/worker packages passed to the runner","Verify the import works in the actual execution environment, not just locally"],"exampleFix":"# before\npip install apache_beam\n# after\npip install 'apache_beam[transformers]'","handlingStrategy":"try-catch","validationCode":"try:\n    from apache_beam.ml.inference.huggingface_inference import HuggingFacePipelineModelHandler\nexcept ImportError:\n    raise SystemExit(\"Run: pip install 'apache_beam[transformers]'\")","typeGuard":"def has_hf_handler():\n    import importlib.util\n    return importlib.util.find_spec('apache_beam.ml.inference.huggingface_inference') is not None","tryCatchPattern":"try:\n    transform = RunInference(model_handler=hf_spec)\nexcept ValueError as e:\n    if 'transformers dependencies' in str(e):\n        install_transformers()\n    else:\n        raise","preventionTips":["Install apache_beam[transformers] wherever the pipeline runs","Add transformers and torch to worker requirements","Verify imports in the exact runner environment (Dataflow, Flink, etc.)"],"tags":["python","apache-beam","dependency","huggingface","transformers"],"backgroundTag":"missing-optional-dependency","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}