github-actions[bot] commented on code in PR #41424:
URL: https://github.com/apache/beam/pull/41424#discussion_r4598330552


##########
sdks/python/apache_beam/yaml/yaml_ml.py:
##########
@@ -66,20 +78,251 @@ def _list_submodules(package):
 except ImportError:
   MLTransform = None  # type: ignore
 
+_OK = 'ok'
+_ERR = 'err'
+
+_TYPE_MAP: dict[str, Any] = {
+    'str': str,
+    'string': str,
+    'int': int,
+    'integer': int,
+    'float': float,
+    'double': float,
+    'number': float,
+    'bool': bool,
+    'boolean': bool,
+    'bytes': bytes,
+    'any': Any,
+}
+
+
+def _parse_type_spec(spec: Any) -> Any:
+  if spec is None:
+    return Any
+  if isinstance(spec, type) or spec is Any:
+    return spec
+  if isinstance(spec, str):
+    return _TYPE_MAP.get(spec.lower(), Any)
+  if isinstance(spec, Mapping):
+    fields = [(str(k), _parse_type_spec(v)) for k, v in spec.items()]
+    return RowTypeConstraint.from_fields(fields)
+  return Any
+
+
+def _to_python_scalar(val: Any) -> Any:
+  if hasattr(val, 'item') and callable(val.item):
+    try:
+      return val.item()
+    except Exception:
+      pass
+  if hasattr(val, 'tolist') and callable(val.tolist):
+    try:
+      return val.tolist()
+    except Exception:
+      pass
+  return val
+
+
+class _WeakRefModelBox:
+  """Weak-referenceable wrapper for models that do not natively support 
weakref."""
+  def __init__(self, model: Any):
+    self.model = model
+
+
+class _FunctionModelHandler(ModelHandler[Any, Any, Any]):
+  """ModelHandler backed by user-supplied load_model_fn and inference_fn."""
+  def __init__(
+      self,
+      inference_fn: Callable[..., Any],
+      load_model_fn: Optional[Callable[[], Any]] = None,
+      **kwargs):
+    super().__init__(**kwargs)
+    self._inference_fn = inference_fn
+    self._load_model_fn = load_model_fn
+
+  def load_model(self) -> Any:
+    if self._load_model_fn is not None:
+      return self._load_model_fn()
+    return None
+
+  def run_inference(
+      self,
+      batch: Sequence[Any],
+      model: Any,
+      inference_args: Optional[dict[str, Any]] = None) -> Iterable[Any]:
+    try:
+      return self._inference_fn(batch, model, inference_args)
+    except TypeError:

Review Comment:
   **🟠 High**: Catching `TypeError` around `self._inference_fn(...)` to probe 
its arity masks legitimate `TypeError` exceptions raised inside the user's 
`inference_fn` (for example, when a malformed element in `batch` has an 
unexpected type). If a 2- or 3-argument `inference_fn` raises `TypeError` 
internally:
   1. It is re-executed in the fallback block.
   2. The final fallback `[self._inference_fn(item) for item in batch]` calls 
the function with 1 argument, raising `TypeError: ... missing 1 required 
positional argument: 'model'`.
   3. With `error_handling` enabled, the per-element poison-pill retry and DLQ 
output record this signature `TypeError` instead of the actual data error.
   
   Inspect the callable's parameter count once via 
`inspect.signature(self._inference_fn)` (or only catch signature binding errors 
via `inspect.signature.bind`) before invoking `self._inference_fn`.



##########
sdks/python/apache_beam/yaml/yaml_ml.py:
##########
@@ -374,12 +626,467 @@ def inference_output_type(self):
     return Any
 
 
[email protected]_handler_type('PyTorch')
[email protected]_handler_type('PytorchModelHandlerTensor')
[email protected]_handler_type('PytorchModelHandlerKeyedTensor')
+class PytorchModelHandlerProvider(ModelHandlerProvider):
+  def __init__(
+      self,
+      state_dict_path: Optional[str] = None,
+      model_class: Optional[Union[dict[str, str], Callable[..., Any]]] = None,
+      model_params: Optional[dict[str, Any]] = None,
+      torch_script_model_path: Optional[str] = None,
+      device: str = 'CPU',
+      preprocess: Optional[dict[str, str]] = None,
+      postprocess: Optional[dict[str, str]] = None,
+      inference_fn: Optional[dict[str, str]] = None,
+      min_batch_size: Optional[int] = None,
+      max_batch_size: Optional[int] = None,
+      max_batch_duration_secs: Optional[int] = None,
+      large_model: bool = False,
+      keyed_tensor: bool = False,
+      input_field: Optional[Union[str, list[str]]] = None,
+      **kwargs):
+    if not torch_script_model_path and not (state_dict_path and model_class):
+      raise ValueError(
+          "PyTorch model handler requires either 'torch_script_model_path' or "
+          "both 'state_dict_path' and 'model_class'.")
+    clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+    model_class_obj = (
+        model_class
+        if callable(model_class) else self.parse_processing_transform(
+            model_class, 'model_class') if model_class else None)
+    inference_fn_obj = (
+        self.parse_processing_transform(inference_fn, 'inference_fn')
+        if inference_fn else None)
+
+    def _build_pytorch_handler():
+      try:
+        from apache_beam.ml.inference.pytorch_inference import 
PytorchModelHandlerKeyedTensor
+        from apache_beam.ml.inference.pytorch_inference import 
PytorchModelHandlerTensor
+      except ImportError:
+        raise ValueError(
+            'Unable to import PyTorch ModelHandler. Please install torch.')
+      handler_cls = (
+          PytorchModelHandlerKeyedTensor
+          if keyed_tensor else PytorchModelHandlerTensor)
+      extra: dict[str, Any] = {}
+      if inference_fn_obj is not None:
+        extra['inference_fn'] = inference_fn_obj
+      return handler_cls(
+          state_dict_path=state_dict_path,
+          model_class=model_class_obj,
+          model_params=model_params,
+          torch_script_model_path=torch_script_model_path,
+          device=device,
+          min_batch_size=min_batch_size,
+          max_batch_size=max_batch_size,
+          max_batch_duration_secs=max_batch_duration_secs,
+          large_model=large_model,
+          **extra,
+          **clean_kwargs)
+
+    _handler = _LazyYamlModelHandler(
+        _build_pytorch_handler,
+        min_batch_size=min_batch_size,
+        max_batch_size=max_batch_size,
+        max_batch_duration_secs=max_batch_duration_secs,
+        large_model=large_model)
+    super().__init__(
+        _handler,
+        preprocess,
+        postprocess,
+        input_field=input_field,
+        input_mode='torch')
+

Review Comment:
   **🟡 Medium**: Two issues with `PytorchModelHandlerKeyedTensor` support (and 
similarly `SklearnModelHandlerPandas` at lines 714/777):
   1. `PytorchModelHandlerProvider` is registered for 
`'PytorchModelHandlerKeyedTensor'`, `ModelHandlerProvider.create_handler` does 
not pass the registered `type` name, so `keyed_tensor` defaults to `False` even 
when `type: PytorchModelHandlerKeyedTensor` is explicitly configured.
   2. When `keyed_tensor=True` is used with `input_field`, `input_mode='torch'` 
