Skip to content

Commit 716dbbd

Browse files
[PLT-981][PLT-982] Throw user readable errors when creating a custom embedding with an existing name or too few dimensions.
1 parent b142a6f commit 716dbbd

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

libs/labelbox/src/labelbox/adv_client.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
from typing import Dict, Any, Optional, List, Callable
55
from urllib.parse import urlparse
6+
from labelbox.exceptions import ADVError
67

78
import requests
89
from requests import Session, Response
@@ -59,7 +60,18 @@ def _request(self,
5960
url,
6061
data=requests_data,
6162
headers=headers)
62-
response.raise_for_status()
63+
if response.status_code != 200:
64+
response_json = response.json()
65+
if response_json.get('status') == 'CONFLICT':
66+
raise ADVError("A custom embedding with this name already exists. Custom embeddings must have unique names.")
67+
message = response_json.get('message')
68+
if message:
69+
if message == "must be greater than or equal to 8":
70+
raise ADVError("Custom embeddings require at least 8 dimensions.")
71+
else:
72+
raise ADVError(response.json()['message'])
73+
else:
74+
response.raise_for_status()
6375
return response.json()
6476

6577
def _send_ndjson(self,

libs/labelbox/src/labelbox/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ def __init__(self, message, cause=None):
1515
def __str__(self):
1616
return self.message + str(self.args)
1717

18+
class ADVError(LabelboxError):
19+
"""Raised when ADV service returns an error for a REST call"""
20+
pass
1821

1922
class AuthenticationError(LabelboxError):
2023
"""Raised when an API key fails authentication."""

libs/labelbox/src/labelbox/schema/embedding.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Optional, Callable, Dict, Any, List
22

33
from labelbox.adv_client import AdvClient
4+
from labelbox.exceptions import ADVError
45
from labelbox.pydantic_compat import BaseModel, PrivateAttr
56

67

libs/labelbox/tests/integration/test_embedding.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ def callback(_: Dict[str, Any]):
5454
assert event.wait(10.0) # seconds
5555

5656

57+
def test_create_embedding_with_too_few_dimensions(client: Client):
58+
with pytest.raises(labelbox.exceptions.ADVError):
59+
client.create_embedding(f"toosmall", 1)
60+
61+
62+
def test_create_embedding_with_name_collision(client: Client, embedding: Embedding):
63+
with pytest.raises(labelbox.exceptions.ADVError):
64+
client.create_embedding(embedding.name, 1)
65+
66+
5767
def test_get_imported_vector_count(dataset: Dataset, embedding: Embedding):
5868
assert embedding.get_imported_vector_count() == 0
5969

0 commit comments

Comments
 (0)