# TypeError：文本输入必须是 `str`（单个示例）、`List[str]`（批次）或 `List[List[str]]`（序列批次）类型。收到类型：<class 'int'>

- **ID:** `huggingface/tokenizer-typeerror-nonstring`
- **领域:** huggingface
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

分词器接收到了非文本输入类型（例如整数、浮点数），而不是预期的字符串类型。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| transformers>=4.20.0 | active | — | — |

## 解决方案

1. ```
   在分词前将整数输入转换为字符串：对于单个整数使用 `tokenizer(str(123))`，对于整数列表使用 `tokenizer([str(x) for x in [1, 2, 3]])`。
   ```
2. ```
   确保数据管道输出字符串类型。如果使用数据集，通过 `dataset = dataset.map(lambda x: {'text': str(x['label'])})` 进行映射转换列。
   ```

## 无效尝试

- **** — While `str(123)` works for a single int, if the input is a list of ints like `[1, 2, 3]`, `str([1,2,3])` produces a single string '[1, 2, 3]' which the tokenizer will tokenize incorrectly as a single sequence of characters, not as three separate tokens. (70% 失败率)
- **** — `tokenizer.encode` also performs type checking and will raise the same error for non-string inputs. It is not a bypass. (95% 失败率)
