Fix WebDatasets KeyError for user-defined Features when a field is missing in an example
State: closed

Fixes: https://github.com/huggingface/datasets/issues/6900

Not sure if this needs any addition stuff before merging

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/datasets/pr_7004). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

<details>
<summary>Show benchmarks</summary>

PyArrow==8.0.0

<details>
<summary>Show updated benchmarks!</summary>

### Benchmark: benchmark_array_xd.json

| metric | read_batch_formatted_as_numpy after write_array2d | read_batch_formatted_as_numpy after write_flattened_sequence | read_batch_formatted_as_numpy after write_nested_sequence | read_batch_unformated after write_array2d | read_batch_unformated after write_flattened_sequence | read_batch_unformated after write_nested_sequence | read_col_formatted_as_numpy after write_array2d | read_col_formatted_as_numpy after write_flattened_sequence | read_col_formatted_as_numpy after write_nested_sequence | read_col_unformated after write_array2d | read_col_unformated after write_flattened_sequence | read_col_unformated after write_nested_sequence | read_formatted_as_numpy after write_array2d | read_formatted_as_numpy after write_flattened_sequence | read_formatted_as_numpy after write_nested_sequence | read_unformated after write_array2d | read_unformated after write_flattened_sequence | read_unformated after write_nested_sequence | write_array2d | write_flattened_sequence | write_nested_sequence |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.005188 / 0.011353 (-0.006165) | 0.003812 / 0.011008 (-0.007196) | 0.062408 / 0.038508 (0.023900) | 0.030734 / 0.023109 (0.007625) | 0.236528 / 0.275898 (-0.039370) | 0.267684 / 0.323480 (-0.055796) | 0.003182 / 0.007986 (-0.004804) | 0.004009 / 0.004328 (-0.000319) | 0.048966 / 0.004250 (0.044715) | 0.045259 / 0.037052 (0.008207) | 0.250586 / 0.258489 (-0.007903) | 0.287079 / 0.293841 (-0.006762) | 0.029235 / 0.128546 (-0.099311) | 0.012216 / 0.075646 (-0.063431) | 0.203864 / 0.419271 (-0.215408) | 0.036324 / 0.043533 (-0.007209) | 0.245180 / 0.255139 (-0.009959) | 0.270327 / 0.283200 (-0.012872) | 0.018676 / 0.141683 (-0.123007) | 1.115568 / 1.452155 (-0.336586) | 1.183267 / 1.492716 (-0.309449) |

### Benchmark: benchmark_getitem\_100B.json

| metric | get_batch_of\_1024\_random_rows | get_batch_of\_1024\_rows | get_first_row | get_last_row |
|--------|---|---|---|---|
| new / old (diff) | 0.094307 / 0.018006 (0.076301) | 0.299071 / 0.000490 (0.298581) | 0.000219 / 0.000200 (0.000019) | 0.000042 / 0.000054 (-0.000012) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.018336 / 0.037411 (-0.019076) | 0.062973 / 0.014526 (0.048447) | 0.074137 / 0.176557 (-0.102420) | 0.120553 / 0.737135 (-0.616582) | 0.075411 / 0.296338 (-0.220927) |

### Benchmark: benchmark_iterating.json

| metric | read 5000 | read 50000 | read_batch 50000 10 | read_batch 50000 100 | read_batch 50000 1000 | read_formatted numpy 5000 | read_formatted pandas 5000 | read_formatted tensorflow 5000 | read_formatted torch 5000 | read_formatted_batch numpy 5000 10 | read_formatted_batch numpy 5000 1000 | shuffled read 5000 | shuffled read 50000 | shuffled read_batch 50000 10 | shuffled read_batch 50000 100 | shuffled read_batch 50000 1000 | shuffled read_formatted numpy 5000 | shuffled read_formatted_batch numpy 5000 10 | shuffled read_formatted_batch numpy 5000 1000 |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.284751 / 0.215209 (0.069542) | 2.789294 / 2.077655 (0.711640) | 1.457789 / 1.504120 (-0.046331) | 1.339140 / 1.541195 (-0.202055) | 1.341685 / 1.468490 (-0.126805) | 0.714928 / 4.584777 (-3.869849) | 2.361197 / 3.745712 (-1.384516) | 2.791569 / 5.269862 (-2.478293) | 1.892261 / 4.565676 (-2.673416) | 0.077954 / 0.424275 (-0.346321) | 0.005454 / 0.007607 (-0.002153) | 0.350766 / 0.226044 (0.124721) | 3.416749 / 2.268929 (1.147820) | 1.835377 / 55.444624 (-53.609247) | 1.506456 / 6.876477 (-5.370020) | 1.642728 / 2.142072 (-0.499344) | 0.791543 / 4.805227 (-4.013684) | 0.133102 / 6.500664 (-6.367562) | 0.042572 / 0.075469 (-0.032897) |

### Benchmark: benchmark_map_filter.json

| metric | filter | map fast-tokenizer batched | map identity | map identity batched | map no-op batched | map no-op batched numpy | map no-op batched pandas | map no-op batched pytorch | map no-op batched tensorflow |
|--------|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.977958 / 1.841788 (-0.863830) | 11.438271 / 8.074308 (3.363963) | 9.305719 / 10.191392 (-0.885673) | 0.141239 / 0.680424 (-0.539185) | 0.014330 / 0.534201 (-0.519871) | 0.302201 / 0.579283 (-0.277082) | 0.261688 / 0.434364 (-0.172676) | 0.338752 / 0.540337 (-0.201586) | 0.468466 / 1.386936 (-0.918470) |

</details>
PyArrow==latest

<details>
<summary>Show updated benchmarks!</summary>

