Fix: RAGFlowJsonParser crashes with IndexError on top-level JSON scalars (#16877)

This commit is contained in:
Yash Raj Pandey
2026-07-28 04:58:42 -04:00
committed by GitHub
parent b8bb2297f9
commit b08e5f5647
2 changed files with 103 additions and 4 deletions

View File

@@ -45,6 +45,15 @@ class RAGFlowJsonParser:
"""Calculate the size of the serialized JSON object."""
return len(json.dumps(data, ensure_ascii=False))
@staticmethod
def _is_empty_chunk(chunk: Any) -> bool:
"""Only null and empty containers carry no content; 0 and false do."""
if chunk is None:
return True
if isinstance(chunk, (dict, list, str)):
return not chunk
return False
@staticmethod
def _set_nested_dict(d: dict, path: list[str], value: Any) -> None:
"""Set a value in a nested dictionary based on the given path."""
@@ -93,7 +102,11 @@ class RAGFlowJsonParser:
self._json_split(value, new_path, chunks)
else:
# handle single item
self._set_nested_dict(chunks[-1], current_path, data)
if not current_path:
# top-level scalar (number/string/bool/null) has no key to nest under
chunks[-1] = data
else:
self._set_nested_dict(chunks[-1], current_path, data)
return chunks
def split_json(
@@ -110,7 +123,7 @@ class RAGFlowJsonParser:
chunks = self._json_split(json_data, None, None)
# Remove the last chunk if it's empty
if not chunks[-1]:
if chunks and self._is_empty_chunk(chunks[-1]):
chunks.pop()
return chunks
@@ -132,7 +145,7 @@ class RAGFlowJsonParser:
try:
json_data = json.loads(content)
chunks = self.split_json(json_data, True)
sections = [json.dumps(line, ensure_ascii=False) for line in chunks if line]
sections = [json.dumps(line, ensure_ascii=False) for line in chunks if not self._is_empty_chunk(line)]
except json.JSONDecodeError:
pass
return sections
@@ -146,7 +159,7 @@ class RAGFlowJsonParser:
try:
data = json.loads(line)
chunks = self.split_json(data, convert_lists=True)
all_chunks.extend(json.dumps(chunk, ensure_ascii=False) for chunk in chunks if chunk)
all_chunks.extend(json.dumps(chunk, ensure_ascii=False) for chunk in chunks if not self._is_empty_chunk(chunk))
except json.JSONDecodeError:
continue
return all_chunks