Azure Document Intelligence出现TypeError错误的原因与解决办法咨询
Azure Document Intelligence出现TypeError错误的原因与解决办法咨询
看起来你遇到的这个TypeError是因为Azure Document Intelligence SDK版本的参数变化导致的,我来帮你一步步解决:
错误原因
你当前使用的Azure Document Intelligence Python SDK应该是v4.0及以上的新版本,而你参考的代码是基于旧版本SDK编写的。在新版本中,begin_analyze_document方法的参数名从body改成了document,所以传递body=f会触发这个"不识别参数"的错误。
解决办法
1. 确认SDK版本
先执行以下命令检查你安装的SDK版本:
pip show azure-ai-documentintelligence
如果版本号是4.0.0或更高,就需要按照新版本的参数规范修改代码。
2. 修改代码中的核心参数
把原代码里的body=f替换成document=f,这是修复错误的关键。另外我也补充了代码中遗漏的os模块导入(因为用到了os.environ),以下是完整的修正后代码:
from azure.core.credentials import AzureKeyCredential from azure.ai.documentintelligence import DocumentIntelligenceClient from azure.ai.documentintelligence.models import AnalyzeOutputOption, AnalyzeResult import os # 补充导入os模块 endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"] key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"] document_intelligence_client = DocumentIntelligenceClient(endpoint=endpoint, credential=AzureKeyCredential(key)) with open(path_to_sample_documents, "rb") as f: poller = document_intelligence_client.begin_analyze_document( "prebuilt-read", document=f, # 关键修改:将body替换为document output=[AnalyzeOutputOption.PDF], ) result: AnalyzeResult = poller.result() operation_id = poller.details["operation_id"] # 新版本中获取PDF结果的逻辑保持不变 response = document_intelligence_client.get_analyze_result_pdf(model_id=result.model_id, result_id=operation_id) with open("analyze_result.pdf", "wb") as writer: writer.writelines(response)
3. 额外环境检查(可选)
如果你之前安装过旧版的azure-ai-formrecognizer(Document Intelligence的前身SDK),建议先卸载再升级到新版:
pip uninstall -y azure-ai-formrecognizer pip install azure-ai-documentintelligence --upgrade
验证修改
运行修正后的代码,应该就能正常发起文档分析请求,不再触发TypeError了。如果还有问题,可以先检查你的endpoint和API密钥是否配置正确,以及目标文件路径是否存在。
备注:内容来源于stack exchange,提问作者Droid-Bird
相关产品推荐
相关产品推荐

