快速结论:该报错通常出现在运行 Transformer 课程 Notebook 中 NER(命名实体识别)Pipeline 示例时,原因是 grouped_entities 参数已弃用。优先将参数替换为 aggregation_strategy="simple" 即可解决。
适用环境:Hugging Face Transformers(Python 3.12 环境);已知受影响场景为 huggingface/notebooks 仓库中的课程第 3 节 Notebook(第 17 个单元格)。
最快修复方案:将 pipeline("ner", grouped_entities=True) 改为 pipeline("ner", aggregation_strategy="simple")——这是 Issue 评论中明确验证过的修复方式。
注意事项:如果代码中同时使用了旧版 Transformers,可能需要同步升级库版本;此外,huggingface/notebooks 仓库中可能还有其他位置引用了 grouped_entities,需要一并清理。
问题场景
用户在运行 Hugging Face LLM Course(Hugging Face 课程)第 3 节《Transformers, what can they do?》的 Jupyter Notebook 时,在第 17 个单元格执行 NER Pipeline 示例代码而触发报错。触发代码为:
from transformers import pipeline
ner = pipeline("ner", grouped_entities=True)
ner("My name is Sylvain and I work at Hugging Face in Brooklyn.")
报错原文
TypeError: TokenClassificationPipeline._sanitize_parameters() got an unexpected keyword argument 'grouped_entities'
原因分析
核心原因非常明确:grouped_entities 参数已在 Transformers 库中被弃用,其功能被 aggregation_strategy 参数替代。旧版示例代码(课程 Notebook 编写较早)仍使用旧参数,导致当前版本 Transformers 无法识别并抛出 TypeError。Issue 评论明确指出:“The grouped_entities parameter was deprecated in favor of aggregation_strategy“。该问题属于 Notebook 示例未跟上库更新,而非用户代码错误逻辑。Issue 作者提交了对 Transformers 仓库的 PR 进行修复,同时社区成员也在 huggingface/notebooks 仓库提交了 PR(huggingface/notebooks#617)修正课程代码。
环境排查
- Transformers 版本:确认是否为较新版本(报错环境为 Python 3.12 + dist-packages,推断为较新版本)。旧版本 Transformers 可能仍接受
grouped_entities参数,建议检查transformers.__version__。 - 代码来源:确认 Notebook 是否来自
huggingface/notebooks仓库(如transformers_doc/en/pipeline_tutorial.ipynb或课程对应文件),旧版文件可能包含已弃用参数。 - Python 环境:确认 Python 版本(3.12)及 Transformers 安装路径(3.12 dist-packages)无误。
解决步骤
- 打开 Jupyter Notebook,定位到第 17 个单元格(或任何包含
pipeline("ner", grouped_entities=True)的单元格)。 - 将
grouped_entities=True替换为aggregation_strategy="simple"。修改后的代码为:from transformers import pipeline ner = pipeline("ner", aggregation_strategy="simple") ner("My name is Sylvain and I work at Hugging Face in Brooklyn.") - 如果代码中还有其他地方使用了
grouped_entities(例如在文本分类或其他 Pipeline 中),一并搜索并替换为对应的aggregation_strategy选项(如"simple"、"first"或"average")。 - 重新运行单元格,确认报错消失。
- (可选)如果 Notebook 文件来源为
huggingface/notebooks仓库,可直接拉取最新版本文件,或者手动检查仓库中是否有其他grouped_entities引用需清理(Issue 评论中招募贡献者进行此类清理)。
验证方法
重新执行修改后的单元格(或整个 Notebook),确认不再抛出 TypeError。正确运行时,NER Pipeline 应输出类似“My name is Sylvain and I work at Hugging Face in Brooklyn.”的实体识别结果(如识别出人名“Sylvain”和组织“Hugging Face”),且不出现任何异常堆栈信息。
参考来源
huggingface/transformers #44016
AI 工具推荐
想把多个 AI 模型放在一个入口?
GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。
推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。
这个方案解决了吗?
可以继续搜索完整报错,或查看同一工具的其他排查指南。

![[Bug] Gmail trigger silently stops after 7 days: subscription expires_at is persisted as -1](https://www.chat-gpts.plus/wp-content/uploads/2026/09/41162-d3216684-768x403.jpg)
