# Saving outcomes if Error while applying map function on dataset

**URL:** <https://discuss.huggingface.co/t/saving-outcomes-if-error-while-applying-map-function-on-dataset/31614>\
**Category:** 🤗Datasets\
**Created:** [February 13, 2023, 1:47pm UTC](https://discuss.huggingface.co/t/saving-outcomes-if-error-while-applying-map-function-on-dataset/31614 "2023-02-13T13:47:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hebkon](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@hebkon](https://discuss.huggingface.co/u/hebkon)\
**Post date:** [February 13, 2023, 1:47pm UTC](https://discuss.huggingface.co/t/saving-outcomes-if-error-while-applying-map-function-on-dataset/31614/1 "2023-02-13T13:47:17Z")

</div>

I use an API (like huggingface\_hub) to let a language model answer questions from my dataset.  
Since I want to send every single example to the language model, I wrote a function that does that and then use the map function to map this API call to every example of my dataset.

My issue is: If there is an Error at any point (e.g. the API throws an Error after one hour, because sth happend) I loose all the information. Lets say the map worked for the first 100 examples and then the API throws an Error, I loose the generated data of all the 100 examples that were handled correctly before the error happened.

Is there a way to let the map function return all the items it already finished, when an Error occurs. I do not know how to access this information and always have to start from new…

---

<div class="post-metadata">

**Author:** ![mariosasko](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/mariosasko/32/31548_2.png) [@mariosasko](https://discuss.huggingface.co/u/mariosasko)\
**Post date:** [February 13, 2023, 3:43pm UTC](https://discuss.huggingface.co/t/saving-outcomes-if-error-while-applying-map-function-on-dataset/31614/2 "2023-02-13T15:43:38Z")

</div>

Hi! You can use `try/except` to catch the error and return `None` to avoid interrupting the processing procedure:

```python

def get_api_response(ex):
    try:
        # API call...
        return {"response": "..."} 
    except Exception:
        return {"response": None}

dataset.map(get_api_response)

```

---

<div class="post-metadata">

**Author:** ![hebkon](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@hebkon](https://discuss.huggingface.co/u/hebkon)\
**Post date:** [February 14, 2023, 11:21am UTC](https://discuss.huggingface.co/t/saving-outcomes-if-error-while-applying-map-function-on-dataset/31614/3 "2023-02-14T11:21:30Z")

</div>

Thanks for that solution. I was already thinking about doing sth like that, but I thought the map function might have an option for doing it automatically…  
But as you show it is very easy to implement on my own 🙂