### Benchmark: benchmark_array_xd.json

| metric | read_batch_formatted_as_numpy after write_array2d | read_batch_formatted_as_numpy after write_flattened_sequence | read_batch_formatted_as_numpy after write_nested_sequence | read_batch_unformated after write_array2d | read_batch_unformated after write_flattened_sequence | read_batch_unformated after write_nested_sequence | read_col_formatted_as_numpy after write_array2d | read_col_formatted_as_numpy after write_flattened_sequence | read_col_formatted_as_numpy after write_nested_sequence | read_col_unformated after write_array2d | read_col_unformated after write_flattened_sequence | read_col_unformated after write_nested_sequence | read_formatted_as_numpy after write_array2d | read_formatted_as_numpy after write_flattened_sequence | read_formatted_as_numpy after write_nested_sequence | read_unformated after write_array2d | read_unformated after write_flattened_sequence | read_unformated after write_nested_sequence | write_array2d | write_flattened_sequence | write_nested_sequence |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.005629 / 0.011353 (-0.005723) | 0.003997 / 0.011008 (-0.007011) | 0.050447 / 0.038508 (0.011939) | 0.031694 / 0.023109 (0.008585) | 0.277392 / 0.275898 (0.001494) | 0.290440 / 0.323480 (-0.033040) | 0.004403 / 0.007986 (-0.003583) | 0.002851 / 0.004328 (-0.001478) | 0.048593 / 0.004250 (0.044343) | 0.040622 / 0.037052 (0.003570) | 0.282640 / 0.258489 (0.024151) | 0.309390 / 0.293841 (0.015549) | 0.031453 / 0.128546 (-0.097094) | 0.012424 / 0.075646 (-0.063223) | 0.060311 / 0.419271 (-0.358960) | 0.033195 / 0.043533 (-0.010338) | 0.266867 / 0.255139 (0.011728) | 0.281966 / 0.283200 (-0.001234) | 0.018026 / 0.141683 (-0.123657) | 1.136273 / 1.452155 (-0.315882) | 1.141643 / 1.492716 (-0.351073) |

### Benchmark: benchmark_getitem\_100B.json

| metric | get_batch_of\_1024\_random_rows | get_batch_of\_1024\_rows | get_first_row | get_last_row |
|--------|---|---|---|---|
| new / old (diff) | 0.095011 / 0.018006 (0.077005) | 0.300571 / 0.000490 (0.300082) | 0.000220 / 0.000200 (0.000020) | 0.000044 / 0.000054 (-0.000011) |

### Benchmark: benchmark_indices_mapping.json

| metric | select | shard | shuffle | sort | train_test_split |
|--------|---|---|---|---|---|
| new / old (diff) | 0.022903 / 0.037411 (-0.014508) | 0.077130 / 0.014526 (0.062604) | 0.089576 / 0.176557 (-0.086980) | 0.127156 / 0.737135 (-0.609980) | 0.090008 / 0.296338 (-0.206331) |

### Benchmark: benchmark_iterating.json

| metric | read 5000 | read 50000 | read_batch 50000 10 | read_batch 50000 100 | read_batch 50000 1000 | read_formatted numpy 5000 | read_formatted pandas 5000 | read_formatted tensorflow 5000 | read_formatted torch 5000 | read_formatted_batch numpy 5000 10 | read_formatted_batch numpy 5000 1000 | shuffled read 5000 | shuffled read 50000 | shuffled read_batch 50000 10 | shuffled read_batch 50000 100 | shuffled read_batch 50000 1000 | shuffled read_formatted numpy 5000 | shuffled read_formatted_batch numpy 5000 10 | shuffled read_formatted_batch numpy 5000 1000 |
|--------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 0.289270 / 0.215209 (0.074061) | 2.848239 / 2.077655 (0.770585) | 1.546788 / 1.504120 (0.042668) | 1.417275 / 1.541195 (-0.123920) | 1.456214 / 1.468490 (-0.012276) | 0.716688 / 4.584777 (-3.868088) | 0.940242 / 3.745712 (-2.805470) | 2.911956 / 5.269862 (-2.357906) | 1.871358 / 4.565676 (-2.694318) | 0.077553 / 0.424275 (-0.346722) | 0.005279 / 0.007607 (-0.002328) | 0.343338 / 0.226044 (0.117294) | 3.368694 / 2.268929 (1.099766) | 1.896765 / 55.444624 (-53.547859) | 1.612414 / 6.876477 (-5.264063) | 1.615934 / 2.142072 (-0.526138) | 0.794016 / 4.805227 (-4.011212) | 0.131821 / 6.500664 (-6.368843) | 0.041495 / 0.075469 (-0.033975) |

### Benchmark: benchmark_map_filter.json

| metric | filter | map fast-tokenizer batched | map identity | map identity batched | map no-op batched | map no-op batched numpy | map no-op batched pandas | map no-op batched pytorch | map no-op batched tensorflow |
|--------|---|---|---|---|---|---|---|---|---|
| new / old (diff) | 1.003418 / 1.841788 (-0.838370) | 12.073906 / 8.074308 (3.999598) | 10.166291 / 10.191392 (-0.025101) | 0.131224 / 0.680424 (-0.549200) | 0.015246 / 0.534201 (-0.518955) | 0.299835 / 0.579283 (-0.279448) | 0.124308 / 0.434364 (-0.310056) | 0.336414 / 0.540337 (-0.203924) | 0.429569 / 1.386936 (-0.957367) |

</details>
</details>

![](https://cml.dev/watermark.png#83d28601fad73755b74314a9bc1e327005514d54 "CML watermark")


@lhoestq Thank you!