Repository navigation
Fix LangChainChunker import for langchain_text_splitters and copy chunk metadata - #41625
arijeetmukherjee3108-droid wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR updates LangChainChunker and its tests to support importing TextSplitter from the standalone langchain_text_splitters package (with a fallback to langchain.text_splitter), and ensures each yielded Chunk gets a shallow copy of the document metadata dictionary. The changes are clean, backward-compatible, and well-tested.
🔍 General Feedback
- The fallback import structure properly handles both modern (
langchain-text-splitters) and legacy (langchain) installations. - Copying the metadata dictionary per chunk prevents unintended cross-chunk state mutation in downstream transforms or custom
chunk_id_fncallbacks.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @damccorm 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). |
|
|
||
| try: | ||
| from langchain.text_splitter import TextSplitter | ||
| from langchain_text_splitters import TextSplitter |
There was a problem hiding this 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?
There was a problem hiding this comment.
Good catch, thank you. I grepped sdks/python/ and confirmed no other modules import langchain, so I replaced 'langchain' with 'langchain-text-splitters' in ml_base_core in sdks/python/setup.py so langchain_test.py runs in PreCommit Python ML.
| def check_independent_metadata(chunks): | ||
| if len(chunks) < 2: | ||
| raise BeamAssertException( | ||
| f"Expected at least 2 chunks, got {len(chunks)}") |
There was a problem hiding this 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)?
There was a problem hiding this comment.
Great catch—I hadn't accounted for the GroupByKey/coder round-trip inside assert_that deserializing separate Chunk objects. I updated test_chunks_have_independent_metadata to call list(_LangChainTextSplitter(...).process(doc)) directly and verify that mutating both top-level and nested metadata does not affect sibling chunks or the input doc.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #41625 +/- ##
=========================================
Coverage 58.72% 58.73%
Complexity 2315 2315
=========================================
Files 1135 1135
Lines 170465 170488 +23
Branches 1499 1499
=========================================
+ Hits 100113 100135 +22
- Misses 67806 67807 +1
Partials 2546 2546
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
fixes #41623
LangChainChunkercurrently only importsTextSplitterfromlangchain.text_splitter, which raises anImportErrorwhen using standalonelangchain-text-splittersand silently skipslangchain_test.py._LangChainTextSplitter.processalso passes the samemetadatadict reference to everyChunkyielded from a document, so mutatingchunk.metadatadownstream affects all sibling chunks.TextSplitter(and test splitters) fromlangchain_text_splittersfirst, falling back tolangchain.text_splitter.copy.deepcopy(metadata)for each yieldedChunkand add a test verifying metadata independence inlangchain_test.py.Tested locally with
pytest sdks/python/apache_beam/ml/rag/chunking/langchain_test.py -v.