Skip to content

Commit aeb5313

Browse files
committed
Return helpful error when an empty list of docs are imported.
1 parent bb2a15c commit aeb5313

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

examples/collection_operations.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import sys
44
import typesense
5-
5+
from typesense.exceptions import TypesenseClientError
66

77
curr_dir = os.path.dirname(os.path.realpath(__file__))
88
sys.path.insert(1, os.path.abspath(os.path.join(curr_dir, os.pardir)))
@@ -131,6 +131,13 @@
131131
# Deleting documents matching a filter query
132132
print(client.collections['books'].documents.delete({'filter_by': 'ratings_count: 4780653'}))
133133

134+
# Try importing empy list
135+
try:
136+
import_results = client.collections['books'].documents.import_([], {"action": "upsert"})
137+
print(import_results)
138+
except TypesenseClientError as e:
139+
print("Detected import of empty document list.")
140+
134141
# Drop the collection
135142
drop_response = client.collections['books'].delete()
136143
print(drop_response)

typesense/documents.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def import_(self, documents, params=None, batch_size=None):
7474
for document in documents:
7575
document_strs.append(json.dumps(document))
7676

77+
if len(document_strs) == 0:
78+
raise TypesenseClientError(f"Cannot import an empty list of documents.")
79+
7780
docs_import = '\n'.join(document_strs)
7881
api_response = self.api_call.post(self._endpoint_path('import'), docs_import, params, as_json=False)
7982
res_obj_strs = api_response.split('\n')

0 commit comments

Comments
 (0)