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


##########
sdks/python/apache_beam/ml/rag/chunking/langchain_test.py:
##########
@@ -227,6 +231,33 @@ def test_empty_text(self):
 
       assert_that(chunks, equal_to([]))
 
+  def test_chunks_have_independent_metadata(self):
+    """Test that each chunk receives an independent copy of metadata."""
+    splitter = RecursiveCharacterTextSplitter(
+        chunk_size=30, chunk_overlap=0, separators=[".", " "])
+    provider = LangChainChunker(
+        document_field='content',
+        metadata_fields=['source', 'language'],
+        text_splitter=splitter)
+
+    def check_independent_metadata(chunks):
+      if len(chunks) < 2:
+        raise BeamAssertException(
+            f"Expected at least 2 chunks, got {len(chunks)}")
+      chunks[0].metadata['source'] = 'mutated.txt'
+      for chunk in chunks[1:]:
+        if chunk.metadata['source'] != 'simple.txt':
+          raise BeamAssertException(
+              "Mutating one chunk's metadata affected another chunk.")
+      return True
+
+    with TestPipeline() as p:
+      chunks = (
+          p
+          | beam.Create([self.simple_text])
+          | provider.get_ptransform_for_processing())
+      assert_that(chunks, check_independent_metadata)
+
 
 if __name__ == '__main__':
-  unittest.main()
+  unittest.main()

Review Comment:
   **🟢 Nitpick**: Please preserve the trailing newline at the end of the file.



##########
sdks/python/apache_beam/ml/rag/chunking/langchain.py:
##########
@@ -115,4 +119,5 @@ def process(self, element):
     text_chunks = self.text_splitter.split_text(element[self.document_field])
     metadata = {field: element[field] for field in self.metadata_fields}
     for i, text_chunk in enumerate(text_chunks):
-      yield Chunk(content=Content(text=text_chunk), index=i, metadata=metadata)
+      yield Chunk(
+          content=Content(text=text_chunk), index=i, metadata=metadata.copy())

Review Comment:
   **🟢 Nitpick**: If a metadata field contains a mutable nested object (e.g., a 
`list` of tags or a nested `dict`), a shallow `.copy()` will still share 
references to those inner objects across chunks. Consider whether 
`copy.deepcopy(metadata)` might be safer here, or if shallow copying is 
sufficient for expected use cases. Also, please add a trailing newline at the 
end of the file to conform with standard formatting conventions.



-- 
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