weaviate-python-client
weaviate-python-client copied to clipboard
Reduce stack traces in error messages
When there is an error the client returns a stack trace. Sometimes the stack trace is very long, which makes it hard to find the error statement. Can the stack traces be shortened (or made optional)?
In this case, the error statement is:
UnexpectedStatusCodeError: Create class! Unexpected status code: 422, with response body: {'error': [{'message': 'class name "Questions" already exists'}]}.
The full stack trace is:
---------------------------------------------------------------------------
UnexpectedStatusCodeError Traceback (most recent call last)
Cell In[54], line 23
20 print("Success!")
22 #Create the collection for products
---> 23 collection = client.collections.create(
24 name="Questions",
25 vectorizer_config=wvc.Configure.Vectorizer.text2vec_transformers(),
26 properties=[
27 wvc.Property(
28 name="answer",
29 data_type=wvc.DataType.TEXT
30 ),
31 wvc.Property(
32 name="question",
33 data_type=wvc.DataType.TEXT
34 ),
35 wvc.Property(
36 name="category",
37 data_type=wvc.DataType.TEXT
38 )
39 ]
40 )
42 print("Collection Created!")
44 # Import all Questions in batches
File /opt/conda/lib/python3.11/site-packages/weaviate/collections/collections.py:104, in _Collections.create(self, name, description, generative_config, inverted_index_config, multi_tenancy_config, properties, references, replication_config, reranker_config, sharding_config, vector_index_config, vectorizer_config, data_model_properties, data_model_references)
45 """Use this method to create a collection in Weaviate and immediately return a collection object.
46
47 This method takes several arguments that allow you to configure the collection to your liking. Each argument
(...)
88 If the configuration object is invalid.
89 """
90 config = _CollectionConfigCreate(
91 description=description,
92 generative_config=generative_config,
(...)
102 vector_index_config=vector_index_config,
103 )
--> 104 name = super()._create(config._to_dict())
105 if config.name != name:
106 raise ValueError(
107 f"Name of created collection ({name}) does not match given name ({config.name})"
108 )
File /opt/conda/lib/python3.11/site-packages/weaviate/collections/base.py:62, in _CollectionsBase._create(self, config)
60 raise RequestsConnectionError("Class may not have been created properly.") from conn_err
61 if response.status_code != 200:
---> 62 raise UnexpectedStatusCodeError("Create class", response)
64 collection_name = response.json()["class"]
65 assert isinstance(collection_name, str)
UnexpectedStatusCodeError: Create class! Unexpected status code: 422, with response body: {'error': [{'message': 'class name "Questions" already exists'}]}.
Update: The error messages are different in Jupyter(VSCode) and on the command line. The Jupyter messages are much longer.
Command line:
Traceback (most recent call last):
File "/Users/davecuthbert/src/weaviate-io/v125-20240509/brokenConnection.py", line 16, in <module>
response = collection.generate.near_text(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/davecuthbert/src/weaviate-io/v125-20240509/lib/python3.11/site-packages/weaviate/collections/queries/near_text/generate.py", line 99, in near_text
res = self._query.near_text(
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/davecuthbert/src/weaviate-io/v125-20240509/lib/python3.11/site-packages/weaviate/collections/grpc/query.py", line 450, in near_text
return self.__call(request)
^^^^^^^^^^^^^^^^^^^^
File "/Users/davecuthbert/src/weaviate-io/v125-20240509/lib/python3.11/site-packages/weaviate/collections/grpc/query.py", line 653, in __call
assert self._connection.grpc_stub is not None
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/davecuthbert/src/weaviate-io/v125-20240509/lib/python3.11/site-packages/weaviate/connect/v4.py", line 730, in grpc_stub
raise WeaviateClosedClientError()
weaviate.exceptions.WeaviateClosedClientError: The `WeaviateClient` is closed. Run `client.connect()` to (re)connect!
Jupyter:
--------------------------------------------------------------------------
WeaviateClosedClientError Traceback (most recent call last)
Cell In[6], line 16
13 client.close()
15 collection = client.collections.get("WikipediaSimple")
---> 16 response = collection.generate.near_text(
17 query="A Harry Potter plot reference",
18 limit=10,
19 )
21 print(len(response.objects))
22 print(response.objects[0].properties["content"])
File ~/src/weaviate-io/v125-crossword/lib/python3.11/site-packages/weaviate/collections/queries/near_text/generate.py:99, in _NearTextGenerate.near_text(self, query, single_prompt, grouped_task, grouped_properties, certainty, distance, move_to, move_away, limit, offset, auto_limit, filters, group_by, rerank, target_vector, include_vector, return_metadata, return_properties, return_references)
26 def near_text(
27 self,
28 query: Union[List[str], str],
(...)
47 return_references: Optional[ReturnReferences[TReferences]] = None,
48 ) -> GenerativeSearchReturnType[Properties, References, TProperties, TReferences]:
49 """Perform retrieval-augmented generation (RaG) on the results of a by-image object search in this collection using the image-capable vectorization module and vector-based similarity search.
50
51 See the [docs](https://weaviate.io/developers/weaviate/api/graphql/search-operators#neartext) for a more detailed explanation.
(...)
97 If the request to the Weaviate server fails.
98 """
---> 99 res = self._query.near_text(
100 near_text=query,
101 certainty=certainty,
102 distance=distance,
103 move_to=move_to,
104 move_away=move_away,
105 limit=limit,
106 offset=offset,
107 autocut=auto_limit,
108 filters=filters,
109 group_by=_GroupBy.from_input(group_by),
110 rerank=rerank,
111 target_vector=target_vector,
112 generative=_Generative(
113 single=single_prompt,
114 grouped=grouped_task,
115 grouped_properties=grouped_properties,
116 ),
117 return_metadata=self._parse_return_metadata(return_metadata, include_vector),
118 return_properties=self._parse_return_properties(return_properties),
119 return_references=self._parse_return_references(return_references),
120 )
121 return self._result_to_generative_return(
122 res,
123 _QueryOptions.from_input(
(...)
133 return_references,
134 )
File ~/src/weaviate-io/v125-crossword/lib/python3.11/site-packages/weaviate/collections/grpc/query.py:450, in _QueryGRPC.near_text(self, near_text, certainty, distance, move_to, move_away, limit, offset, autocut, filters, group_by, generative, rerank, target_vector, return_metadata, return_properties, return_references)
427 near_text_req = search_get_pb2.NearTextSearch(
428 query=near_text,
429 certainty=certainty,
(...)
433 move_to=self.__parse_move(move_to),
434 )
436 request = self.__create_request(
437 limit=limit,
438 offset=offset,
(...)
447 near_text=near_text_req,
448 )
--> 450 return self.__call(request)
File ~/src/weaviate-io/v125-crossword/lib/python3.11/site-packages/weaviate/collections/grpc/query.py:653, in _QueryGRPC.__call(self, request)
651 def __call(self, request: search_get_pb2.SearchRequest) -> search_get_pb2.SearchReply:
652 try:
--> 653 assert self._connection.grpc_stub is not None
654 res: search_get_pb2.SearchReply # According to PEP-0526
655 res, _ = self._connection.grpc_stub.Search.with_call(
656 request,
657 metadata=self._connection.grpc_headers(),
658 timeout=self._connection.timeout_config.query,
659 )
File ~/src/weaviate-io/v125-crossword/lib/python3.11/site-packages/weaviate/connect/v4.py:730, in ConnectionV4.grpc_stub(self)
727 @property
728 def grpc_stub(self) -> Optional[weaviate_pb2_grpc.WeaviateStub]:
729 if not self.is_connected():
--> 730 raise WeaviateClosedClientError()
731 return self._grpc_stub
WeaviateClosedClientError: The `WeaviateClient` is closed. Run `client.connect()` to (re)connect!
The actual error:
WeaviateClosedClientError: The `WeaviateClient` is closed. Run `client.connect()` to (re)connect!
Reproducer code:
import os
import weaviate
client = weaviate.connect_to_local(
headers={
"X-OpenAI-Api-Key": os.environ["OPENAI_APIKEY"]
}
)
print(client.is_ready())
# Break connection to cause error
client.close()
collection = client.collections.get("WikipediaSimple")
response = collection.generate.near_text(
query="A Harry Potter plot reference",
limit=10,
)
print(len(response.objects))
print(response.objects[0].properties["content"])