Repository navigation
fix(documentai-toolbox): honor universe_domain and client_options for regional Document AI endpoints #19296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
chalmerlowe
wants to merge
1
commit into
main
Choose a base branch
from
fix/documentai-toolbox-universe
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
fix(documentai-toolbox): honor universe_domain and client_options for regional Document AI endpoints #19296
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
packages/google-cloud-documentai-toolbox/tests/unit/test_universe_domain.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Unit tests for universe_domain endpoint resolution in documentai-toolbox.""" | ||
|
|
||
| from types import MappingProxyType | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
| from google.api_core import client_options as client_options_lib | ||
| from google.api_core.universe import EmptyUniverseError | ||
|
|
||
| from google.cloud import documentai | ||
| from google.cloud.documentai_toolbox.converters import converter | ||
| from google.cloud.documentai_toolbox.utilities import gcs_utilities | ||
| from google.cloud.documentai_toolbox.wrappers import document | ||
|
|
||
| DEFAULT_UNIVERSE = "googleapis.com" | ||
| CUSTOM_UNIVERSE = "apis-tpclp.goog" | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def clean_universe_env(monkeypatch): | ||
| monkeypatch.delenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN", raising=False) | ||
|
|
||
|
|
||
| def test_resolve_client_universe_precedence_and_errors(monkeypatch): | ||
| assert gcs_utilities._resolve_client_universe(None) == DEFAULT_UNIVERSE | ||
|
|
||
| monkeypatch.setenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN", f" {CUSTOM_UNIVERSE} ") | ||
| assert gcs_utilities._resolve_client_universe(None) == CUSTOM_UNIVERSE | ||
|
|
||
| opts = client_options_lib.ClientOptions(universe_domain="explicit.goog") | ||
| assert gcs_utilities._resolve_client_universe(opts) == "explicit.goog" | ||
|
|
||
| mapping = MappingProxyType({"universe_domain": "mapping.goog"}) | ||
| assert gcs_utilities._resolve_client_universe(mapping) == "mapping.goog" | ||
|
|
||
| with pytest.raises(EmptyUniverseError): | ||
| gcs_utilities._resolve_client_universe({"universe_domain": " "}) | ||
|
|
||
| monkeypatch.setenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN", " ") | ||
| with pytest.raises(EmptyUniverseError): | ||
| gcs_utilities._resolve_client_universe(None) | ||
|
|
||
| with pytest.raises(TypeError, match="ClientOptions instance, Mapping, or None"): | ||
| gcs_utilities._resolve_client_universe("invalid") # type: ignore[arg-type] | ||
|
|
||
| with pytest.raises(TypeError, match="universe_domain must be a string"): | ||
| gcs_utilities._resolve_client_universe( | ||
| client_options_lib.ClientOptions(universe_domain=123) # type: ignore[arg-type] | ||
| ) | ||
|
|
||
|
|
||
| def test_get_documentai_client_options_respects_env_and_does_not_mutate(monkeypatch): | ||
| default_opts = gcs_utilities._get_documentai_client_options("us") | ||
| assert default_opts.api_endpoint == "us-documentai.googleapis.com" | ||
| assert default_opts.universe_domain == DEFAULT_UNIVERSE | ||
|
|
||
| monkeypatch.setenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN", CUSTOM_UNIVERSE) | ||
| env_opts = gcs_utilities._get_documentai_client_options("eu") | ||
| assert env_opts.api_endpoint == f"eu-documentai.{CUSTOM_UNIVERSE}" | ||
| assert env_opts.universe_domain == CUSTOM_UNIVERSE | ||
|
|
||
| caller_opts = client_options_lib.ClientOptions(quota_project_id="qp-1") | ||
| derived = gcs_utilities._get_documentai_client_options("us", caller_opts) | ||
| assert derived is not caller_opts | ||
| assert derived.api_endpoint == f"us-documentai.{CUSTOM_UNIVERSE}" | ||
| assert derived.universe_domain == CUSTOM_UNIVERSE | ||
| assert derived.quota_project_id == "qp-1" | ||
| assert caller_opts.api_endpoint is None | ||
| assert caller_opts.universe_domain is None | ||
|
|
||
| explicit_endpoint = MappingProxyType({"api_endpoint": "custom.endpoint:443"}) | ||
| derived_ep = gcs_utilities._get_documentai_client_options("us", explicit_endpoint) | ||
| assert derived_ep.api_endpoint == "custom.endpoint:443" | ||
| assert derived_ep.universe_domain == CUSTOM_UNIVERSE | ||
|
|
||
|
|
||
| @mock.patch("google.cloud.documentai_toolbox.converters.converter.documentai") | ||
| def test_get_base_ocr_honors_universe_domain(mock_docai, monkeypatch): | ||
| monkeypatch.setenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN", CUSTOM_UNIVERSE) | ||
| mock_client = mock_docai.DocumentProcessorServiceClient.return_value | ||
| mock_client.process_document.return_value.document = "Done" | ||
|
|
||
| actual = converter._get_base_ocr( | ||
| project_id="proj", | ||
| location="us", | ||
| processor_id="proc", | ||
| file_bytes=b"pdf", | ||
| mime_type="application/pdf", | ||
| ) | ||
|
|
||
| assert actual == "Done" | ||
| passed_opts = mock_docai.DocumentProcessorServiceClient.call_args.kwargs[ | ||
| "client_options" | ||
| ] | ||
| assert passed_opts.api_endpoint == f"us-documentai.{CUSTOM_UNIVERSE}" | ||
| assert passed_opts.universe_domain == CUSTOM_UNIVERSE | ||
|
|
||
|
|
||
| @mock.patch("google.cloud.documentai_toolbox.wrappers.document.documentai") | ||
| def test_get_batch_process_metadata_honors_universe_domain(mock_docai, monkeypatch): | ||
| monkeypatch.setenv("GOOGLE_CLOUD_UNIVERSE_DOMAIN", CUSTOM_UNIVERSE) | ||
| mock_client = mock_docai.DocumentProcessorServiceClient.return_value | ||
| metadata = documentai.BatchProcessMetadata( | ||
| state=documentai.BatchProcessMetadata.State.SUCCEEDED, | ||
| ) | ||
| mock_client.get_operation.return_value = mock.Mock( | ||
| done=True, | ||
| metadata=mock.Mock( | ||
| type_url="type.googleapis.com/google.cloud.documentai.v1.BatchProcessMetadata", | ||
| value=documentai.BatchProcessMetadata.serialize(metadata), | ||
| ), | ||
| ) | ||
|
|
||
| document._get_batch_process_metadata("projects/123/locations/eu/operations/456") | ||
|
|
||
| passed_opts = mock_docai.DocumentProcessorServiceClient.call_args.kwargs[ | ||
| "client_options" | ||
| ] | ||
| assert passed_opts.api_endpoint == f"eu-documentai.{CUSTOM_UNIVERSE}" | ||
| assert passed_opts.universe_domain == CUSTOM_UNIVERSE | ||
|
|
||
|
|
||
| @mock.patch( | ||
| "google.cloud.documentai_toolbox.wrappers.document._get_batch_process_metadata" | ||
| ) | ||
| @mock.patch.object(document.Document, "from_batch_process_metadata", return_value=[]) | ||
| def test_from_batch_process_operation_forwards_client_options( | ||
| mock_from_meta, mock_get_meta | ||
| ): | ||
| opts = client_options_lib.ClientOptions(universe_domain=CUSTOM_UNIVERSE) | ||
| document.Document.from_batch_process_operation( | ||
| location="us", | ||
| operation_name="projects/123/locations/us/operations/456", | ||
| client_options=opts, | ||
| ) | ||
| mock_get_meta.assert_called_once_with( | ||
| operation_name="projects/123/locations/us/operations/456", | ||
| location="us", | ||
| timeout=None, | ||
| client_options=opts, | ||
| ) | ||
| mock_from_meta.assert_called_once() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converting
client_optionsfrom aMappingto aClientOptionsinstance viaclient_options_lib.from_dictjust to extract theuniverse_domainis inefficient and redundant, as it is converted again in_get_documentai_client_options. Additionally, if an older version ofgoogle-api-coreis used wherefrom_dictdoes not support or parseuniverse_domain, the value would be lost.Instead, we can directly retrieve
universe_domainfrom theMappingorClientOptionsobject without performing the redundant conversion.