Skip to content

[Bug]: [Python/ML] RunInference.with_exception_handling() documents Exception tuple while emitting str, and shared.Shared fails on dict models #41351

Done

Description

@jk-shah

What happened?

Two related runtime issues affect custom error handling and ModelHandler implementations in apache_beam.ml.inference.RunInference:

  1. RunInference.with_exception_handling() docstring vs runtime type mismatch (sdks/python/apache_beam/ml/inference/base.py:1795-1806 vs transforms/core.py:1624, 2550):
    • RunInference.with_exception_handling() documents failed_inferences, failed_preprocessing, and failed_postprocessing as PCollection[tuple[failed batch, exception]].
    • At runtime, _ExceptionHandlingWrapperDoFn (transforms/core.py:2550) emits (element, traceback.format_exc()) where the second tuple element is a str, not an Exception instance — causing downstream dead-letter transforms that inspect exc.args or type(exc).__name__ to fail.
    • (Additionally, on batched inference, a single poison-pill element in a batch of N fails all N elements and evicts/reloads the entire model in memory per lines 1829-1830.)
  2. Custom ModelHandler.load_model() returning a dict crashes shared.Shared.acquire() (sdks/python/apache_beam/utils/shared.py:136):
    • Multi-artifact custom ModelHandlers commonly return {'tokenizer': tok, 'model': mdl} from load_model().
    • shared.Shared.acquire() wraps the loaded object directly in weakref.ref(result), crashing immediately with:
      TypeError: cannot create weak reference to 'dict' object.

Minimal reproducer:

import apache_beam as beam
from apache_beam.ml.inference.base import ModelHandler, RunInference

class DictModelHandler(ModelHandler):
  def load_model(self):
    return {'weights': [1.0, 2.0]}
  def run_inference(self, batch, model, inference_args=None):
    return [sum(model['weights']) * x for x in batch]

with beam.Pipeline() as p:
  _ = p | beam.Create([1, 2]) | RunInference(DictModelHandler())
# Raises TypeError: cannot create weak reference to 'dict' object

Cause

_ExceptionHandlingWrapperDoFn formats traceback.format_exc() as str while RunInference.with_exception_handling() documents exception objects; and shared.py calls weakref.ref(result) without wrapping non-weakrefable containers or raising an actionable error pointing to ModelHandler.load_model().

Issue Priority

Priority: 2 (default / most bugs should be filed as P2)

Issue Components

  • Component: Python SDK
  • Component: Java SDK
  • Component: Go SDK
  • Component: Typescript SDK
  • Component: IO connector
  • Component: Beam YAML
  • Component: Beam ML
  • Component: Beam examples
  • Component: Beam playground
  • Component: Beam katas
  • Component: Website
  • Component: Infrastructure
  • Component: Spark Runner
  • Component: Flink Runner
  • Component: Prism Runner
  • Component: Hazelcast Jet Runner
  • Component: Google Cloud Dataflow Runner

Activity

  1. schizophrenicmaniac commented on Nov 16, 2026

    @schizophrenicmaniac
    Contributor

    .take-issue

    Hi, I'd like to work on this. I'd wrap models that can't be weakly referenced (dict, list) in a small holder in _RunInferenceDoFn._load_model before they reach shared.Shared, and fix the with_exception_handling() docstring to describe the (type, repr, traceback) tuple that's actually emitted, with tests for both.

  2. added this to the 2.79.0 Release milestone on Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions