{"record":{"id":"ee2d0c63c7ee1d86","repo":"apache/beam","slug":"please-provide-both-model-class-and-model-uri-to-load-the","errorCode":null,"errorMessage":"Please provide both model class and model uri to load the model.Got params as model_uri={model_uri} and model_class={model_class}.","messagePattern":"Please provide both model class and model uri to load the model\\.Got params as model_uri=(.+?) and model_class=(.+?)\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/ml/inference/huggingface_inference.py","lineNumber":121,"sourceCode":"  TokenClassification = 'token-classification'\n  Translation = 'translation'\n  VideoClassification = 'video-classification'\n  VisualQuestionAnswering = 'visual-question-answering'\n  VQA = 'vqa'\n  ZeroShotAudioClassification = 'zero-shot-audio-classification'\n  ZeroShotClassification = 'zero-shot-classification'\n  ZeroShotImageClassification = 'zero-shot-image-classification'\n  ZeroShotObjectDetection = 'zero-shot-object-detection'\n  Translation_XX_to_YY = 'translation_XX_to_YY'\n\n\ndef _validate_constructor_args(model_uri, model_class):\n  message = (\n      \"Please provide both model class and model uri to load the model.\"\n      \"Got params as model_uri={model_uri} and \"\n      \"model_class={model_class}.\")\n  if not model_uri and not model_class:\n    raise RuntimeError(\n        message.format(model_uri=model_uri, model_class=model_class))\n  elif not model_uri:\n    raise RuntimeError(\n        message.format(model_uri=model_uri, model_class=model_class))\n  elif not model_class:\n    raise RuntimeError(\n        message.format(model_uri=model_uri, model_class=model_class))\n\n\ndef no_gpu_available_warning():\n  _LOGGER.warning(\n      \"HuggingFaceModelHandler specified a 'GPU' device, \"\n      \"but GPUs are not available. Switching to CPU.\")\n\n\ndef is_gpu_available_torch():\n  if torch.cuda.is_available():\n    return True","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/ml/inference/huggingface_inference.py#L103-L139","documentation":"HuggingFaceModelHandler's constructor validation raises RuntimeError when neither model_uri nor model_class is provided. Both are required together to load a model from a state dict (the message template is shared by all three branches of _validate_constructor_args, so it fires even though the message says 'both'). This branch handles the case where BOTH are missing.","triggerScenarios":"Calling HuggingFaceModelHandler() with no model_uri and no model_class; __init__ calls _validate_constructor_args which hits the first 'if not model_uri and not model_class' branch.","commonSituations":"Constructing the handler with only task/load_pipeline_args; copying example code and dropping the model-loading params; refactoring away the two coupled params.","solutions":["Pass both model_uri (path/URI of the state dict) and model_class (the transformers model class).","If loading a pipeline instead, use HuggingFacePipelineModelHandler with task or model.","Check that config/env values for model_uri and model_class are not empty/None at call time."],"exampleFix":"// before\nhandler = HuggingFaceModelHandler(load_pipeline_args={'device': 'cpu'})\n// after\nhandler = HuggingFaceModelHandler(model_uri='gs://bucket/model.pth', model_class=AutoModelForSequenceClassification)","handlingStrategy":"validation","validationCode":"def can_construct(model_uri, model_class):\n    return bool(model_uri) and bool(model_class)\nif not can_construct(uri, cls):\n    raise ValueError('HuggingFaceModelHandler needs both model_uri and model_class')","typeGuard":"def has_model_params(uri, cls) -> bool:\n    return isinstance(uri, str) and uri != '' and cls is not None","tryCatchPattern":"try:\n    handler = HuggingFaceModelHandler(model_uri=uri, model_class=cls)\nexcept RuntimeError as e:\n    if 'model_uri' in str(e) or 'model_class' in str(e):\n        handler = HuggingFacePipelineModelHandler(task=task)\n    else:\n        raise","preventionTips":["Validate both params at config-load time, before building the pipeline","Never pass model_uri/model_class from unchecked env vars","Prefer HuggingFacePipelineModelHandler when you only have a model id"],"tags":["python","apache-beam","machine-learning","huggingface","constructor-validation"],"backgroundTag":"missing-required-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}