使用Google Cloud DocumentAI批量处理文档时遇无效参数错误
文档AI批量处理请求参数错误排查
我在使用Google Document AI进行文档批量处理时遇到问题:使用相同凭据调用client.processDocument()可正常运行,但调用batchProcessDocuments()时返回无效参数错误。已参考官方示例构建请求,但未找到批量处理请求类型的有效解释,需排查问题原因。
代码示例
const { DocumentProcessorServiceClient } = require("@google-cloud/documentai").v1; const client = new DocumentProcessorServiceClient() const name = `projects/${projectId}/locations/${location}/processors/${processorId}`; const request = { name, inputDocuments: { gcsDocuments: { documents: [ { gcsUri: 'gs://${bucketName}/1691859276860-958070983.pdf', mimeType: 'application/pdf' } ] }, }, documentOutputConfig: { gcsOutputConfig: { gcsUri: `${bucketName}/${gcsOutputUri}/${gcsOutputUriPrefix}`, }, }, }; // Batch process document using a long-running operation. // You can wait for now, or get results later. // Note: first request to the service takes longer than subsequent requests. const [operation] = await client.batchProcessDocuments(request);
错误信息
Error processing files: Error: 3 INVALID_ARGUMENT: Request contains an invalid argument.
排查方向
- GCS输出路径格式错误:
gcsOutputConfig.gcsUri缺少gs://前缀,正确格式应为gs://${bucketName}/${gcsOutputUri}/${gcsOutputUriPrefix},当前写法会导致路径无效。 - GCS输入路径模板解析失败:
gcsUri使用单引号包裹,模板变量${bucketName}无法被解析,需改为反引号:gcsUri:gs://${bucketName}/1691859276860-958070983.pdf``,否则路径会被当作字面量处理,无法指向真实文件。 - 存储桶权限问题:确认服务账号拥有目标GCS桶的写入权限,批量处理可能需要比单文档处理更多的存储操作权限(如创建目录、写入多个对象)。
- 处理器模式兼容性:验证所使用的Document AI处理器是否支持批量处理模式,部分特殊处理器仅支持单文档处理。
内容的提问来源于stack exchange,提问作者DMOON
相关产品推荐
相关产品推荐

