apache/beam · error · RuntimeError
Please specify either torch_script_model_path or…
Error message
Please specify either torch_script_model_path or (state_dict_path, model_class) to successfully load the model.
What it means
Raised by _validate_constructor_args in PytorchModelHandlerKeyedModel/PytorchModelHandler when the constructor arguments are inconsistent. Loading a PyTorch model requires either a TorchScript serialized model path alone, or a state_dict path paired with the model's class. Passing torch_script_model_path together with state_dict_path is ambiguous, so the handler refuses to construct.
Solutions
- Remove torch_script_model_path if you intend to load via (state_dict_path, model_class)
- Remove state_dict_path and model_class if you intend to load a TorchScript model via torch_script_model_path
- Ensure your config/kwargs builder emits exactly one of the two loading styles
Example fix
// before
handler = PytorchModelHandlerKeyedModel(
state_dict_path='gs://bucket/model.pt',
model_class=MyNet,
torch_script_path='gs://bucket/model_scripted.pt')
// after
handler = PytorchModelHandlerKeyedModel(
state_dict_path='gs://bucket/model.pt',
model_class=MyNet) Defensive patterns
Strategy: validation
Validate before calling
def check_pytorch_handler_kwargs(kwargs):
has_ts = bool(kwargs.get('torch_script_model_path'))
has_sd = bool(kwargs.get('state_dict_path') and kwargs.get('model_class'))
if has_ts and has_sd:
raise ValueError('Pass either torch_script_model_path or (state_dict_path, model_class), not both.')
if not has_ts and not has_sd:
raise ValueError('Provide torch_script_model_path or (state_dict_path, model_class).') Type guard
def is_valid_loading_style(kwargs: dict) -> bool:
has_ts = kwargs.get('torch_script_model_path') is not None
has_sd = kwargs.get('state_dict_path') is not None and kwargs.get('model_class') is not None
return has_ts ^ has_sd Try / catch
try:
handler = PytorchModelHandlerKeyedModel(**kwargs)
except RuntimeError as e:
if 'torch_script_model_path' in str(e):
kwargs.pop('torch_script_model_path')
handler = PytorchModelHandlerKeyedModel(**kwargs)
else:
raise Prevention
- Build handler kwargs from a single config source so only one loading style can be present
- Assert before construction that exactly one of torch_script_model_path / state_dict_path is set
- Keep TorchScript and state_dict examples separate; don't merge snippet kwargs
When it happens
Trigger: Constructing a ModelHandler (e.g. PytorchModelHandlerKeyedModel(...)) with both torch_script_model_path and state_dict_path set to non-None values.
Common situations: Migrating a handler from TorchScript loading to state_dict loading (or vice versa) and forgetting to remove the old path argument; building handler kwargs from a config that contains both keys; copy-pasting example code that mixes the two loading styles.
Related errors
- Callable create_model_fn must be passedwith…
- A has been supplied to the model handler, but the required…
- Batch does not have expected dtype
- Batch does not have expected shape
- Batch is not an instance of torch.Tensor
AI-assisted analysis of apache/beam@12126d8942 (2026-09-13).
Data as JSON: /api/errors/f263a8e7f4237815.
Report an issue: GitHub.
Appendix: source
Thrown at sdks/python/apache_beam/ml/inference/pytorch_inference.py:91
def _validate_constructor_args(
state_dict_path, model_class, torch_script_model_path):
message = (
"A {param1} has been supplied to the model "
"handler, but the required {param2} is missing. "
"Please provide the {param2} in order to "
"successfully load the {param1}.")
# state_dict_path and model_class are coupled with each other
# raise RuntimeError if user forgets to pass any one of them.
if state_dict_path and not model_class:
raise RuntimeError(
message.format(param1="state_dict_path", param2="model_class"))
if not state_dict_path and model_class:
raise RuntimeError(
message.format(param1="model_class", param2="state_dict_path"))
if torch_script_model_path and state_dict_path:
raise RuntimeError(
"Please specify either torch_script_model_path or "
"(state_dict_path, model_class) to successfully load the model.")
def _load_model(
model_class: Optional[Callable[..., torch.nn.Module]],
state_dict_path: Optional[str],
device: torch.device,
model_params: Optional[dict[str, Any]],
torch_script_model_path: Optional[str],
load_model_args: Optional[dict[str, Any]]):
if device == torch.device('cuda') and not _cuda_device_is_usable():
logging.warning(
"Model handler specified a 'GPU' device, but GPUs are not available. "
"Switching to CPU.")
device = torch.device('cpu')
try:View on GitHub (pinned to 12126d8942)