googleapis / google-cloud-ruby

Google Cloud Client Library for Ruby
https://googleapis.github.io/google-cloud-ruby/
Apache License 2.0
1.34k stars 537 forks source link

Facing difficult in sending a request to document ai processor #25951

Open nshganesh opened 1 month ago

nshganesh commented 1 month ago

https://cloud.google.com/document-ai/docs/process-documents-client-libraries following this documentation on making a request to individual document processing.

According to the below screenshot, we need to send a base64 encoded image.

Screenshot 2024-05-24 at 2 50 44 PM

But https://cloud.google.com/document-ai/docs/process-documents-client-libraries suggests without encoding approach.

content = File.binread file_path

Both approaches:

On making a request without base64 encoding, getting the below error:

**An error occurred when inspecting the object: #<ArgumentError: invalid byte sequence in UTF-8>
Result of Kernel#inspect: #<Google::Cloud::DocumentAI::V1::ProcessResponse:0x0000000128b78670 @descriptor=#<Google::Protobuf::Descriptor:0x0000000125b89ec8>>**

On making a request with base64 encoding, getting the below error:

bin_file = File.binread file_path content = Base64.strict_encode64(bin_file)

**3:Invalid image content. debug_error_string:{UNKNOWN:Error received from peer ipv6:%5B2404:6800:4007:81e::200a%5D:443 {created_time:"2024-05-24T14:54:57.103571+05:30", grpc_status:3, grpc_message:"Invalid image content"}} (Google::Cloud::InvalidArgumentError)**
dazuma commented 1 month ago

Hi @nshganesh,

I'm not sure where the first screenshot comes from, but it's not on the https://cloud.google.com/document-ai/docs/process-documents-client-libraries page. The use of JSON and base64 is specific to an older endpoint for documentai, and does not apply to the code in https://cloud.google.com/document-ai/docs/process-documents-client-libraries. (If the documentation on https://cloud.google.com/document-ai/docs/process-documents-client-libraries links directly to that screenshot somewhere and implies that it is related, then that's misleading and it should be corrected; if you could point us to where that link is, that would help, because I wasn't able to locate it.)

If you're using the client libraries in https://cloud.google.com/document-ai/docs/process-documents-client-libraries then you should not be doing any base64 encoding. The error you're getting without base64 encoding suggests to me that the request actually did succeed. The error is actually happening when trying to pretty-print the response. It looks like there's a field somewhere in the response message that is a binary string but is being incorrectly marked as utf-8, which if true might indicate a bug in the protobuf library. As a workaround, I suggest, instead of printing or inspecting the response, just get the fields you're interested in.

nshganesh commented 1 month ago

HI @dazuma

Thank you so much for taking time to go through my issue. I actually went ahead with a nodejs client library. It works perfectly with base64 encoding and according to the documentation.

Sharing little more details about the problem however when I used ruby.

The screenshot I have shared is part of an authenticated page where we can create custom processors. Something like this https://console.cloud.google.com/ai/document-ai/locations/us/processors/[processorid].

I am not inspecting or printing from my part as such, even though the error message somehow suggests that. I also believe that there is some bug in the protobuf library just as you pointed it out.