in `_make_input_field_preprocess_fn` returns a single `torch.Tensor` rather 
than a `dict[str, torch.Tensor]`, causing 
`PytorchModelHandlerKeyedTensor.run_inference` to fail when calling 
`example.items()`.



##########
sdks/python/apache_beam/yaml/yaml_ml.py:
##########
@@ -374,12 +626,467 @@ def inference_output_type(self):
     return Any
 
 
[email protected]_handler_type('PyTorch')
[email protected]_handler_type('PytorchModelHandlerTensor')
[email protected]_handler_type('PytorchModelHandlerKeyedTensor')
+class PytorchModelHandlerProvider(ModelHandlerProvider):
+  def __init__(
+      self,
+      state_dict_path: Optional[str] = None,
+      model_class: Optional[Union[dict[str, str], Callable[..., Any]]] = None,
+      model_params: Optional[dict[str, Any]] = None,
+      torch_script_model_path: Optional[str] = None,
+      device: str = 'CPU',
+      preprocess: Optional[dict[str, str]] = None,
+      postprocess: Optional[dict[str, str]] = None,
+      inference_fn: Optional[dict[str, str]] = None,
+      min_batch_size: Optional[int] = None,
+      max_batch_size: Optional[int] = None,
+      max_batch_duration_secs: Optional[int] = None,
+      large_model: bool = False,
+      keyed_tensor: bool = False,
+      input_field: Optional[Union[str, list[str]]] = None,
+      **kwargs):
+    if not torch_script_model_path and not (state_dict_path and model_class):
+      raise ValueError(
+          "PyTorch model handler requires either 'torch_script_model_path' or "
+          "both 'state_dict_path' and 'model_class'.")
+    clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+    model_class_obj = (
+        model_class
+        if callable(model_class) else self.parse_processing_transform(
+            model_class, 'model_class') if model_class else None)
+    inference_fn_obj = (
+        self.parse_processing_transform(inference_fn, 'inference_fn')
+        if inference_fn else None)
+
+    def _build_pytorch_handler():
+      try:
+        from apache_beam.ml.inference.pytorch_inference import 
PytorchModelHandlerKeyedTensor
+        from apache_beam.ml.inference.pytorch_inference import 
PytorchModelHandlerTensor
+      except ImportError:
+        raise ValueError(
+            'Unable to import PyTorch ModelHandler. Please install torch.')
+      handler_cls = (
+          PytorchModelHandlerKeyedTensor
+          if keyed_tensor else PytorchModelHandlerTensor)
+      extra: dict[str, Any] = {}
+      if inference_fn_obj is not None:
+        extra['inference_fn'] = inference_fn_obj
+      return handler_cls(
+          state_dict_path=state_dict_path,
+          model_class=model_class_obj,
+          model_params=model_params,
+          torch_script_model_path=torch_script_model_path,
+          device=device,
+          min_batch_size=min_batch_size,
+          max_batch_size=max_batch_size,
+          max_batch_duration_secs=max_batch_duration_secs,
+          large_model=large_model,
+          **extra,
+          **clean_kwargs)
+
+    _handler = _LazyYamlModelHandler(
+        _build_pytorch_handler,
+        min_batch_size=min_batch_size,
+        max_batch_size=max_batch_size,
+        max_batch_duration_secs=max_batch_duration_secs,
+        large_model=large_model)
+    super().__init__(
+        _handler,
+        preprocess,
+        postprocess,
+        input_field=input_field,
+        input_mode='torch')
+
+  @staticmethod
+  def validate(config):
+    if not config or (
+        not config.get('torch_script_model_path') and
+        not (config.get('state_dict_path') and config.get('model_class'))):
+      raise ValueError(
+          "PyTorch model handler requires either 'torch_script_model_path' or "
+          "both 'state_dict_path' and 'model_class'.")
+
+
[email protected]_handler_type('Sklearn')
[email protected]_handler_type('SklearnModelHandlerNumpy')
[email protected]_handler_type('SklearnModelHandlerPandas')
+class SklearnModelHandlerProvider(ModelHandlerProvider):
+  def __init__(
+      self,
+      model_uri: str = '',
+      model_file_type: str = 'pickle',
+      input_data_type: str = 'numpy',
+      preprocess: Optional[dict[str, str]] = None,
+      postprocess: Optional[dict[str, str]] = None,
+      inference_fn: Optional[dict[str, str]] = None,
+      min_batch_size: Optional[int] = None,
+      max_batch_size: Optional[int] = None,
+      max_batch_duration_secs: Optional[int] = None,
+      large_model: bool = False,
+      input_field: Optional[Union[str, list[str]]] = None,
+      **kwargs):
+    if not model_uri:
+      raise ValueError("Sklearn model handler requires 'model_uri'.")
+    clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+    inference_fn_obj = (
+        self.parse_processing_transform(inference_fn, 'inference_fn')
+        if inference_fn else None)
+
+    def _build_sklearn_handler():
+      try:
+        from apache_beam.ml.inference.sklearn_inference import ModelFileType
+        from apache_beam.ml.inference.sklearn_inference import 
SklearnModelHandlerNumpy
+        from apache_beam.ml.inference.sklearn_inference import 
SklearnModelHandlerPandas
+      except ImportError:
+        raise ValueError(
+            'Unable to import Sklearn ModelHandler. Please install '
+            'scikit-learn.')
+      file_type = (
+          ModelFileType.JOBLIB
+          if str(model_file_type).lower() == 'joblib' else 
ModelFileType.PICKLE)
+      handler_cls = (
+          SklearnModelHandlerPandas if str(input_data_type).lower() == 'pandas'
+          else SklearnModelHandlerNumpy)
+      extra: dict[str, Any] = {}
+      if inference_fn_obj is not None:
+        extra['inference_fn'] = inference_fn_obj
+      return handler_cls(
+          model_uri=model_uri,
+          model_file_type=file_type,
+          min_batch_size=min_batch_size,
+          max_batch_size=max_batch_size,
+          max_batch_duration_secs=max_batch_duration_secs,
+          large_model=large_model,
+          **extra,
+          **clean_kwargs)
+
+    _handler = _LazyYamlModelHandler(
+        _build_sklearn_handler,
+        min_batch_size=min_batch_size,
+        max_batch_size=max_batch_size,
+        max_batch_duration_secs=max_batch_duration_secs,
+        large_model=large_model)
+    super().__init__(
+        _handler,
+        preprocess,
+        postprocess,
+        input_field=input_field,
+        input_mode='numpy')
+
+  @staticmethod
+  def validate(config):
+    if not config or not config.get('model_uri'):
+      raise ValueError("Sklearn model handler requires 'model_uri'.")
+
+
[email protected]_handler_type('Custom')
[email protected]_handler_type('CustomModelHandler')
+class CustomModelHandlerProvider(ModelHandlerProvider):
+  def __init__(
+      self,
+      callable: Optional[Union[str, Callable[..., Any]]] = None,  # pylint: 
disable=redefined-builtin
+      path: Optional[str] = None,
+      name: Optional[str] = None,
+      inference_fn: Optional[Union[dict[str, str], str, Callable[...,
+                                                                 Any]]] = None,
+      load_model_fn: Optional[Union[dict[str, str], str, Callable[[],
+                                                                  Any]]] = 
None,
+      model_kwargs: Optional[dict[str, Any]] = None,
+      preprocess: Optional[dict[str, str]] = None,
+      postprocess: Optional[dict[str, str]] = None,
+      min_batch_size: Optional[int] = None,
+      max_batch_size: Optional[int] = None,
+      max_batch_duration_secs: Optional[int] = None,
+      large_model: bool = False,
+      input_field: Optional[Union[str, list[str]]] = None,
+      **kwargs):
+    if not (callable or (path and name) or inference_fn):
+      raise ValueError(
+          "Custom model handler requires either 'callable', 'path' and 'name', 
"
+          "or 'inference_fn'.")
+    clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+    builtins_callable = __builtins__['callable'] if isinstance(
+        __builtins__, dict) else __builtins__.callable
+
+    def _resolve_fn(spec, label):
+      if spec is None:
+        return None
+      if builtins_callable(spec):
+        return spec
+      if isinstance(spec, str):
+        return python_callable.PythonCallableWithSource(spec)
+      if isinstance(spec, dict):
+        return self.parse_processing_transform(spec, label)
+      raise ValueError(f"Invalid {label} specification: {spec}")
+
+    factory_spec = ({
+        'callable': callable, 'path': path, 'name': name
+    } if (callable or path or name) else None)
+    factory_fn = (
+        callable
+        if builtins_callable(callable) else self.parse_processing_transform(
+            factory_spec, 'custom_model_handler') if factory_spec else None)
+    resolved_inference_fn = _resolve_fn(inference_fn, 'inference_fn')
+    resolved_load_fn = _resolve_fn(load_model_fn, 'load_model_fn')
+
+    def _build_custom_handler():
+      if factory_fn is not None:
+        init_kwargs = dict(model_kwargs or {})
+        init_kwargs.update(clean_kwargs)
+        return (
+            factory_fn if isinstance(factory_fn, ModelHandler) else factory_fn(
+                **init_kwargs))
+      return _FunctionModelHandler(
+          inference_fn=resolved_inference_fn,
+          load_model_fn=resolved_load_fn,
+          min_batch_size=min_batch_size,
+          max_batch_size=max_batch_size,
+          max_batch_duration_secs=max_batch_duration_secs,
+          large_model=large_model)
+
+    _handler = _LazyYamlModelHandler(
+        _build_custom_handler,
+        min_batch_size=min_batch_size,
+        max_batch_size=max_batch_size,
+        max_batch_duration_secs=max_batch_duration_secs,
+        large_model=large_model)
+    super().__init__(_handler, preprocess, postprocess, 
input_field=input_field)
+
+  @staticmethod
+  def validate(config):
+    if not config or not (config.get('callable') or
+                          (config.get('path') and config.get('name')) or
+                          config.get('inference_fn')):
+      raise ValueError(
+          "Custom model handler requires either 'callable', 'path' and 'name', 
"
+          "or 'inference_fn'.")
+
+
+class _SafePreprocessDoFn(beam.DoFn):
+  def __init__(
+      self,
+      preprocess_fn: Callable[[Any], Any],
+      dead_letter_tag: Optional[str] = None):
+    self._preprocess_fn = preprocess_fn
+    self._dead_letter_tag = dead_letter_tag
+
+  def process(self, row: Any, *args, **kwargs):
+    try:
+      yield (row, self._preprocess_fn(row))
+    except Exception as exn:
+      if self._dead_letter_tag:
+        yield pvalue.TaggedOutput(
+            self._dead_letter_tag,
+            (
+                row,
+                (
+                    type(exn),
+                    repr(exn),
+                    traceback.format_exception(*sys.exc_info()))))
+      else:
+        raise
+
+
+class _PostprocessAndAttachDoFn(beam.DoFn):
+  def __init__(
+      self,
+      postprocess_fn: Callable[[Any], Any],
+      output_field: str,
+      flatten_predictions: bool = False,
+      unwrap_prediction_result: bool = False,
+      dead_letter_tag: Optional[str] = None):
+    self._postprocess_fn = postprocess_fn
+    self._output_field = output_field
+    self._flatten_predictions = flatten_predictions
+    self._unwrap_prediction_result = unwrap_prediction_result
+    self._dead_letter_tag = dead_letter_tag
+
+  @staticmethod
+  def _unwrap(pred: Any) -> Any:
+    val = pred.inference if isinstance(pred, PredictionResult) else pred
+    if (isinstance(val, (list, tuple)) and len(val) == 1 and
+        (isinstance(val[0], Mapping) or hasattr(val[0], '_asdict'))):
+      val = val[0]
+    return val
+
+  def process(self, element: tuple[Any, Any], *args, **kwargs):
+    row, raw_outcome = element
+    if self._dead_letter_tag is not None:
+      status, payload = raw_outcome
+      if status == _ERR:
+        yield pvalue.TaggedOutput(self._dead_letter_tag, (row, payload))
+        return
+      raw_pred = payload
+    else:
+      raw_pred = raw_outcome
+
+    try:
+      processed = self._postprocess_fn(raw_pred)
+      row_dict = dict(row._asdict()) if hasattr(row, '_asdict') else dict(row)
+
+      if self._flatten_predictions:
+        val = self._unwrap(processed)
+        if hasattr(val, '_asdict'):
+          val = val._asdict()
+        if isinstance(val, Mapping):
+          for k, v in val.items():
+            row_dict[f"{self._output_field}_{k}"] = _to_python_scalar(v)
+        else:
+          row_dict[self._output_field] = _to_python_scalar(val)
+      else:
+        if self._unwrap_prediction_result:
+          val = self._unwrap(processed)
+          if isinstance(val, Mapping):
+            val = beam.Row(**{k: _to_python_scalar(v) for k, v in val.items()})
+          else:
+            val = _to_python_scalar(val)
+        else:
+          val = processed
+        row_dict[self._output_field] = val
+
+      yield beam.Row(**row_dict)
+    except Exception as exn:
+      if self._dead_letter_tag:
+        yield pvalue.TaggedOutput(
+            self._dead_letter_tag,
+            (
+                row,
+                (
+                    type(exn),
+                    repr(exn),
+                    traceback.format_exception(*sys.exc_info()))))
+      else:
+        raise
+
+
+class _YamlRunInferenceTransform(beam.PTransform):
+  def __init__(
+      self,
+      model_handler_provider: ModelHandlerProvider,
+      output_field: str,
+      inference_args: Optional[dict[str, Any]] = None,
+      flatten_predictions: bool = False,
+      output_type: Optional[Any] = None,
+      declarative_mode: bool = False):
+    self._provider = model_handler_provider
+    self._output_field = output_field
+    self._inference_args = inference_args
+    self._flatten_predictions = flatten_predictions
+    self._output_type = output_type
+    self._declarative_mode = declarative_mode
+    self._exception_handling_args: Optional[dict[str, Any]] = None
+
+  def with_exception_handling(self, **kwargs):
+    self._exception_handling_args = kwargs
+    return self
+
+  def _output_fields_for_inference(self) -> list[tuple[str, Any]]:
+    if self._flatten_predictions:
+      if isinstance(self._output_type, Mapping):
+        return [(f"{self._output_field}_{k}", _parse_type_spec(v))
+                for k, v in self._output_type.items()]
+      return [
+          (f"{self._output_field}_label", str),
+          (f"{self._output_field}_score", float),
+      ]
+    if self._output_type is not None:
+      return [(self._output_field, _parse_type_spec(self._output_type))]
+    return [(self._output_field, self._provider.inference_output_type())]
+
+  def infer_output_type(self, input_type):
+    try:
+      if isinstance(input_type, RowTypeConstraint):
+        base_fields = list(input_type._fields)
+      else:
+        base_fields = list(named_fields_from_element_type(input_type))
+    except Exception:
+      return typehints.Any
+    if not base_fields:
+      return typehints.Any
+    return RowTypeConstraint.from_fields(
+        base_fields + self._output_fields_for_inference())
+

