You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase云函数调用Document AI报无效参数错误求助

Firebase云函数调用Document AI报INVALID_ARGUMENT错误排查与修复

核心问题分析

你遇到的3 INVALID_ARGUMENT错误,结合fieldViolations提示,大概率是请求参数格式或内容不符合Document AI的要求,以下是具体排查方向和修复方案:

1. 地域参数使用错误

Document AI处理器的路径中,地域必须是具体的GCP区域代码(如us-central1、europe-west1),而非笼统的国家缩写us。你的代码中name字段的地域部分写的是us,这是无效的。

  • 修复方法:登录GCP控制台找到你的Document AI处理器,查看其完整路径,替换代码中的地域部分。例如:
    name: `projects/sample-room-58c38/locations/us-central1/processor/a89f2d4fd6b09a9a`,
    

2. 硬编码MIME类型不匹配

代码中固定设置mimeType: 'image/jpeg',但如果上传的文件是PNG、PDF等其他格式,就会触发参数错误。

  • 修复方法:从Storage对象的contentType字段动态获取MIME类型:
    inputConfig: {
        gcsDocumentUri: gcsUri,
        mimeType: object.contentType,
    },
    

3. 错误日志未展开细节

当前日志只输出了fieldViolations: [Array],无法看到具体是哪个字段出错,需要修改日志代码展开详细信息:

catch (error) {
    console.error('Error details:', error);
    if (error.statusDetails?.length > 0) {
        console.error('Field violations:', JSON.stringify(error.statusDetails[0].fieldViolations, null, 2));
    }
    throw error;
}

运行后就能看到具体是哪个参数不符合要求,精准定位问题。

4. 权限验证补充

虽然你已核对权限,仍需确认:

  • 服务账号拥有roles/documentai.apiUser角色,允许调用Document AI API
  • 服务账号拥有Cloud Storage的storage.objects.get权限,能读取目标存储桶中的文件

修改后的完整代码示例

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { DocumentProcessorServiceClient } = require('@google-cloud/documentai').v1beta3;

admin.initializeApp({
    credential: admin.credential.cert(require('./sample-room-58c38-bd767e088e9d.json')),
});

const client = new DocumentProcessorServiceClient({
    projectId: 'sample-room-58c38',
    keyFilename: './sample-room-58c38-bd767e088e9d.json',
});

const bucketName = 'sample-room-58c38-q6gtc';

exports.processImage = functions.storage
    .bucket(bucketName)
    .object()
    .onFinalize(async (object) => {
        const gcsUri = `gs://${bucketName}/${object.name}`;
        // 修正地域参数,替换为处理器实际所在的具体区域
        const request = {
            name: `projects/sample-room-58c38/locations/us-central1/processor/a89f2d4fd6b09a9a`,
            inputConfig: {
                gcsDocumentUri: gcsUri,
                // 动态获取文件MIME类型
                mimeType: object.contentType,
            },
        };

        console.log('gcsUri:', gcsUri);
        console.log('Request:', JSON.stringify(request, null, 2));

        try {
            const [result] = await client.processDocument(request);

            const { document } = result;
            const { formFields } = document;
            const data = {};

            formFields.forEach(field => {
                const fieldName = field.fieldName.text;
                const fieldValue = field.fieldValue.text;
                data[fieldName] = fieldValue;
            });

            console.log('Data:', data);
            return admin.firestore().collection('DocumentAI Photos').doc(object.name).set(data);
        } catch (error) {
            // 展开错误细节,方便定位
            console.error('Error in processDocument:', error);
            if (error.statusDetails?.length > 0) {
                console.error('Field violations:', JSON.stringify(error.statusDetails[0].fieldViolations, null, 2));
            }
            throw error;
        }
    });

内容的提问来源于stack exchange,提问作者cristopher blunt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 03:21:07