# TypeError: 流式数据集没有已知长度。请仅对非流式数据集使用 `len(dataset)`。

- **ID:** `huggingface/datasets-streaming-iterable-dataset-length-error`
- **领域:** huggingface
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

对流式（Iterable）数据集调用 len()，该数据集由于是惰性加载而不支持长度计算。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| datasets>=2.5.0 | active | — | — |

## 解决方案

1. ```
   Check if the dataset is streaming with `isinstance(dataset, IterableDataset)` before calling len(). Example: `if not isinstance(dataset, IterableDataset): print(len(dataset)) else: print('Length unknown')`
   ```
2. ```
   If you need the length, load the dataset non-streaming only once to get the size, then reload with streaming=True: `length = len(load_dataset('dataset_name', split='train', streaming=False)); dataset = load_dataset('dataset_name', split='train', streaming=True)`
   ```
3. ```
   Use dataset.n_shards if available (for sharded datasets) to estimate length, or rely on the dataset's metadata if provided by the source.
   ```

## 无效尝试

- **** — This defeats the purpose of streaming (memory efficiency) and may cause OOM for large datasets. Also, the dataset might be too large to fit in memory. (70% 失败率)
- **** — These methods also rely on known length and will raise similar errors or return None. (80% 失败率)
- **** — This iterates through the entire dataset, which is slow and defeats streaming benefits; also, for very large datasets it may take hours or cause memory issues. (50% 失败率)