Review Comment:
   **🟢 Low**: If `output_field` (or one of the flattened output fields) has the 
same name as an existing column in `input_type`, `_PostprocessAndAttachDoFn` 
overwrites that key in `row_dict`, whereas `base_fields + 
self._output_fields_for_inference()` produces duplicate field names in 
`RowTypeConstraint.from_fields(...)`. Filter out any field names present in the 
new output fields from `base_fields` before constructing the 
`RowTypeConstraint`.



##########
sdks/python/apache_beam/yaml/yaml_ml.py:
##########
@@ -374,12 +626,467 @@ def inference_output_type(self):
     return Any
 
 
[email protected]_handler_type('PyTorch')
[email protected]_handler_type('PytorchModelHandlerTensor')
[email protected]_handler_type('PytorchModelHandlerKeyedTensor')
+class PytorchModelHandlerProvider(ModelHandlerProvider):
+  def __init__(
+      self,
+      state_dict_path: Optional[str] = None,
+      model_class: Optional[Union[dict[str, str], Callable[..., Any]]] = None,
+      model_params: Optional[dict[str, Any]] = None,
+      torch_script_model_path: Optional[str] = None,
+      device: str = 'CPU',
+      preprocess: Optional[dict[str, str]] = None,
+      postprocess: Optional[dict[str, str]] = None,
+      inference_fn: Optional[dict[str, str]] = None,
+      min_batch_size: Optional[int] = None,
+      max_batch_size: Optional[int] = None,
+      max_batch_duration_secs: Optional[int] = None,
+      large_model: bool = False,
+      keyed_tensor: bool = False,
+      input_field: Optional[Union[str, list[str]]] = None,
+      **kwargs):
+    if not torch_script_model_path and not (state_dict_path and model_class):
+      raise ValueError(
+          "PyTorch model handler requires either 'torch_script_model_path' or "
+          "both 'state_dict_path' and 'model_class'.")
+    clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+    model_class_obj = (
+        model_class
+        if callable(model_class) else self.parse_processing_transform(
+            model_class, 'model_class') if model_class else None)
+    inference_fn_obj = (
+        self.parse_processing_transform(inference_fn, 'inference_fn')
+        if inference_fn else None)
+
+    def _build_pytorch_handler():
+      try:
+        from apache_beam.ml.inference.pytorch_inference import 
PytorchModelHandlerKeyedTensor
+        from apache_beam.ml.inference.pytorch_inference import 
PytorchModelHandlerTensor
+      except ImportError:
+        raise ValueError(
+            'Unable to import PyTorch ModelHandler. Please install torch.')
+      handler_cls = (
+          PytorchModelHandlerKeyedTensor
+          if keyed_tensor else PytorchModelHandlerTensor)
+      extra: dict[str, Any] = {}
+      if inference_fn_obj is not None:
+        extra['inference_fn'] = inference_fn_obj
+      return handler_cls(
+          state_dict_path=state_dict_path,
+          model_class=model_class_obj,
+          model_params=model_params,
+          torch_script_model_path=torch_script_model_path,
+          device=device,
+          min_batch_size=min_batch_size,
+          max_batch_size=max_batch_size,
+          max_batch_duration_secs=max_batch_duration_secs,
+          large_model=large_model,
+          **extra,
+          **clean_kwargs)
+
+    _handler = _LazyYamlModelHandler(
+        _build_pytorch_handler,
+        min_batch_size=min_batch_size,
+        max_batch_size=max_batch_size,
+        max_batch_duration_secs=max_batch_duration_secs,
+        large_model=large_model)
+    super().__init__(
+        _handler,
+        preprocess,
+        postprocess,
+        input_field=input_field,
+        input_mode='torch')
+
+  @staticmethod
+  def validate(config):
+    if not config or (
+        not config.get('torch_script_model_path') and
+        not (config.get('state_dict_path') and config.get('model_class'))):
+      raise ValueError(
+          "PyTorch model handler requires either 'torch_script_model_path' or "
+          "both 'state_dict_path' and 'model_class'.")
+
+
[email protected]_handler_type('Sklearn')
[email protected]_handler_type('SklearnModelHandlerNumpy')
[email protected]_handler_type('SklearnModelHandlerPandas')
+class SklearnModelHandlerProvider(ModelHandlerProvider):
+  def __init__(
+      self,
+      model_uri: str = '',
+      model_file_type: str = 'pickle',
+      input_data_type: str = 'numpy',
+      preprocess: Optional[dict[str, str]] = None,
+      postprocess: Optional[dict[str, str]] = None,
+      inference_fn: Optional[dict[str, str]] = None,
+      min_batch_size: Optional[int] = None,
+      max_batch_size: Optional[int] = None,
+      max_batch_duration_secs: Optional[int] = None,
+      large_model: bool = False,
+      input_field: Optional[Union[str, list[str]]] = None,
+      **kwargs):
+    if not model_uri:
+      raise ValueError("Sklearn model handler requires 'model_uri'.")
+    clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+    inference_fn_obj = (
+        self.parse_processing_transform(inference_fn, 'inference_fn')
+        if inference_fn else None)
+
+    def _build_sklearn_handler():
+      try:
+        from apache_beam.ml.inference.sklearn_inference import ModelFileType
+        from apache_beam.ml.inference.sklearn_inference import 
SklearnModelHandlerNumpy
+        from apache_beam.ml.inference.sklearn_inference import 
SklearnModelHandlerPandas
+      except ImportError:
+        raise ValueError(
+            'Unable to import Sklearn ModelHandler. Please install '
+            'scikit-learn.')
+      file_type = (
+          ModelFileType.JOBLIB
+          if str(model_file_type).lower() == 'joblib' else 
ModelFileType.PICKLE)
+      handler_cls = (
+          SklearnModelHandlerPandas if str(input_data_type).lower() == 'pandas'
+          else SklearnModelHandlerNumpy)
+      extra: dict[str, Any] = {}
+      if inference_fn_obj is not None:
+        extra['inference_fn'] = inference_fn_obj
+      return handler_cls(
+          model_uri=model_uri,
+          model_file_type=file_type,
+          min_batch_size=min_batch_size,
+          max_batch_size=max_batch_size,
+          max_batch_duration_secs=max_batch_duration_secs,
+          large_model=large_model,
+          **extra,
+          **clean_kwargs)
+
+    _handler = _LazyYamlModelHandler(
+        _build_sklearn_handler,
+        min_batch_size=min_batch_size,
+        max_batch_size=max_batch_size,
+        max_batch_duration_secs=max_batch_duration_secs,
+        large_model=large_model)
+    super().__init__(
+        _handler,
+        preprocess,
+        postprocess,
+        input_field=input_field,
+        input_mode='numpy')
+
+  @staticmethod
+  def validate(config):
+    if not config or not config.get('model_uri'):
+      raise ValueError("Sklearn model handler requires 'model_uri'.")
+
+
[email protected]_handler_type('Custom')
[email protected]_handler_type('CustomModelHandler')
+class CustomModelHandlerProvider(ModelHandlerProvider):
+  def __init__(
+      self,
+      callable: Optional[Union[str, Callable[..., Any]]] = None,  # pylint: 
disable=redefined-builtin
+      path: Optional[str] = None,
+      name: Optional[str] = None,
+      inference_fn: Optional[Union[dict[str, str], str, Callable[...,
+                                                                 Any]]] = None,
+      load_model_fn: Optional[Union[dict[str, str], str, Callable[[],
+                                                                  Any]]] = 
None,
+      model_kwargs: Optional[dict[str, Any]] = None,
+      preprocess: Optional[dict[str, str]] = None,
+      postprocess: Optional[dict[str, str]] = None,
+      min_batch_size: Optional[int] = None,
+      max_batch_size: Optional[int] = None,
+      max_batch_duration_secs: Optional[int] = None,
+      large_model: bool = False,
+      input_field: Optional[Union[str, list[str]]] = None,
+      **kwargs):
+    if not (callable or (path and name) or inference_fn):
+      raise ValueError(
+          "Custom model handler requires either 'callable', 'path' and 'name', 
"
+          "or 'inference_fn'.")
+    clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+    builtins_callable = __builtins__['callable'] if isinstance(
+        __builtins__, dict) else __builtins__.callable
+
+    def _resolve_fn(spec, label):
+      if spec is None:
+        return None
+      if builtins_callable(spec):
+        return spec
+      if isinstance(spec, str):
+        return python_callable.PythonCallableWithSource(spec)
+      if isinstance(spec, dict):
+        return self.parse_processing_transform(spec, label)
+      raise ValueError(f"Invalid {label} specification: {spec}")
+
+    factory_spec = ({
+        'callable': callable, 'path': path, 'name': name
+    } if (callable or path or name) else None)
+    factory_fn = (
+        callable
+        if builtins_callable(callable) else self.parse_processing_transform(
+            factory_spec, 'custom_model_handler') if factory_spec else None)
+    resolved_inference_fn = _resolve_fn(inference_fn, 'inference_fn')
+    resolved_load_fn = _resolve_fn(load_model_fn, 'load_model_fn')
+
+    def _build_custom_handler():
+      if factory_fn is not None:
+        init_kwargs = dict(model_kwargs or {})
+        init_kwargs.update(clean_kwargs)
+        return (
+            factory_fn if isinstance(factory_fn, ModelHandler) else factory_fn(
+                **init_kwargs))
+      return _FunctionModelHandler(
+          inference_fn=resolved_inference_fn,
+          load_model_fn=resolved_load_fn,
+          min_batch_size=min_batch_size,
+          max_batch_size=max_batch_size,
+          max_batch_duration_secs=max_batch_duration_secs,
+          large_model=large_model)
+
+    _handler = _LazyYamlModelHandler(
+        _build_custom_handler,
+        min_batch_size=min_batch_size,
+        max_batch_size=max_batch_size,
+        max_batch_duration_secs=max_batch_duration_secs,
+        large_model=large_model)
+    super().__init__(_handler, preprocess, postprocess, 
input_field=input_field)
+
+  @staticmethod
+  def validate(config):
+    if not config or not (config.get('callable') or
+                          (config.get('path') and config.get('name')) or
+                          config.get('inference_fn')):
+      raise ValueError(
+          "Custom model handler requires either 'callable', 'path' and 'name', 
"
+          "or 'inference_fn'.")
+
+
+class _SafePreprocessDoFn(beam.DoFn):
+  def __init__(
+      self,
+      preprocess_fn: Callable[[Any], Any],
+      dead_letter_tag: Optional[str] = None):
+    self._preprocess_fn = preprocess_fn
+    self._dead_letter_tag = dead_letter_tag
+
+  def process(self, row: Any, *args, **kwargs):
+    try:
+      yield (row, self._preprocess_fn(row))
+    except Exception as exn:
+      if self._dead_letter_tag:
+        yield pvalue.TaggedOutput(
+            self._dead_letter_tag,
+            (
+                row,
+                (
+                    type(exn),
+                    repr(exn),
+                    traceback.format_exception(*sys.exc_info()))))
+      else:
+        raise
+
+
+class _PostprocessAndAttachDoFn(beam.DoFn):
+  def __init__(
+      self,
+      postprocess_fn: Callable[[Any], Any],
+      output_field: str,
+      flatten_predictions: bool = False,
+      unwrap_prediction_result: bool = False,
+      dead_letter_tag: Optional[str] = None):
+    self._postprocess_fn = postprocess_fn
+    self._output_field = output_field
+    self._flatten_predictions = flatten_predictions
+    self._unwrap_prediction_result = unwrap_prediction_result
+    self._dead_letter_tag = dead_letter_tag
+
+  @staticmethod
+  def _unwrap(pred: Any) -> Any:
+    val = pred.inference if isinstance(pred, PredictionResult) else pred
+    if (isinstance(val, (list, tuple)) and len(val) == 1 and
+        (isinstance(val[0], Mapping) or hasattr(val[0], '_asdict'))):
+      val = val[0]
+    return val
+
+  def process(self, element: tuple[Any, Any], *args, **kwargs):
+    row, raw_outcome = element
+    if self._dead_letter_tag is not None:
+      status, payload = raw_outcome
+      if status == _ERR:
+        yield pvalue.TaggedOutput(self._dead_letter_tag, (row, payload))
+        return
+      raw_pred = payload
+    else:
+      raw_pred = raw_outcome
+
+    try:
+      processed = self._postprocess_fn(raw_pred)
+      row_dict = dict(row._asdict()) if hasattr(row, '_asdict') else dict(row)
+
+      if self._flatten_predictions:
+        val = self._unwrap(processed)
+        if hasattr(val, '_asdict'):
+          val = val._asdict()
+        if isinstance(val, Mapping):
+          for k, v in val.items():
+            row_dict[f"{self._output_field}_{k}"] = _to_python_scalar(v)
+        else:
+          row_dict[self._output_field] = _to_python_scalar(val)
+      else:
+        if self._unwrap_prediction_result:
+          val = self._unwrap(processed)
+          if isinstance(val, Mapping):
+            val = beam.Row(**{k: _to_python_scalar(v) for k, v in val.items()})
+          else:
+            val = _to_python_scalar(val)
+        else:
+          val = processed
+        row_dict[self._output_field] = val
+
+      yield beam.Row(**row_dict)
+    except Exception as exn:
+      if self._dead_letter_tag:
+        yield pvalue.TaggedOutput(
+            self._dead_letter_tag,
+            (
+                row,
+                (
+                    type(exn),
+                    repr(exn),
+                    traceback.format_exception(*sys.exc_info()))))
+      else:
+        raise
+
+
+class _YamlRunInferenceTransform(beam.PTransform):
+  def __init__(
+      self,
+      model_handler_provider: ModelHandlerProvider,
+      output_field: str,
+      inference_args: Optional[dict[str, Any]] = None,
+      flatten_predictions: bool = False,
+      output_type: Optional[Any] = None,
+      declarative_mode: bool = False):
+    self._provider = model_handler_provider
+    self._output_field = output_field
+    self._inference_args = inference_args
+    self._flatten_predictions = flatten_predictions
+    self._output_type = output_type
+    self._declarative_mode = declarative_mode
+    self._exception_handling_args: Optional[dict[str, Any]] = None
+
+  def with_exception_handling(self, **kwargs):
+    self._exception_handling_args = kwargs
+    return self
+
+  def _output_fields_for_inference(self) -> list[tuple[str, Any]]:
+    if self._flatten_predictions:
+      if isinstance(self._output_type, Mapping):
+        return [(f"{self._output_field}_{k}", _parse_type_spec(v))
+                for k, v in self._output_type.items()]
+      return [
+          (f"{self._output_field}_label", str),
+          (f"{self._output_field}_score", float),
+      ]
+    if self._output_type is not None:
+      return [(self._output_field, _parse_type_spec(self._output_type))]
+    return [(self._output_field, self._provider.inference_output_type())]
+
+  def infer_output_type(self, input_type):

