# OSError: 反序列化头部时出错：HeaderTooLarge

- **ID:** `huggingface/cache-corruption-safetensors`
- **领域:** huggingface
- **类别:** system_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

Hugging Face缓存中的safetensors文件损坏，通常由下载不完整、磁盘写入错误或多个进程并发访问导致。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| safetensors 0.4.0 | active | — | — |
| transformers 4.42.0 | active | — | — |
| huggingface-hub 0.23.0 | active | — | — |

## 解决方案

1. ```
   清除特定模型的Hugging Face缓存并重新下载：

import shutil
from pathlib import Path
cache_dir = Path.home() / '.cache' / 'huggingface' / 'hub'
# 找到并删除损坏的模型文件夹（例如 models--bert-base-uncased）
shutil.rmtree(cache_dir / 'models--bert-base-uncased')
# 然后重新加载模型
from transformers import AutoModel
model = AutoModel.from_pretrained('bert-base-uncased')
   ```
2. ```
   使用 `huggingface-cli` 从缓存中删除模型：`huggingface-cli delete-cache` 并选择损坏的模型，然后使用 `from_pretrained` 重新下载。
   ```

## 无效尝试

- **** — The file on disk is corrupted, not the library; reinstalling doesn't fix the cache. (95% 失败率)
- **** — The error is about a corrupted file header, not resource exhaustion. (90% 失败率)
