# Map with batch=True gives ArrowInvalid error for mismatch in a column's expected length

**URL:** <https://discuss.huggingface.co/t/map-with-batch-true-gives-arrowinvalid-error-for-mismatch-in-a-columns-expected-length/44983>\
**Category:** 🤗Datasets\
**Created:** [June 29, 2023, 4:36pm UTC](https://discuss.huggingface.co/t/map-with-batch-true-gives-arrowinvalid-error-for-mismatch-in-a-columns-expected-length/44983 "2023-06-29T16:36:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SMMousavi](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/smmousavi/32/2314_2.png) [@SMMousavi](https://discuss.huggingface.co/u/SMMousavi)\
**Post date:** [June 29, 2023, 4:36pm UTC](https://discuss.huggingface.co/t/map-with-batch-true-gives-arrowinvalid-error-for-mismatch-in-a-columns-expected-length/44983/1 "2023-06-29T16:36:16Z")

</div>

I am tokenizing my dataset with a customized `tokenize_function` to tokenize 2 different texts and then append them toghether, this is the code:

```python
# Load the datasets
data_files = {
    "train": "train_pair.csv",
    "test": "test_pair.csv",
    "val": "val_pair.csv"
}
datasets = load_dataset('csv', data_files=data_files)

# tokenize the dataset
def tokenize_function(batch):
    # Get the maximum length from the model configuration
    max_length = 512

    # Tokenize each text separately and truncate to half the maximum length
    tokenized_text1 = tokenizer(batch['text1'], truncation=True, max_length=int(max_length/2), add_special_tokens=True)
    tokenized_text2 = tokenizer(batch['text2'], truncation=True, max_length=int(max_length/2), add_special_tokens=True)

    # Merge the results
    tokenized_inputs = {
        'input_ids': tokenized_text1['input_ids'] + tokenized_text2['input_ids'][1:], # exclude the [CLS] token from the second sequence
        'attention_mask': tokenized_text1['attention_mask'] + tokenized_text2['attention_mask'][1:]
    }
    return tokenized_inputs

# Tokenize the datasets
tokenized_datasets = datasets.map(tokenize_function, batched=True)

```

This code is generating this error:

```auto
ArrowInvalid: Column 3 named input_ids expected length 1000 but got length 1999

```

The error is misleading, it suggests that the `input_ids` length is 1999, while it is impossible for the maximum length of this column to be more than `512`. If I set `batch=False` there is no error.  
I also tried with different batch sizes such as 8 or 25 (cause the number of samples is dividable by 25) but it did not work.

---

<div class="post-metadata">

**Author:** ![Dnsibu](https://avatars.discourse-cdn.com/v4/letter/d/f1d935/32.png) [@Dnsibu](https://discuss.huggingface.co/u/Dnsibu)\
**Post date:** [December 12, 2023, 8:08pm UTC](https://discuss.huggingface.co/t/map-with-batch-true-gives-arrowinvalid-error-for-mismatch-in-a-columns-expected-length/44983/2 "2023-12-12T20:08:57Z")

</div>

@SMMousavi did you solve this issue? If so , could you detail it below.
