I am attempting to use Google Cloud Document AI's asynchronous batch processing feature in a new Google Cloud project, but I am consistently encountering a ValueError. I also notice a limitation in the Google Cloud Console UI for the Document AI processor.
When I execute my Python script in Google Cloud Shell to perform Document AI batch processing, it fails with the following error traceback:
Traceback (most recent call last):
File "/home/akihiro/process_document.py", line 101, in <module>
batch_process_document(
File "/home/akihiro/process_document.py", line 48, in batch_process_document
gcs_documents=documentai.GcsDocuments(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/proto/message.py", line 724, in init
raise ValueError(
ValueError: Unknown field for GcsDocuments: gcs_uri
This error indicates that the gcs_uri field is not recognized for documentai.GcsDocuments, which is unexpected as it should be the correct field name according to current API documentation and client library usage.
Additionally, in the Google Cloud Console for my Document AI processor (under "My Processors" or the specific processor details page), only a "Test" button is displayed, and options like "Start Batch Processing" are missing. This prevents me from initiating batch processing directly from the UI.
Here's my project and resource information:
- Google Cloud Project ID:
docai-test-XXXXXX-new(Project Number:619893412057) - Document AI Processor Type: Document OCR
- Processor Name:
document-ocr-XXXXXX-2025 - Processor ID:
bf52b8e242a187ff - Processor Region:
us - Input GCS Bucket Name:
docai-new-input-XXXXXX-2025-final - Input File URI:
gs://docai-new-input-XXXXXX-2025-final/ZZZ07_600dpi.pdf(This is a ~75MB PDF file.)
This is the Python script code I am using:
(Please note that PROJECT_ID, PROCESSOR_ID, and GCS_INPUT_FILE_URI are updated to match my new project resources.)
import os
from urllib.parse import urlparse
# Cloud Shell 環境での最新かつ確実なインポート方法
from google.cloud import documentai
from google.api_core.client_options import ClientOptions
from google.cloud import storage
# --- あなたの設定情報(ここだけご確認ください) ---
# 【確認1】あなたのGoogle CloudプロジェクトIDを正確に入力してください。
# 例: "your-project-id-123456"
PROJECT_ID = "docai-test-XXXXXX-new" # ★★★ここをあなたの新しいプロジェクトIDに更新してください!★★★
# 【確認2】Document AIプロセッサのロケーションを正確に入力してください。
# プロセッサ作成時に選択したロケーションです (通常は us)。
LOCATION = "us"
# 【確認3】Document AIプロセッサIDを正確に入力してください。
# 「マイプロセッサ」画面で確認できるIDです。
PROCESSOR_ID = "bf52b8e242a187ff" # ★★★ここをあなたの新しいプロセッサIDに更新してください!★★★
# 【確認4】処理したいPDFファイルがGCS上のどこにあるか、そのURIを正確に入力してください。
# 例: "gs://docai-new-input-XXXXXX-2025-final/ZZZ07_600dpi.pdf"
# このパスは、あなたがGCSにアップロードしたあなたの75MBのPDFファイルの場所です。
GCS_INPUT_FILE_URI = "gs://docai-new-input-XXXXXX-2025-final/ZZZ07_600dpi.pdf" # ★★★ここをあなたが指定したGCS URIに更新してください!★★★
# 【確認5】GCSの出力フォルダ名を指定してください。
# これは結果が保存されるバケット内のフォルダ名です。末尾に / を忘れずにつけてください。
# 例: "processed_results/"
GCS_OUTPUT_PREFIX = "processed_results/"
# --- メインの非同期処理関数 ---
def batch_process_document(project_id, location, processor_id, gcs_input_uri, gcs_output_prefix):
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
# Document AI クライアントの初期化 (documentai モジュールから直接取得)
client = documentai.DocumentProcessorServiceClient(client_options=opts)
processor_name = client.processor_path(project_id, location, processor_id)
# GCS入力URIからバケット名とオブジェクト名を抽出
parsed_uri = urlparse(gcs_input_uri)
gcs_input_bucket = parsed_uri.netloc # バケット名
# 1. BatchProcessDocumentsRequest の作成
# Document AI クライアントの型定義も documentai モジュールから直接アクセス
input_config = documentai.BatchDocumentsInputConfig(
gcs_documents=documentai.GcsDocuments(
gcs_uri=gcs_input_uri
)
)
# 出力パスのGCSバケット名を決定 (入力バケットと同じとする)
gcs_output_bucket_uri = f"gs://{gcs_input_bucket}/{gcs_output_prefix}"
output_config = documentai.DocumentOutputConfig(
gcs_output_config=documentai.DocumentOutputConfig.GcsOutputConfig(
gcs_uri=gcs_output_bucket_uri
)
)
request = documentai.BatchProcessRequest(
name=processor_name,
input_documents=input_config,
document_output_config=output_config,
)
print(f"Document AI で非同期処理を開始します...")
try:
operation = client.batch_process_documents(request=request)
print(f"処理が開始されました。Operation ID: {operation.operation.name}")
print("完了を待機中... (これには時間がかかる場合があります)")
operation.result()
print("Document AI Batch Processing が完了しました!")
print(f"結果は GCS バケット '{gcs_input_bucket}' の '{gcs_output_prefix}' 以下に保存されています。")
# GCSから結果ファイルを列挙し、存在を確認する簡単な例
storage_client = storage.Client() # Cloud Shellでは認証済み
blobs_in_output = storage_client.list_blobs(gcs_input_bucket, prefix=gcs_output_prefix)
found_result_files = False
print("\nGCSの出力パスで結果ファイルを探しています...")
for blob_item in blobs_in_output:
if blob_item.name.endswith(".json"): # JSON形式の結果ファイルを想定
print(f"-> 結果ファイルが見つかりました: gs://{gcs_input_bucket}/{blob_item.name}")
found_result_files = True
if not found_result_files:
print("注意: 指定されたGCS出力パスに結果ファイルが見つかりませんでした。Cloud LoggingとGCSを確認してください。")
except Exception as e:
print(f"Document AI Batch Processing 中にエラーが発生しました: {e}")
print("Cloud Logging で詳細なエラーログを確認してください。")
print("特に、Document AI プロセッサが Cloud Storage バケットへのアクセス権を持っているか確認してください。")
if __name__ == "__main__":
# Cloud Shell ではGCSバケットの自動作成やローカルからのアップロードは行いません
# GCS_INPUT_FILE_URI で指定されたファイルが既にGCSに存在することを前提とします。
# Batch Process を実行
batch_process_document(
PROJECT_ID,
LOCATION,
PROCESSOR_ID,
GCS_INPUT_FILE_URI,
GCS_OUTPUT_PREFIX
)
I expected the script to successfully initiate the Document AI batch processing and store the results in the specified GCS output path. However, it consistently fails with the ValueError as described above, and I am unable to use the console UI as an alternative.
I have already tried the following troubleshooting steps, but the issue persists:
Updated the Python script from gcs_uris=[gcs_input_uri] to gcs_uri=gcs_input_uri to match the latest API specification. Confirmed PROJECT_ID, PROCESSOR_ID, and GCS_INPUT_FILE_URI are correctly set to the new project's resources. Completely deleted old script files (e.g., process_document.py, process_document_(1).py) from Cloud Shell using rm -f and confirmed their absence with ls -l before re-uploading the latest script. Created a brand new Google Cloud Project (docai-test-akihiro-new) and performed all setup (API enablement, new Document OCR processor, new GCS bucket, PDF upload) within this fresh environment, then launched a fresh Cloud Shell session tied to this new project. Enabled the Document AI API in the new project. Switched the processor type from "Form Parser" to "Document OCR". Previously executed pip install google-cloud-documentai --upgrade in Cloud Shell.
The Cloud Shell is running Python 3.12.
I suspect there might be a persistent issue with the Cloud Shell environment itself (e.g., caching or library linking issues) or a more fundamental problem with how the google-cloud-documentai client library interacts with the Python version in this specific Cloud Shell instance. The console UI limitation also suggests a potential deeper issue with the processor's state or configuration that might not be directly related to the script code.
gcsDocumentsis type GcsDocuments and takes a mutable sequence (e.g.[...]) of [GcsDocument](https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1beta3.types.GcsDocument) which takes e.g.gcs_uri. So you need something of the formGcsDocuments(documents=[GcsDocument(gcs_uri=..., mime_type=...)])`processors.batchProcessand drilling down....GcsDocumentsanddocumentsof typeGcsDocument