diff --git a/integrations/opensearch/src/haystack_integrations/document_stores/opensearch/document_store.py b/integrations/opensearch/src/haystack_integrations/document_stores/opensearch/document_store.py index e9c88274c..f5bbe2b3b 100644 --- a/integrations/opensearch/src/haystack_integrations/document_stores/opensearch/document_store.py +++ b/integrations/opensearch/src/haystack_integrations/document_stores/opensearch/document_store.py @@ -181,6 +181,11 @@ def write_documents(self, documents: List[Document], policy: DuplicatePolicy = D duplicate_errors_ids = [] other_errors = [] for e in errors: + # OpenSearch might not return a correctly formatted error, in that case we + # treat it as a generic error + if "create" not in e: + other_errors.append(e) + continue error_type = e["create"]["error"]["type"] if policy == DuplicatePolicy.FAIL and error_type == "version_conflict_engine_exception": duplicate_errors_ids.append(e["create"]["_id"]) diff --git a/integrations/opensearch/tests/test_document_store.py b/integrations/opensearch/tests/test_document_store.py index bc0d1c434..8e984953d 100644 --- a/integrations/opensearch/tests/test_document_store.py +++ b/integrations/opensearch/tests/test_document_store.py @@ -324,3 +324,12 @@ def test_write_documents_different_embedding_sizes_fail( with pytest.raises(DocumentStoreError): document_store_embedding_dim_4.write_documents(docs) + + @patch("haystack_integrations.document_stores.opensearch.document_store.bulk") + def test_write_documents_with_badly_formatted_bulk_errors(self, mock_bulk, document_store): + error = {"some_key": "some_value"} + mock_bulk.return_value = ([], [error]) + + with pytest.raises(DocumentStoreError) as e: + document_store.write_documents([Document(content="Hello world")]) + e.match(f"{error}")