Review Comment:
   **🟠 High**: In declarative mode (`provider: VertexAI`) without 
`flatten_predictions` or an explicit `output_type`, `unwrap_pred` is `True` 
(line 1019), so `_PostprocessAndAttachDoFn` unwraps `PredictionResult` to 
`pred.inference`. However, `_output_fields_for_inference()` still returns 
`self._provider.inference_output_type()`, which for 
`VertexAIModelHandlerJSONProvider` is `RowTypeConstraint(example=Any, 
inference=Any, model_id=Optional[str])`. This causes the declared PCollection 
schema on `main_out` to mismatch the actual unwrapped values emitted at runtime.
   
   ```suggestion
       return [(self._output_field, Any if (self._declarative_mode and not 
self._provider._has_custom_postprocess) else 
self._provider.inference_output_type())]
   ```



##########
sdks/python/apache_beam/yaml/yaml_ml.py:
##########
@@ -374,12 +626,467 @@ def inference_output_type(self):
     return Any
 
 
[email protected]_handler_type('PyTorch')
[email protected]_handler_type('PytorchModelHandlerTensor')
[email protected]_handler_type('PytorchModelHandlerKeyedTensor')
+class PytorchModelHandlerProvider(ModelHandlerProvider):
+  def __init__(
+      self,
+      state_dict_path: Optional[str] = None,
+      model_class: Optional[Union[dict[str, str], Callable[..., Any]]] = None,
+      model_params: Optional[dict[str, Any]] = None,
+      torch_script_model_path: Optional[str] = None,
+      device: str = 'CPU',
+      preprocess: Optional[dict[str, str]] = None,
+      postprocess: Optional[dict[str, str]] = None,
+      inference_fn: Optional[dict[str, str]] = None,
+      min_batch_size: Optional[int] = None,
+      max_batch_size: Optional[int] = None,
+      max_batch_duration_secs: Optional[int] = None,
+      large_model: bool = False,
+      keyed_tensor: bool = False,
+      input_field: Optional[Union[str, list[str]]] = None,
+      **kwargs):
+    if not torch_script_model_path and not (state_dict_path and model_class):
+      raise ValueError(
+          "PyTorch model handler requires either 'torch_script_model_path' or "
+          "both 'state_dict_path' and 'model_class'.")
+    clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+    model_class_obj = (
+        model_class
+        if callable(model_class) else self.parse_processing_transform(
+            model_class, 'model_class') if model_class else None)
+    inference_fn_obj = (
+        self.parse_processing_transform(inference_fn, 'inference_fn')
+        if inference_fn else None)
+
+    def _build_pytorch_handler():
+      try:
+        from apache_beam.ml.inference.pytorch_inference import 
PytorchModelHandlerKeyedTensor
+        from apache_beam.ml.inference.pytorch_inference import 
PytorchModelHandlerTensor
+      except ImportError:
+        raise ValueError(
+            'Unable to import PyTorch ModelHandler. Please install torch.')
+      handler_cls = (
+          PytorchModelHandlerKeyedTensor
+          if keyed_tensor else PytorchModelHandlerTensor)
+      extra: dict[str, Any] = {}
+      if inference_fn_obj is not None:
+        extra['inference_fn'] = inference_fn_obj
+      return handler_cls(
+          state_dict_path=state_dict_path,
+          model_class=model_class_obj,
+          model_params=model_params,
+          torch_script_model_path=torch_script_model_path,
+          device=device,
+          min_batch_size=min_batch_size,
+          max_batch_size=max_batch_size,
+          max_batch_duration_secs=max_batch_duration_secs,
+          large_model=large_model,
+          **extra,
+          **clean_kwargs)
+
+    _handler = _LazyYamlModelHandler(
+        _build_pytorch_handler,
+        min_batch_size=min_batch_size,
+        max_batch_size=max_batch_size,
+        max_batch_duration_secs=max_batch_duration_secs,
+        large_model=large_model)
+    super().__init__(
+        _handler,
+        preprocess,
+        postprocess,
+        input_field=input_field,
+        input_mode='torch')
+
+  @staticmethod
+  def validate(config):
+    if not config or (
+        not config.get('torch_script_model_path') and
+        not (config.get('state_dict_path') and config.get('model_class'))):
+      raise ValueError(
+          "PyTorch model handler requires either 'torch_script_model_path' or "
+          "both 'state_dict_path' and 'model_class'.")
+
+
[email protected]_handler_type('Sklearn')
[email protected]_handler_type('SklearnModelHandlerNumpy')
[email protected]_handler_type('SklearnModelHandlerPandas')
+class SklearnModelHandlerProvider(ModelHandlerProvider):
+  def __init__(
+      self,
+      model_uri: str = '',
+      model_file_type: str = 'pickle',
+      input_data_type: str = 'numpy',
+      preprocess: Optional[dict[str, str]] = None,
+      postprocess: Optional[dict[str, str]] = None,
+      inference_fn: Optional[dict[str, str]] = None,
+      min_batch_size: Optional[int] = None,
+      max_batch_size: Optional[int] = None,
+      max_batch_duration_secs: Optional[int] = None,
+      large_model: bool = False,
+      input_field: Optional[Union[str, list[str]]] = None,
+      **kwargs):
+    if not model_uri:
+      raise ValueError("Sklearn model handler requires 'model_uri'.")
+    clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+    inference_fn_obj = (
+        self.parse_processing_transform(inference_fn, 'inference_fn')
+        if inference_fn else None)
+
+    def _build_sklearn_handler():
+      try:
+        from apache_beam.ml.inference.sklearn_inference import ModelFileType
+        from apache_beam.ml.inference.sklearn_inference import 
SklearnModelHandlerNumpy
+        from apache_beam.ml.inference.sklearn_inference import 
SklearnModelHandlerPandas
+      except ImportError:
+        raise ValueError(
+            'Unable to import Sklearn ModelHandler. Please install '
+            'scikit-learn.')
+      file_type = (
+          ModelFileType.JOBLIB
+          if str(model_file_type).lower() == 'joblib' else 
ModelFileType.PICKLE)
+      handler_cls = (
+          SklearnModelHandlerPandas if str(input_data_type).lower() == 'pandas'
+          else SklearnModelHandlerNumpy)
+      extra: dict[str, Any] = {}
+      if inference_fn_obj is not None:
+        extra['inference_fn'] = inference_fn_obj
+      return handler_cls(
+          model_uri=model_uri,
+          model_file_type=file_type,
+          min_batch_size=min_batch_size,
+          max_batch_size=max_batch_size,
+          max_batch_duration_secs=max_batch_duration_secs,
+          large_model=large_model,
+          **extra,
+          **clean_kwargs)
+
+    _handler = _LazyYamlModelHandler(
+        _build_sklearn_handler,
+        min_batch_size=min_batch_size,
+        max_batch_size=max_batch_size,
+        max_batch_duration_secs=max_batch_duration_secs,
+        large_model=large_model)
+    super().__init__(
+        _handler,
+        preprocess,
+        postprocess,
+        input_field=input_field,
+        input_mode='numpy')
+
+  @staticmethod
+  def validate(config):
+    if not config or not config.get('model_uri'):
+      raise ValueError("Sklearn model handler requires 'model_uri'.")
+
+
[email protected]_handler_type('Custom')
[email protected]_handler_type('CustomModelHandler')
+class CustomModelHandlerProvider(ModelHandlerProvider):
+  def __init__(
+      self,
+      callable: Optional[Union[str, Callable[..., Any]]] = None,  # pylint: 
disable=redefined-builtin
+      path: Optional[str] = None,
+      name: Optional[str] = None,
+      inference_fn: Optional[Union[dict[str, str], str, Callable[...,
+                                                                 Any]]] = None,
+      load_model_fn: Optional[Union[dict[str, str], str, Callable[[],
+                                                                  Any]]] = 
None,
+      model_kwargs: Optional[dict[str, Any]] = None,
+      preprocess: Optional[dict[str, str]] = None,
+      postprocess: Optional[dict[str, str]] = None,
+      min_batch_size: Optional[int] = None,
+      max_batch_size: Optional[int] = None,
+      max_batch_duration_secs: Optional[int] = None,
+      large_model: bool = False,
+      input_field: Optional[Union[str, list[str]]] = None,
+      **kwargs):
+    if not (callable or (path and name) or inference_fn):
+      raise ValueError(
+          "Custom model handler requires either 'callable', 'path' and 'name', 
"
+          "or 'inference_fn'.")
+    clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+    builtins_callable = __builtins__['callable'] if isinstance(
+        __builtins__, dict) else __builtins__.callable
+
+    def _resolve_fn(spec, label):
+      if spec is None:
+        return None
+      if builtins_callable(spec):
+        return spec
+      if isinstance(spec, str):
+        return python_callable.PythonCallableWithSource(spec)
+      if isinstance(spec, dict):
+        return self.parse_processing_transform(spec, label)
+      raise ValueError(f"Invalid {label} specification: {spec}")
+
+    factory_spec = ({
+        'callable': callable, 'path': path, 'name': name
+    } if (callable or path or name) else None)
+    factory_fn = (
+        callable
+        if builtins_callable(callable) else self.parse_processing_transform(
+            factory_spec, 'custom_model_handler') if factory_spec else None)
+    resolved_inference_fn = _resolve_fn(inference_fn, 'inference_fn')
+    resolved_load_fn = _resolve_fn(load_model_fn, 'load_model_fn')
+
+    def _build_custom_handler():
+      if factory_fn is not None:
+        init_kwargs = dict(model_kwargs or {})
+        init_kwargs.update(clean_kwargs)
+        return (
+            factory_fn if isinstance(factory_fn, ModelHandler) else factory_fn(
+                **init_kwargs))
+      return _FunctionModelHandler(
+          inference_fn=resolved_inference_fn,
+          load_model_fn=resolved_load_fn,
+          min_batch_size=min_batch_size,
+          max_batch_size=max_batch_size,
+          max_batch_duration_secs=max_batch_duration_secs,
+          large_model=large_model)
+
+    _handler = _LazyYamlModelHandler(
+        _build_custom_handler,
+        min_batch_size=min_batch_size,
+        max_batch_size=max_batch_size,
+        max_batch_duration_secs=max_batch_duration_secs,
+        large_model=large_model)
+    super().__init__(_handler, preprocess, postprocess, 
input_field=input_field)
+
+  @staticmethod
+  def validate(config):
+    if not config or not (config.get('callable') or
+                          (config.get('path') and config.get('name')) or
+                          config.get('inference_fn')):
+      raise ValueError(
+          "Custom model handler requires either 'callable', 'path' and 'name', 
"
+          "or 'inference_fn'.")
+
+
+class _SafePreprocessDoFn(beam.DoFn):
+  def __init__(
+      self,
+      preprocess_fn: Callable[[Any], Any],
+      dead_letter_tag: Optional[str] = None):
+    self._preprocess_fn = preprocess_fn
+    self._dead_letter_tag = dead_letter_tag
+
+  def process(self, row: Any, *args, **kwargs):
+    try:
+      yield (row, self._preprocess_fn(row))
+    except Exception as exn:
+      if self._dead_letter_tag:
+        yield pvalue.TaggedOutput(
+            self._dead_letter_tag,
+            (
+                row,
+                (
+                    type(exn),
+                    repr(exn),
+                    traceback.format_exception(*sys.exc_info()))))
+      else:
+        raise
+
+
+class _PostprocessAndAttachDoFn(beam.DoFn):
+  def __init__(
+      self,
+      postprocess_fn: Callable[[Any], Any],
+      output_field: str,
+      flatten_predictions: bool = False,
+      unwrap_prediction_result: bool = False,
+      dead_letter_tag: Optional[str] = None):
+    self._postprocess_fn = postprocess_fn
+    self._output_field = output_field
+    self._flatten_predictions = flatten_predictions
+    self._unwrap_prediction_result = unwrap_prediction_result
+    self._dead_letter_tag = dead_letter_tag
+
+  @staticmethod
+  def _unwrap(pred: Any) -> Any:
+    val = pred.inference if isinstance(pred, PredictionResult) else pred
+    if (isinstance(val, (list, tuple)) and len(val) == 1 and
+        (isinstance(val[0], Mapping) or hasattr(val[0], '_asdict'))):
+      val = val[0]
+    return val
+
+  def process(self, element: tuple[Any, Any], *args, **kwargs):
+    row, raw_outcome = element
+    if self._dead_letter_tag is not None:
+      status, payload = raw_outcome
+      if status == _ERR:
+        yield pvalue.TaggedOutput(self._dead_letter_tag, (row, payload))
+        return
+      raw_pred = payload
+    else:
+      raw_pred = raw_outcome
+
+    try:
+      processed = self._postprocess_fn(raw_pred)
+      row_dict = dict(row._asdict()) if hasattr(row, '_asdict') else dict(row)
+
+      if self._flatten_predictions:
+        val = self._unwrap(processed)
+        if hasattr(val, '_asdict'):
+          val = val._asdict()
+        if isinstance(val, Mapping):
+          for k, v in val.items():
+            row_dict[f"{self._output_field}_{k}"] = _to_python_scalar(v)
+        else:
+          row_dict[self._output_field] = _to_python_scalar(val)
+      else:
+        if self._unwrap_prediction_result:
+          val = self._unwrap(processed)
+          if isinstance(val, Mapping):
+            val = beam.Row(**{k: _to_python_scalar(v) for k, v in val.items()})
+          else:
+            val = _to_python_scalar(val)
+        else:
+          val = processed
+        row_dict[self._output_field] = val
+
+      yield beam.Row(**row_dict)
+    except Exception as exn:
+      if self._dead_letter_tag:
+        yield pvalue.TaggedOutput(
+            self._dead_letter_tag,
+            (
+                row,
+                (
+                    type(exn),
+                    repr(exn),
+                    traceback.format_exception(*sys.exc_info()))))
+      else:
+        raise
+
+
+class _YamlRunInferenceTransform(beam.PTransform):
+  def __init__(
+      self,
+      model_handler_provider: ModelHandlerProvider,
+      output_field: str,
+      inference_args: Optional[dict[str, Any]] = None,
+      flatten_predictions: bool = False,
+      output_type: Optional[Any] = None,
+      declarative_mode: bool = False):
+    self._provider = model_handler_provider
+    self._output_field = output_field
+    self._inference_args = inference_args
+    self._flatten_predictions = flatten_predictions
+    self._output_type = output_type
+    self._declarative_mode = declarative_mode
+    self._exception_handling_args: Optional[dict[str, Any]] = None

