damccorm commented on code in PR #41625:
URL: https://github.com/apache/beam/pull/41625#discussion_r4684686441


##########
sdks/python/apache_beam/ml/rag/chunking/langchain.py:
##########
@@ -115,4 +120,8 @@ 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=copy.deepcopy(metadata))
+      

Review Comment:
   Nit: there's a trailing whitespace-only line and missing final newline here 
(and at the end of `langchain_test.py`) which will trip the 
formatter/whitespace checks.



##########
sdks/python/apache_beam/ml/rag/chunking/langchain_test.py:
##########
@@ -227,6 +231,39 @@ 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."""
+    doc = {**self.simple_text, 'tags': ['a']}
+    splitter = RecursiveCharacterTextSplitter(
+        chunk_size=30, chunk_overlap=0, separators=[".", " "])
+    provider = LangChainChunker(
+        document_field='content',
+        metadata_fields=['source', 'language', 'tags'],
+        text_splitter=splitter)
+
+    def check_independent_metadata(chunks):
+      if len(chunks) < 2:
+        raise BeamAssertException(
+            f"Expected at least 2 chunks, got {len(chunks)}")

Review Comment:
   Since `check_independent_metadata` runs inside `assert_that` (after a 
`GroupByKey` / coder serialization round-trip), each `Chunk` has already been 
deserialized into a separate object by the time we mutate `chunks[0].metadata` 
— so this test would pass even without `copy.deepcopy(metadata)`.
   
   To actually catch the shared-reference bug, could we either call 
`_LangChainTextSplitter(...).process(doc)` directly (`list(dofn.process(doc))`) 
or mutate `chunk.metadata` inside a `chunk_id_fn` / downstream `beam.Map` 
before `assert_that` (e.g., mutating when `chunk.index == 0` and asserting 
later chunks aren't affected)?



##########
sdks/python/apache_beam/ml/rag/chunking/langchain.py:
##########
@@ -25,9 +26,12 @@
 from apache_beam.ml.rag.types import Content
 
 try:
-  from langchain.text_splitter import TextSplitter
+  from langchain_text_splitters import TextSplitter

Review Comment:
   Right now `sdks/python/setup.py` (`ml_base_core`) only lists `'langchain'`, 
which resolves to `langchain` 1.x in `PreCommit Python ML` and doesn't include 
`langchain-text-splitters` or `langchain.text_splitter` — so all tests in 
`langchain_test.py` are still getting silently skipped in CI. Could you also 
replace `'langchain'` with `'langchain-text-splitters'` (or add it alongside) 
in `sdks/python/setup.py` so these tests actually run?



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