# Batch thread died unexpectedly

**URL:** <https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056>\
**Category:** Support\
**Tags:** developer-experience\
**Created:** [July 16, 2024, 6:54am UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056 "2024-07-16T06:54:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![aleks](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@aleks](https://forum.weaviate.io/u/aleks)\
**Post date:** [July 16, 2024, 6:54am UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056/1 "2024-07-16T06:54:07Z")

</div>

Environment:

- weaviate 1.25.1
- weaviate client 4.6.4
- fastapi 0.111.0

when using import batching like so:

async def batch\_save(facts: Sequence[CreateWithVector],  
weaviate\_class\_name: str,  
weaviate\_session: WeaviateClient):  
collection = weaviate\_session.collections.get(weaviate\_class\_name)

```
with collection.batch.dynamic() as batch:
    for fact in facts:
        fact_id = uuid.uuid4()
        batch.add_object(
            properties={
                "fact": fact.content,
            },
            vector=fact.vector,
            uuid=fact_id,
        )
        weaviate_uuid_list.append(str(fact_id))

```

there is an exception → batch thread died unexpectedly.

Any help is appreciated! thanks!

---

<div class="post-metadata">

**Author:** ![Shahin](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/shahin/32/7944_2.png) [@Shahin](https://forum.weaviate.io/u/Shahin)\
**Post date:** [July 16, 2024, 9:46am UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056/2 "2024-07-16T09:46:05Z")

</div>

Hi @aleks,

Welcome to our community! it’s great to have you here 🤗

Have you considered to capture more information about this error by implementing error handling and logging during the batch process?

In Weaviate Pythin Client, the following exception handling can help raises various error conditions:

- `weaviate.exceptions.WeaviateConnectionError` for failed connections.
- `weaviate.exceptions.WeaviateQueryError` for failed queries.
- `weaviate.exceptions.WeaviateBatchError` for failed batch operations.
- `weaviate.exceptions.WeaviateClosedClientError` for operations on a closed client.

As an example, to help catch more of what happens during batch import:

> try:  
> collection = client.collections.get(“NonExistentCollection”)  
> collection.query.fetch\_objects(limit=2)  
> except weaviate.exceptions.WeaviateBaseError as e:  
> print(f"Caught a Weaviate error: {e.message}")

You can review [this module](https://github.com/weaviate/weaviate-python-client/blob/main/weaviate/exceptions.py) which defines the exceptions that can be raised by the client library.

Additionally, I would recommend upgrading your Weaviate to the latest v1.25.7 as well. There are improvements since v1.25.1.

### Exception handling →

> **[Python | Weaviate Documentation](https://docs.weaviate.io/weaviate/client-libraries/python)**
>
> Official Python client library documentation for integrating Weaviate with Python applications and services.

### Error Handling →

> **[Python | Weaviate Documentation](https://docs.weaviate.io/weaviate/client-libraries/python)**
>
> Official Python client library documentation for integrating Weaviate with Python applications and services.

---

<div class="post-metadata">

**Author:** ![Dirk](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dirk/32/46_2.png) [@Dirk](https://forum.weaviate.io/u/Dirk)\
**Post date:** [July 16, 2024, 12:45pm UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056/3 "2024-07-16T12:45:41Z")

</div>

> [@aleks](#):
>
> async def batch\_save(facts: Sequence[CreateWithVector],

batching does not support being called within async functions. That is probably why it fails

---

<div class="post-metadata">

**Author:** ![aleks](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@aleks](https://forum.weaviate.io/u/aleks)\
**Post date:** [July 16, 2024, 7:00pm UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056/4 "2024-07-16T19:00:55Z")

</div>

Thanks @Shahin for your help and suggestion, but unluckily the exception thrown is Exception and not the one that you indicated.

As mentioned by @Dirk , batching is not supported in a asynchronous multithreaded environment.

Are there any plans to support it? Is there any workaround?

Any hint would be appreciated.

Thanks so much in advanced.

---

<div class="post-metadata">

**Author:** ![Dirk](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dirk/32/46_2.png) [@Dirk](https://forum.weaviate.io/u/Dirk)\
**Post date:** [July 17, 2024, 7:35am UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056/5 "2024-07-17T07:35:57Z")

</div>

> [@aleks](#):
>
> Are there any plans to support it? Is there any workaround?

Batching is using async behind the scenes already. Adding objects is non-blocking and it automatically sends multiple concurrent requests. We do not think it makes sense to call this async

If you want to do async batching yourself you can use data.insert\_many(). The latest developement version (`4.7.0-rc-2`) also contains an async client taht you could try out.

---

<div class="post-metadata">

**Author:** ![aleks](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@aleks](https://forum.weaviate.io/u/aleks)\
**Post date:** [July 17, 2024, 8:50am UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056/6 "2024-07-17T08:50:17Z")

</div>

@Dirk thanks for your reply and details.  
One aspect that I do not have clear is. You said that batching is not supported being called by async, but in the previous post you commented that batching is using async already behind the scenes. I am confused because I thought batching is not supported in async environments.

Maybe I am missing something 🙂

Thanks for your support and kind regards.

---

<div class="post-metadata">

**Author:** ![Dirk](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dirk/32/46_2.png) [@Dirk](https://forum.weaviate.io/u/Dirk)\
**Post date:** [July 17, 2024, 9:31am UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056/7 "2024-07-17T09:31:39Z")

</div>

> [@aleks](#):
>
> One aspect that I do not have clear is. You said that batching is not supported being called by async, but in the previous post you commented that batching is using async already behind the scenes. I am confused because I thought batching is not supported in async environments.

Our implementation of batch is using async-code to send batches.  
So basically if you call batch.add\_object() you add objects to a queue and then there are background threads that observer that queue and send batches. The internal send\_batch function is async to allow for multiple concurrent requests without threading

---

<div class="post-metadata">

**Author:** ![aleks](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@aleks](https://forum.weaviate.io/u/aleks)\
**Post date:** [July 17, 2024, 9:41am UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056/8 "2024-07-17T09:41:02Z")

</div>

Ok, thanks for the details @Dirk . But then, why does batching not working using async methods using fastapi as web framework?

Thanks again for your support, patience and time. Really appreciated.

Kind regards

---

<div class="post-metadata">

**Author:** ![Dirk](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dirk/32/46_2.png) [@Dirk](https://forum.weaviate.io/u/Dirk)\
**Post date:** [July 17, 2024, 9:59am UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056/9 "2024-07-17T09:59:09Z")

</div>

> But then, why does batching not working using async methods using fastapi as web framework?

I _think_ (but not 100% sure) because we also create event loops inside of the batching

> using fastapi as web framework

I am not an fastapi expert, but our batching algorithm is mainly aimed for long running tasks eg 1000 objects+ and is not threadsafe.

I would recommend to either:

- use data.insert/insert\_many() and build your own async wrapper around them
- install `weaviate-client==4.7.0-rc.2` and test our new async client (please not directly in production 🙂 ) you can then do:

```auto
async with weaviate.use_async_with_local() as async_client
    collection = async_client.collections.get(name)
    await collection.data.insert()/insert_many(....)

```

- give us feedback if anything does not work as expected 🙂

---

<div class="post-metadata">

**Author:** ![aleks](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@aleks](https://forum.weaviate.io/u/aleks)\
**Post date:** [July 19, 2024, 7:03am UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056/10 "2024-07-19T07:03:44Z")

</div>

Hi @Dirk ,  
to my surprise batching is working as expected using fastapi and async methods. The problem was that I was running pycharm in debug mode and in this mode batching, async and event loops are not good friends 🙂

Thanks for your support.

Keep you posted in anything comes up.

Kind regards,

---

<div class="post-metadata">

**Author:** ![Dirk](https://yyz1.discourse-cdn.com/flex027/user_avatar/forum.weaviate.io/dirk/32/46_2.png) [@Dirk](https://forum.weaviate.io/u/Dirk)\
**Post date:** [July 22, 2024, 7:09am UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056/11 "2024-07-22T07:09:53Z")

</div>

> to my surprise batching is working as expected using fastapi and async methods. The problem was that I was running pycharm in debug mode and in this mode batching, async and event loops are not good friends 🙂

I would be careful here - I think often it comes down to timing and the debug mode might just have the “wrong” timing. Meaning anything that changes the timing (different query, weird user input) could bring the error back.

@tsmith023 - you are more experienced with async, what do you think?

---

<div class="post-metadata">

**Author:** ![aleks](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@aleks](https://forum.weaviate.io/u/aleks)\
**Post date:** [July 22, 2024, 7:26am UTC](https://forum.weaviate.io/t/batch-thread-died-unexpectedly/3056/12 "2024-07-22T07:26:30Z")

</div>

Yes, of course @Dirk , we will be watching if it works as expected. Anyway, I think there is a plan to release a Weaviate async client. Do you know when it would be the release?

And lets see what is the opinion of @tsmith023 🙂

Thanks a lot for your help?