Review Comment:
   **🟠 High**: `output_type` is not passed through 
`SafeLineLoader.strip_metadata(...)`. When a user specifies a mapping for 
`output_type` in YAML (e.g., `output_type: {label: str, score: float}`), 
`SafeLineLoader` attaches `'__line__'` and `'__uuid__'` keys to the dict. Both 
`_output_fields_for_inference()` and `_parse_type_spec()` iterate over 
`.items()`, causing bogus `<output_field>___line__` / `<output_field>___uuid__` 
(or nested `__line__` / `__uuid__`) fields to be added to the inferred output 
`RowTypeConstraint`.
   
   ```suggestion
       self._output_type = SafeLineLoader.strip_metadata(output_type)
   ```



##########
sdks/python/apache_beam/yaml/yaml_ml.py:
##########
@@ -66,20 +78,251 @@ def _list_submodules(package):
 except ImportError:
   MLTransform = None  # type: ignore
 
+_OK = 'ok'
+_ERR = 'err'
+
+_TYPE_MAP: dict[str, Any] = {
+    'str': str,
+    'string': str,
+    'int': int,
+    'integer': int,
+    'float': float,
+    'double': float,
+    'number': float,
+    'bool': bool,
+    'boolean': bool,
+    'bytes': bytes,
+    'any': Any,
+}
+
+
+def _parse_type_spec(spec: Any) -> Any:
+  if spec is None:
+    return Any
+  if isinstance(spec, type) or spec is Any:
+    return spec
+  if isinstance(spec, str):
+    return _TYPE_MAP.get(spec.lower(), Any)
+  if isinstance(spec, Mapping):
+    fields = [(str(k), _parse_type_spec(v)) for k, v in spec.items()]
+    return RowTypeConstraint.from_fields(fields)
+  return Any
+
+
+def _to_python_scalar(val: Any) -> Any:
+  if hasattr(val, 'item') and callable(val.item):
+    try:
+      return val.item()
+    except Exception:
+      pass
+  if hasattr(val, 'tolist') and callable(val.tolist):
+    try:
+      return val.tolist()
+    except Exception:
+      pass
+  return val
+
+
+class _WeakRefModelBox:
+  """Weak-referenceable wrapper for models that do not natively support 
weakref."""
+  def __init__(self, model: Any):
+    self.model = model
+
+
+class _FunctionModelHandler(ModelHandler[Any, Any, Any]):
+  """ModelHandler backed by user-supplied load_model_fn and inference_fn."""
+  def __init__(
+      self,
+      inference_fn: Callable[..., Any],
+      load_model_fn: Optional[Callable[[], Any]] = None,
+      **kwargs):
+    super().__init__(**kwargs)
+    self._inference_fn = inference_fn
+    self._load_model_fn = load_model_fn
+
+  def load_model(self) -> Any:
+    if self._load_model_fn is not None:
+      return self._load_model_fn()
+    return None
+
+  def run_inference(
+      self,
+      batch: Sequence[Any],
+      model: Any,
+      inference_args: Optional[dict[str, Any]] = None) -> Iterable[Any]:
+    try:
+      return self._inference_fn(batch, model, inference_args)
+    except TypeError:
+      try:
+        return self._inference_fn(batch, model)
+      except TypeError:
+        return [self._inference_fn(item) for item in batch]
+
+
+class _LazyYamlModelHandler(ModelHandler[Any, Any, Any]):
+  """Defers heavy ModelHandler instantiation and imports to worker setup()."""
+  def __init__(
+      self,
+      handler_factory: Callable[[], ModelHandler[Any, Any, Any]],
+      *,
+      min_batch_size: Optional[int] = None,
+      max_batch_size: Optional[int] = None,
+      max_batch_duration_secs: Optional[int] = None,
+      large_model: bool = False,
+      model_copies: Optional[int] = None,
+      catch_errors: bool = False,
+      **kwargs):
+    super().__init__(
+        min_batch_size=min_batch_size,
+        max_batch_size=max_batch_size,
+        max_batch_duration_secs=max_batch_duration_secs,
+        large_model=large_model,
+        model_copies=model_copies,
+        **kwargs)
+    self._handler_factory = handler_factory
+    self._underlying: Optional[ModelHandler[Any, Any, Any]] = None
+    self._catch_errors = catch_errors
+
+  def with_error_catching(self, catch_errors: bool = True):
+    self._catch_errors = catch_errors
+    return self
+
+  def _get_underlying(self) -> ModelHandler[Any, Any, Any]:

Review Comment:
   **🟡 Medium**: `_LazyYamlModelHandler` does not delegate `ModelHandler` 
worker hooks to `self._get_underlying()`:
   - `set_environment_vars()`: `env_vars` passed via `model_config` are 
forwarded to the inner handler via `clean_kwargs`, not to 
`_LazyYamlModelHandler`. When `_RunInferenceDoFn.setup()` calls 
`self._model_handler.set_environment_vars()`, the underlying handler's 
environment variables are never set.
   - `get_metrics_namespace()`: `_RunInferenceDoFn.setup()` calls this on the 
outer handler, so metrics are always reported under `'RunInference'` instead of 
the provider's namespace (e.g., `'BeamML_PyTorch'`, `'BeamML_Sklearn'`).
   - `get_num_bytes(batch)` and `update_model_path(model_path)`: Not forwarded 
to the underlying handler.
   
   Please override and forward these methods to `self._get_underlying()`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to