Google DocumentAI -> ValueError: Protocol message Document has no "file" field

Google DocumentAI -> ValueError: Protocol message Document has no "file" field

在我的脚本中,我有以下内容:

response = requests.get(list_url[0], allow_redirects=True)
s = io.BytesIO()
s.write(response.content)
s.seek(0)
mimetype="application/octet-stream"
document = {'file': s.read(), 'mime': mimetype}
request = {"name": name, "document": document}

但是,当我向服务器发送请求时:

result = client.process_document(request=request)

我得到 ValueError: Protocol message Document has no "file" field

这是因为 google docAI 不接受八位字节流吗?

查看了文档aipython客户端DocumentProcessorServiceClient的最新版本代码,发现这个函数在其request字段上传递了一个Process Request对象。您可以在 process_document github 代码页上查看该函数的详细信息。

Process Request will accept either inline_document or a raw_document (both are mutual exclusive). Based on your code it looks like you are passing a raw_document 只接受字段 contentmime_type 应该使用而不是 filemime.

如果你检查 sample of using python library client for document ai 你会发现这行解释了它应该如何实现:

...
    document = {"content": image_content, "mime_type": "application/pdf"}

    # Configure the process request
    request = {"name": name, "raw_document": document}

    result = client.process_document(request=request)
...

有关其他详细信息,您可以查看 document ai and the official google page for the python client library 的官方 github 项目。