Says collection exist on insertion even

Hi I have already created collections but when I try to insert add object it says class already exist why so?

my code

collection_name = f"Collection_{request.collectionId.replace('-', '')}"
        data_row = {
            "filename": request.id,
            "filecontent": request.text,
        }
        print(f"Passing file : {request.id}")
        collection = client.collections.get(collection_name)
        with collection.batch.dynamic() as batch:
            batch.add_object(properties=data_row)
        if len(collection.batch.failed_objects) > 0:
            print(f"Failed to add objects: {collection.batch.failed_objects}")
            return {
                "status": "error",
                "message": f"Failed to add objects: {collection.batch.failed_objects}",
            }

error I am having

“message”: “Failed to add objects: [ErrorObject(message=‘WeaviateInsertManyAllFailedError(\‘Every object failed during insertion. Here is the set of all errors: class already exists: found similar class "Collection_83cdb7b2569a428095e38b73e8348168"\’)’, object_=_BatchObject(collection=‘Collection_83CDB7B2569A428095E38B73E8348168’, vector=None, uuid=‘dbcf23f4-bdbc-46b1-bc2f-5398eaf75dd4’, properties={‘filename’: ‘B7139300-9CC7-45F0-8096-we’, ‘filecontent’: ‘Saasdfadfs 234 234iddrsdf asdfade!’}, tenant=None, references=None, index=0, retry_count=0), original_uuid=None)]”

creating collections like this

collection_name = 'Collection_' + request.id.replace('-', '')
        client.collections.create(
            collection_name,
            vectorizer_config=[
                Configure.NamedVectors.text2vec_transformers(
                    name="filename", source_properties=["filecontent"]
                )
            ],
        )

Good morning @Muhammad_Ashir,

I hope you’re having a great day!

Looking at the error, it seems there might be an issue in the logic, as you’re attempting to insert the same class again.

I recommend using the exists() function in the client to handle this dynamically.

col = client.collections.get("<COL_NAME>").exists()
if col:
    print("Collection exists")
else:
    print("Collection does not exist")

This way, you can check if the collection already exists before trying to create or insert data into it.

Additionally, there is check on objects which can be beneficial for you:

<VAR> = <COL_NAME>.data.exists(object_uuid)

Hope this helps!

Regards,
Mohamed Shahin,
Weaviate Support Engineer