# Collection does not exist exception in Python V4

**URL:** <https://forum.weaviate.io/t/collection-does-not-exist-exception-in-python-v4/2518>\
**Category:** General\
**Tags:** developer-experience, python\
**Created:** [May 28, 2024, 11:29am UTC](https://forum.weaviate.io/t/collection-does-not-exist-exception-in-python-v4/2518 "2024-05-28T11:29:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ggapac](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/ggapac/32/1282_2.png) [@ggapac](https://forum.weaviate.io/u/ggapac)\
**Post date:** [May 28, 2024, 11:29am UTC](https://forum.weaviate.io/t/collection-does-not-exist-exception-in-python-v4/2518/1 "2024-05-28T11:29:07Z")

</div>

Hi,

I am trying to insert data and I would expect Weaviate to throw an exception if the collection I am trying to insert into does not exist. In my system configuration file I set `AUTOSCHEMA_ENABLED: 'false'` like suggested in the documentation, however, I get no exception or any other error message if I am inserting data using a collection name that does not exist and was therefore not predefined.

Am I missing something or is there really no way of Weaviate warning me here?

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [May 29, 2024, 6:37pm UTC](https://forum.weaviate.io/t/collection-does-not-exist-exception-in-python-v4/2518/2 "2024-05-29T18:37:54Z")

</div>

Hi! I was not able to reproduce this.

For instance:

```auto
import weaviate
from weaviate import classes as wvc
client = weaviate.connect_to_local()
client.collections.delete("Test")

client.collections.exists("Test")
# False

test_col = client.collections.get("Test")
# note here: you get the object instantiated, this doesn't mean the collection exists.

# try insert some data on non existant collection
test_col.data.insert({"text": "this"})

```

I get:

> UnexpectedStatusCodeError: Object was not added! Unexpected status code: 422, with response body: {‘error’: [{‘message’: ‘invalid object: class “Test” not found in schema’}]}.

`AUTOSCHEMA_ENABLED` will mean that if there is a collection, without a previously created property, and you try to insert, Weaviate will raise an error. Like so:

```auto
col = client.collections.create("New")
col.data.insert({"non_existant": "property"})

```

Would return this error:

> UnexpectedStatusCodeError: Object was not added! Unexpected status code: 422, with response body: {‘error’: [{‘message’: “invalid object: no such prop with name ‘non\_existant’ found in class ‘New’ in the schema. Check your schema files for which properties in this class are available”}]}.

Let me know if this helps 🙂

---

<div class="post-metadata">

**Author:** ![ggapac](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/ggapac/32/1282_2.png) [@ggapac](https://forum.weaviate.io/u/ggapac)\
**Post date:** [May 31, 2024, 12:25pm UTC](https://forum.weaviate.io/t/collection-does-not-exist-exception-in-python-v4/2518/3 "2024-05-31T12:25:59Z")

</div>

Hi, thanks for your answer. I managed to reproduce this with your code, however, my issue occurred during bulk insert. The following code results in no error or exception:

```auto
test_col = client.collections.get("Test")

with client.batch.fixed_size(batch_size=100) as batch:
    for i in range(100):
        properties_dict = {
            'col1': "test1",
            'col2': "test2"
        }

        batch.add_object(
            collection="Test",
            properties=properties_dict,
            vector=[0, 1, 2]
        )

```

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [June 7, 2024, 6:36pm UTC](https://forum.weaviate.io/t/collection-does-not-exist-exception-in-python-v4/2518/4 "2024-06-07T18:36:58Z")

</div>

hi @ggapac !!

Sorry for the delay here.

But can you see the objects stored?

here a code with that:

```auto
import weaviate
import os
from weaviate import classes as wvc
client = weaviate.connect_to_local()
client.collections.delete("Test")

client.collections.create(
    "Test",
    vectorizer_config=wvc.config.Configure.Vectorizer.none()
)

test_col = client.collections.get("Test")

with client.batch.fixed_size(batch_size=100) as batch:
    for i in range(100):
        properties_dict = {
            'col1': "test1",
            'col2': "test2"
        }

        batch.add_object(
            collection="Test",
            properties={"text": "1"},
            vector=[0, 1, 2]
        )
print(client.batch.failed_objects)
# no fail

print(test_col.query.fetch_objects().objects[0:5])

```

Let me know if this helps!

Thanks!

---

<div class="post-metadata">

**Author:** ![ggapac](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/ggapac/32/1282_2.png) [@ggapac](https://forum.weaviate.io/u/ggapac)\
**Post date:** [June 10, 2024, 11:19am UTC](https://forum.weaviate.io/t/collection-does-not-exist-exception-in-python-v4/2518/5 "2024-06-10T11:19:52Z")

</div>

Hi, this is not exactly what I am after. Let me explain my use case better.

I have different functions that take care of data insertion, record updates etc. The collection name is passed to these functions as a parameter. Rather than checking if a collection exists _every single time_ I want to interact with the database, I would expect there is an exception that you can catch in this scenario. Let me illustrate my current and preferred insert function workflows in the following pseudocode:

```auto
current_insert_function(client, collection_name):
    if collection named collection_name exists:
         insert into collection_name

preferred_insert_function(client, collection_name):
    try:
         insert into collection_name
    catch if collection named collection_name does not exist:
        create collection
        repeat the process

```

I was just hoping that Weaviate has a way of throwing an exception if you are trying to do something with a non-existent collection, but with the bulk insert code I posted above, this is unfortunately not the case.

---

<div class="post-metadata">

**Author:** ![DudaNogueira](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dudanogueira/32/7846_2.png) [@DudaNogueira](https://forum.weaviate.io/u/DudaNogueira)\
**Post date:** [June 10, 2024, 9:07pm UTC](https://forum.weaviate.io/t/collection-does-not-exist-exception-in-python-v4/2518/6 "2024-06-10T21:07:20Z")

</div>

Oh, I see!!

You are right, this should raise some error.

Otherwise a lot of users can get confused, as it never raised no errors whatsoever.

I have opened an issue here that is somewhat related (on improving batch error handling):

> <https://github.com/weaviate/weaviate/issues/4499>
>
> \### How to reproduce this bug?
> 
> Run this:
> 
> \`\`\`python
> import weaviate
> client …= weaviate.connect\_to\_local()
> client.collections.delete("MyCollection")
> collection = client.collections.create("MyCollection")
> content = \[
> {"name": "Duda", "vector":\[1,2,3,4,5\]},
> {"name": "Bob", "vector":\[1,2,3,4,5,6,7,8,9,10\]},
> {"name": "Byron", "vector":\[1,2,3,4,5,6\]}
> \]
> with collection.batch.dynamic() as batch:
> for data\_row in content:
> batch.add\_object(
> properties={"name": data\_row\["name"\]},
> vector=data\_row\["vector"\]
> )
> if client.batch.failed\_objects:
> print(client.batch.failed\_objects)
> 
> for i in collection.query.fetch\_objects(include\_vector=True).objects:
> print(i.properties, i.vector)
> \`\`\`
> output:
> 
> \> {'name': 'Duda'} {'default': \[1.0, 2.0, 3.0, 4.0, 5.0\]}
> \>{'name': 'Bob'} {'default': \[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0\]}
> \>{'name': 'Byron'} {'default': \[1.0, 2.0, 3.0, 4.0, 5.0, 6.0\]}
> 
> \### What is the expected behavior?
> 
> That code should fail, as we have a dimensions mismatch, for example, this will fail:
> \`\`\`python
> collection.data.insert({"name": "Parker"}, vector=\[1,2,3,4,5,6,7\])
> \`\`\`
> 
> \> UnexpectedStatusCodeError: Object was not added! Unexpected status code: 500, with response body: {'error': \[{'message': 'put object: import into index mycollection: put local object: shard="z20cFOKQ5NNK": Validate vector index for 8fb4d727-1628-4a2d-a31f-44769fb6a108: new node has a vector with length 7. Existing nodes have vectors with length 5'}\]}.
> PS: the vector used as validation will change for each run.
> 
> 
> \### What is the actual behavior?
> 
> The objects get ingested, and have mismatched dimensional vectors that can lead to unexpected behaviors
> 
> \### Supporting information
> 
> \_No response\_
> 
> \### Server Version
> 
> 1.24.3
> 
> \### Code of Conduct
> 
> \- \[X\] I have read and agree to the Weaviate's \[Contributor Guide\](https://weaviate.io/developers/contributor-guide) and \[Code of Conduct\](https://weaviate.io/service/code-of-conduct)

I will link to this thread so we address that when the time comes.

Thanks!
