Skip to content

[Python] Support RunInference models that can't be weakly referenced - #41407

Open
schizophrenicmaniac wants to merge 7 commits into
apache:masterfrom
schizophrenicmaniac:fix/41351-bug-python-ml-runinference-with
Open

schizophrenicmaniac wants to merge 7 commits into
apache:masterfrom
schizophrenicmaniac:fix/41351-bug-python-ml-runinference-with

Conversation

@schizophrenicmaniac

@schizophrenicmaniac schizophrenicmaniac commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #41351

RunInference caches the loaded model with shared.Shared, which keeps a weak reference to it. If a ModelHandler.load_model() returns something like a dict (a common way to bundle a tokenizer and a model), setup fails with TypeError: cannot create weak reference to 'dict' object. _RunInferenceDoFn._load_model now checks whether the loaded model can be weakly referenced and, if not, caches it inside a small holder object. It unwraps the model before handing it to run_inference, so handlers still get the dict they returned. The DoFn keeps a strong reference to the cached holder so it isn't collected while in use. The multi-process and ModelManager paths don't go through shared.Shared and are unchanged.

Added tests for running a pipeline with a dict model and for reusing a cached dict model across new DoFn instances. Also added a CHANGES.md entry.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR fixes a TypeError in RunInference when ModelHandler.load_model() returns an object that does not support weak references (such as a dict or list) by wrapping non-weak-referenceable models in a lightweight holder object before caching them in shared.Shared while preserving a strong reference on the DoFn. It also updates the with_exception_handling() docstring to accurately describe the dead-letter error info tuple and adds thorough unit tests for both changes.

🔍 General Feedback

  • The _WeakRefableModelHolder design cleanly handles both weak-referenceable and non-weak-referenceable models while properly maintaining reference lifetimes via self._shared_model on _RunInferenceDoFn.
  • The docstring update for RunInference.with_exception_handling() accurately reflects the error tuple emitted by PTransformWithSideInputs.with_exception_handling().
  • The added tests cover pipeline execution, DoFn model reuse across garbage collection cycles, and the structure of failed_inferences error info.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions github-actions Bot added python size/m M: 50-249 lines changed labels Nov 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @jrmccluskey for label python.
Backup reviewer: tvalentyn (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@codecov

codecov Bot commented Nov 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.60%. Comparing base (351ceba) to head (1604aa5).
⚠️ Report is 22 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #41407   +/-   ##
=========================================
  Coverage     58.60%   58.60%           
  Complexity     2292     2292           
=========================================
  Files          1133     1133           
  Lines        170086   170103   +17     
  Branches       1487     1487           
=========================================
+ Hits          99685    99695   +10     
- Misses        67860    67867    +7     
  Partials       2541     2541           
Flag Coverage Δ
python 80.55% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jrmccluskey jrmccluskey left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are two different problems and would be worth discussing separately. The weak references are a new feature, but the DLQ docstring probably needs to be discussed further. There' an argument for changing the docstring, there's also potential to change the behavior downstream (e.g. make it actually pass the exception down.) I'd rather the latter get split into another PR and the weak references be taken forward here

@schizophrenicmaniac

Copy link
Copy Markdown
Contributor Author

@jrmccluskey Makes sense. I've removed the docstring change and its test, so this PR now only covers the weak reference handling.

@github-actions github-actions Bot added size/s S: 10-49 lines changed and removed size/m M: 50-249 lines changed labels Nov 19, 2026
@schizophrenicmaniac schizophrenicmaniac changed the title [Python] Support RunInference models that can't be weakly referenced and fix the DLQ docstring [Python] Support RunInference models that can't be weakly referenced Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants