Azure Document Intelligence预构建布局(Prebuilt-layout)使用KeyValuePairs特性时出现无效参数错误的求助
嘿,我来帮你捋清楚这个问题~
你遇到的这个InvalidArgument错误,核心原因其实是预构建布局(prebuilt-layout)模型本身不支持KeyValuePairs这个特性。虽然DocumentAnalysisFeature枚举里确实有这个选项,但它并不是给布局模型用的——布局模型的定位是提取文档的结构化信息(比如段落、表格、页码、字体样式这些),而键值对提取是预构建表单(prebuilt-form)模型或者自定义表单模型的能力范畴。
你测试过的Barcodes、Formulas、Languages这些特性,刚好都在布局模型的支持列表里,所以调用没问题;但KeyValuePairs不在其中,服务器自然就返回了参数无效的错误。
解决方案
如果你的需求是提取文档里的键值对,建议你把模型切换为prebuilt-form,修改后的代码如下:
AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-form", fileUri, new AnalyzeDocumentOptions() { Features = { DocumentAnalysisFeature.KeyValuePairs } });
如果同时需要布局信息和键值对,你有两种选择:
- 分两次调用:先调用
prebuilt-layout获取布局结构,再调用prebuilt-form提取键值对 - 使用组合模型(比如
prebuilt-document,这个模型整合了布局、表单、OCR等多种能力,原生支持KeyValuePairs特性)
你提供的错误信息和代码参考
错误信息
Azure.RequestFailedException: 'Invalid argument. Status: 400 (Bad Request) ErrorCode: InvalidArgument Content: {"error":{"code":"InvalidArgument","message":"Invalid argument.","innererror":{"code":"InvalidParameter","message":"The parameter keyValuePairs is invalid: The feature is invalid or not supported."}}}
原问题代码
AnalyzeDocumentOperation operation = await client.AnalyzeDocumentFromUriAsync(WaitUntil.Completed, "prebuilt-layout", fileUri, new AnalyzeDocumentOptions() { Features = { DocumentAnalysisFeature.KeyValuePairs } });
你测试过的有效特性代码
AnalyzeDocumentOptions options = new AnalyzeDocumentOptions(); options.Features.Add(DocumentAnalysisFeature.Barcodes); options.Features.Add(DocumentAnalysisFeature.Formulas); options.Features.Add(DocumentAnalysisFeature.Languages); options.Features.Add(DocumentAnalysisFeature.FontStyling); options.Features.Add(DocumentAnalysisFeature.OcrHighResolution);
备注:内容来源于stack exchange,提问作者Thierno Abdul Balde

