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

使用C++版Document AI客户端库处理大文件时出现RESOURCE_EXHAUSTED错误的原因及解决方案咨询

C++版Document AI客户端库处理大文件时出现RESOURCE_EXHAUSTED错误的原因及解决方案咨询

针对你遇到的C++版Document AI客户端处理超过4MB文件就报RESOURCE_EXHAUSTED的问题,我来帮你拆解原因并给出可行的解决方案:

问题根源

从错误提示Received message larger than max (5904627 vs. 4194304)就能直接定位问题:C++客户端默认的gRPC接收消息大小限制是4MB,而你处理大文件后返回的响应数据超过了这个阈值,所以触发了资源耗尽的错误。

至于Python和JavaScript客户端没有这个问题,是因为这两个语言的Document AI SDK要么默认设置了更高的gRPC消息大小限制,要么内部自动根据场景调整了参数,不需要你手动配置。

解决方案:手动调整gRPC消息大小限制

你需要在创建客户端连接的时候,显式配置更大的接收消息上限。下面是修改后的完整代码,重点看通道配置的部分:

#include "google/cloud/documentai/v1/document_processor_client.h"
#include "google/cloud/location.h"
#include <fstream>
#include <iostream>
#include <string>

int main(int argc, char* argv[]) try {
    if (argc != 5) {
        std::cerr << "Usage: " << argv[0] << " project-id location-id processor-id filename (JPEG)\n";
        return 1;
    }

    std::string const location_id = argv[2];
    if (location_id != "us" && location_id != "eu") {
        std::cerr << "location-id must be either 'us' or 'eu'\n";
        return 1;
    }

    auto const location = google::cloud::Location(argv[1], location_id);
    namespace documentai = ::google::cloud::documentai_v1;

    // 配置gRPC通道,增大接收消息的最大限制
    auto channel_options = google::cloud::Options{};
    // 这里设置为10MB,你可以根据实际响应大小调整(比如20*1024*1024=20MB)
    channel_options.set<google::cloud::GrpcReceiveMessageSizeOption>(10 * 1024 * 1024);

    auto client = documentai::DocumentProcessorServiceClient(
        documentai::MakeDocumentProcessorServiceConnection(
            location.location_id(), channel_options)); // 传入自定义配置

    google::cloud::documentai::v1::ProcessRequest req;
    req.set_name(location.FullName() + "/processors/" + argv[3]);
    req.set_skip_human_review(true);

    auto& doc = *req.mutable_raw_document();
    doc.set_mime_type("image/jpeg");
    std::ifstream is(argv[4]);
    doc.set_content(std::string{std::istreambuf_iterator<char>(is), {}});

    auto resp = client.ProcessDocument(std::move(req));
    if (!resp) throw std::move(resp).status();

    std::cout << resp->document().text() << "\n";
    return 0;
} catch (google::cloud::Status const& status) {
    std::cerr << "google::cloud::Status thrown: " << status << "\n";
    return 1;
}

核心修改说明

  • 新增了google::cloud::Options对象,通过GrpcReceiveMessageSizeOption参数设置了更大的接收消息容量(示例为10MB),你可以根据自己的文件处理后的响应大小灵活调整这个数值。
  • 在创建连接时,把配置好的channel_options传入MakeDocumentProcessorServiceConnection,覆盖客户端默认的4MB限制。

这样调整后,C++客户端就能像Python和JavaScript版本一样处理大文件的响应了。

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 10:59:30