Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions haystack/components/preprocessors/python_code_splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,9 @@ def _secondary_split(self, unit: _CodeUnit, parent_doc: Document) -> list[Docume
meta["secondary_split"] = True
meta["secondary_split_index"] = idx
meta["secondary_split_total"] = len(intermediate)
meta["qualified_name"] = qualified_name
results.append(Document(content=piece.content or "", meta=meta))

return results

@component.output_types(documents=list[Document])
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
fixes:
- |
Fixed ``PythonCodeSplitter`` losing identifying context for oversized
functions, methods, or classes. When a unit is too large and falls back
to line-based secondary splitting, only the first resulting piece
naturally retains the source ``def``/``class`` line; every piece now
includes a ``qualified_name`` field in ``meta`` identifying the function,
method, or class it came from.
6 changes: 6 additions & 0 deletions test/components/preprocessors/test_python_code_splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,12 @@ def longer():
assert chunks_with_short and chunks_with_long
assert chunks_with_short[0] is not chunks_with_long[0]

def test_qualified_name_in_meta_for_all_pieces(self, oversized_function_source):
splitter = PythonCodeSplitter(min_effective_lines=2, max_effective_lines=5, oversized_factor=3)
result = splitter.run(documents=[Document(content=oversized_function_source)])
for piece in result["documents"]:
assert piece.meta.get("qualified_name") == "giant"


class TestEdgeCases:
def test_module_with_only_docstring(self):
Expand Down
Loading