Skip to content

fix(genai): support Vertex AI multiregion endpoints#1816

Open
Diego Fernández Gil (diefergil) wants to merge 1 commit into
langchain-ai:mainfrom
diefergil:codex/google-genai-multiregion-endpoints
Open

fix(genai): support Vertex AI multiregion endpoints#1816
Diego Fernández Gil (diefergil) wants to merge 1 commit into
langchain-ai:mainfrom
diefergil:codex/google-genai-multiregion-endpoints

Conversation

@diefergil

Copy link
Copy Markdown

Description

Support Vertex AI multiregion locations in langchain-google-genai by resolving location="us" and location="eu" to the Google mREP hostnames when no custom base_url is provided.

This keeps explicit base_url / legacy client_options={"api_endpoint": ...} behavior intact, while avoiding the SDK's regional hostname pattern for multiregion locations.

Relevant issues

Fixes #1799

Type

🐛 Bug Fix
✅ Test

Changes(optional)

  • Added shared base URL resolution for custom endpoints and Vertex AI multiregion endpoints.
  • Applied the resolver to chat models and embeddings clients.
  • Added unit coverage for us and eu mREP endpoint selection and custom base_url precedence.

Testing(optional)

  • uv run --group test pytest tests/unit_tests/test_chat_models.py::test_vertexai_multiregion_location_uses_rep_endpoint tests/unit_tests/test_chat_models.py::test_vertexai_multiregion_location_preserves_custom_base_url tests/unit_tests/test_embeddings.py::test_vertexai_multiregion_location_uses_rep_endpoint tests/unit_tests/test_embeddings.py::test_vertexai_multiregion_location_preserves_custom_base_url
  • uv run --group test pytest tests/unit_tests/test_chat_models.py tests/unit_tests/test_embeddings.py
  • uv run --group lint ruff check langchain_google_genai/_common.py langchain_google_genai/chat_models.py langchain_google_genai/embeddings.py tests/unit_tests/test_chat_models.py tests/unit_tests/test_embeddings.py
  • uv run --group typing mypy langchain_google_genai

Note(optional)

This contribution was prepared with assistance from an AI coding agent; the changes and test results were reviewed before submission.

@diefergil Diego Fernández Gil (diefergil) marked this pull request as ready for review May 29, 2026 10:30
Copilot AI review requested due to automatic review settings May 29, 2026 10:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds automatic resolution of the Vertex AI multiregion (us, eu) REP endpoint when no custom base_url is provided, by extracting the base URL resolution logic into a shared _resolve_base_url helper used by both ChatGoogleGenerativeAI and GoogleGenerativeAIEmbeddings.

Changes:

  • Introduce _resolve_base_url helper in _common.py that handles legacy dict-style client_options and maps us/eu Vertex locations to the aiplatform.{location}.rep.googleapis.com endpoint.
  • Refactor chat_models.py and embeddings.py to use the shared helper.
  • Add unit tests covering multiregion endpoint resolution and custom base_url preservation.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
libs/genai/langchain_google_genai/_common.py Adds _resolve_base_url helper and multiregion URL mapping; updates docstring.
libs/genai/langchain_google_genai/chat_models.py Replaces inline base_url parsing with the shared helper.
libs/genai/langchain_google_genai/embeddings.py Uses the shared helper to resolve the base URL passed to HttpOptions.
libs/genai/tests/unit_tests/test_chat_models.py Adds tests for multiregion endpoint and custom base URL preservation.
libs/genai/tests/unit_tests/test_embeddings.py Adds parallel tests for the embeddings client.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +37 to +42
def _resolve_base_url(
base_url: str | dict | None,
*,
use_vertexai: bool,
location: str | None,
) -> str | None:


def _resolve_base_url(
base_url: str | dict | None,
Comment on lines +203 to +211
def test_vertexai_multiregion_location_preserves_custom_base_url() -> None:
with patch("langchain_google_genai.embeddings.Client") as mock_client:
GoogleGenerativeAIEmbeddings(
model=MODEL_NAME,
vertexai=True,
project="test-project",
location="eu",
base_url="https://gateway.example.com/api/gemini",
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using a multi-region location (us or eu) results in a 404

2 participants