From 0275bd8f163b66bae80f234802bc0f1b1d5ce4b5 Mon Sep 17 00:00:00 2001 From: Anai-Guo Date: Thu, 30 Jul 2026 06:16:17 -0700 Subject: [PATCH] fix(collections): re-raise non-404 errors from async Collection.exists() The async `Collection.exists()` still had a bare `except Exception: return False`, so a connection error, auth failure or timeout was indistinguishable from "collection not found". PR #1950 fixed this for the sync path only. Mirror the sync implementation: return False on 404 and re-raise anything else. Adds a mock test covering both branches on the async client. Fixes #2100 --- mock_tests/test_collection.py | 20 ++++++++++++++++++++ weaviate/collections/collection/async_.py | 7 +++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/mock_tests/test_collection.py b/mock_tests/test_collection.py index 7d325462e..7769e23e0 100644 --- a/mock_tests/test_collection.py +++ b/mock_tests/test_collection.py @@ -484,6 +484,26 @@ def test_collection_exists(weaviate_mock: HTTPServer) -> None: assert e.value.status_code == 500 +@pytest.mark.asyncio +async def test_async_collection_exists(weaviate_mock: HTTPServer) -> None: + non_existing = "NonExistingCollection" + erroring = "ErroringCollection" + weaviate_mock.expect_request(f"/v1/schema/{non_existing}").respond_with_json( + response_json={"error": [{"message": "collection not found"}]}, status=404 + ) + weaviate_mock.expect_request(f"/v1/schema/{erroring}").respond_with_json( + response_json={"error": [{"message": "this is an error"}]}, status=500 + ) + + async with weaviate.use_async_with_local( + port=MOCK_PORT, host=MOCK_IP, grpc_port=MOCK_PORT_GRPC, skip_init_checks=True + ) as client: + assert not await client.collections.use(non_existing).exists() + with pytest.raises(weaviate.exceptions.UnexpectedStatusCodeError) as e: + await client.collections.use(erroring).exists() + assert e.value.status_code == 500 + + def test_grpc_client_version_header( metadata_capture_collection: tuple[ weaviate.collections.Collection, MockMetadataCaptureWeaviateService diff --git a/weaviate/collections/collection/async_.py b/weaviate/collections/collection/async_.py index 2c0cea5b0..7342c0450 100644 --- a/weaviate/collections/collection/async_.py +++ b/weaviate/collections/collection/async_.py @@ -27,6 +27,7 @@ from weaviate.collections.query import _QueryCollectionAsync from weaviate.collections.tenants import _TenantsAsync from weaviate.connect.v4 import ConnectionAsync +from weaviate.exceptions import UnexpectedStatusCodeError from weaviate.types import UUID from .base import _CollectionBase @@ -183,8 +184,10 @@ async def exists(self) -> bool: try: await self.config.get(simple=True) return True - except Exception: - return False + except UnexpectedStatusCodeError as e: + if e.status_code == 404: + return False + raise e async def shards(self) -> List[Shard]: """Get the statuses of all the shards of this collection.