I’m stuck with Google Cloud Document AI and getting zero response from my Python script.
I set up everything properly - created a service account, downloaded the JSON credentials file, and configured the API access. But when I execute my code, absolutely nothing happens. No errors, no output, just silence.
Here’s my current implementation:
from typing import Optional
from google.api_core.client_options import ClientOptions
from google.cloud import documentai
# Configuration
my_project = "my-project-id"
region = "us"
processor_key = "my-processor-id"
doc_file = "sample.pdf"
content_type = "application/pdf"
output_fields = "text,entities,pages.pageNumber"
def extract_document_data(
my_project: str,
region: str,
processor_key: str,
doc_file: str,
content_type: str,
output_fields: Optional[str] = None,
version_id: Optional[str] = None,
) -> None:
# Set up client with regional endpoint
options = ClientOptions(api_endpoint=f"{region}-documentai.googleapis.com")
service_client = documentai.DocumentProcessorServiceClient(client_options=options)
# Build processor path
if version_id:
processor_name = service_client.processor_version_path(
my_project, region, processor_key, version_id
)
else:
processor_name = service_client.processor_path(my_project, region, processor_key)
# Read file content
with open(doc_file, "rb") as file_handle:
file_data = file_handle.read()
# Create document object
input_doc = documentai.RawDocument(content=file_data, mime_type=content_type)
# Build processing request
api_request = documentai.ProcessRequest(
name=processor_name,
raw_document=input_doc,
field_mask=output_fields,
)
# Process document
response = service_client.process_document(request=api_request)
processed_doc = response.document
print("Extracted text from document:")
print(processed_doc.text)
I’ve tested both OCR and custom extraction processors but neither produces any output. My terminal just shows the command prompt again with no feedback. What could be causing this silent failure? Any debugging tips would be appreciated.