Repository navigation
Conversation
The output functions of MLTransform dict embeddings (_dict_output_fn), of the RAG embedding adapters (_add_embedding_fn) and of the Vertex AI multimodal dict adapter (_multimodal_dict_output_fn) assigned the embeddings onto the elements they were given. Those elements are shared with any other consumer of the same PCollection, which then saw the embedded values instead of the original ones. Copy the elements before assigning.
There was a problem hiding this comment.
📋 Review Summary
This PR resolves #41233 by preventing in-place mutation of input elements in _add_embedding_fn, _dict_output_fn, and _multimodal_dict_output_fn. Each output function now shallow-copies input dictionaries (copy.copy) and dataclass instances (dataclasses.replace) before attaching embeddings, upholding Apache Beam's element immutability contract across sibling transforms.
🔍 General Feedback
- The use of
copy.copy(preservingdictsubclasses) anddataclasses.replaceonEmbeddableItem,VertexImage, andVertexVideoavoids unnecessary deep copies of underlying payloads while cleanly isolating output mutations. - Unit tests thoroughly cover all three output functions and verify that input references and nested objects remain unchanged.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @tvalentyn for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Fixes #41233
The output functions of MLTransform embeddings created with
columns=(_dict_output_fn), of the RAG embedding adapters (_add_embedding_fn) and ofVertexAIMultiModalEmbeddings(_multimodal_dict_output_fn) assigned the embeddings onto the input elements. The programming guide warns that "Changes to input elements may be visible to other DoFns executing within the same bundle, and may cause correctness issues", and that happens here: another consumer of the same PCollection (for examplebeam.combiners.ToList()) received the embedded values. The returned element has to be a different object from the input, so each function now returns a shallow copy (copy.copyfor the dict,dataclasses.replacefor the items and the nested Vertex image/video/text objects); the values themselves are not copied.Tests: one per function (the Vertex one uses a stand-in response, no GCP); each fails on master.
pytest apache_beam/ml/transforms/base_test.py apache_beam/ml/rag/embeddings/base_test.py apache_beam/ml/transforms/embeddings/vertex_ai_test.py: 40 passed, 12 skipped (TFT tests not installed). Two-branch pipeline (input into MLTransform with a test handler that reverses the text, and intoToList()): on master theToListbranch receivedcba/deffor the inputsabc/defon the FnApiRunner andcba/fedon the default runner (Prism); which elements are hit depends on how the runner fuses the stages. After the change it receivesabc/defon both.