Skip to content
Open
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
11 changes: 8 additions & 3 deletions libs/text-splitters/langchain_text_splitters/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,21 @@ def _json_split(
size = self._json_size({key: value})
remaining = self.max_chunk_size - chunk_size

if size < remaining:
if size <= remaining:
# Add item to current chunk
self._set_nested_dict(chunks[-1], new_path, value)
else:
if chunk_size >= self.min_chunk_size:
# Chunk is big enough, start a new chunk
chunks.append({})

# Iterate
self._json_split(value, new_path, chunks)
# Only recurse into non-empty dicts; for leaf values
# and empty dicts, add them directly to the current chunk
# so they are not silently dropped.
if isinstance(value, dict) and value:
self._json_split(value, new_path, chunks)
else:
self._set_nested_dict(chunks[-1], new_path, value)
else:
# handle single item
self._set_nested_dict(chunks[-1], current_path, data)
Expand Down