Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: support Google Vertex with Pydantic-AI v2 (#10256)

AADETYA JAISWAL081fa58f4e41
diff --git a/marimo/_server/ai/providers.py b/marimo/_server/ai/providers.py
index 7fe043cad6..0d2238cbb9 100644
--- a/marimo/_server/ai/providers.py
+++ b/marimo/_server/ai/providers.py
@@ -70,7 +70,9 @@ if TYPE_CHECKING:
from pydantic_ai.providers.bedrock import (
BedrockProvider as PydanticBedrock,
)
- from pydantic_ai.providers.google import GoogleProvider as PydanticGoogle
+ from pydantic_ai.providers.google import (
+ BaseGoogleProvider as PydanticGoogleProvider,
+ )
from pydantic_ai.providers.openai import OpenAIProvider as PydanticOpenAI
from pydantic_ai.settings import ModelSettings, ThinkingLevel
from pydantic_ai.toolsets import AbstractToolset
@@ -388,9 +390,11 @@ class PydanticProvider(ABC, Generic[ProviderT]):
return toolset, output_type
-class GoogleProvider(PydanticProvider["PydanticGoogle"]):
+class GoogleProvider(PydanticProvider["PydanticGoogleProvider"]):
@override
- def create_provider(self, config: AnyProviderConfig) -> PydanticGoogle:
+ def create_provider(
+ self, config: AnyProviderConfig
+ ) -> PydanticGoogleProvider:
from pydantic_ai.providers.google import (
GoogleProvider as PydanticGoogle,
)
@@ -404,6 +408,10 @@ class GoogleProvider(PydanticProvider["PydanticGoogle"]):
os.getenv("GOOGLE_GENAI_USE_VERTEXAI", "").lower() == "true"
)
if use_vertex:
+ from pydantic_ai.providers.google_cloud import (
+ GoogleCloudProvider as PydanticGoogleCloud,
+ )
+
project = os.getenv("GOOGLE_CLOUD_PROJECT")
# Upstream (pydantic-ai) defaults to us-central1 if not set
location = os.getenv("GOOGLE_CLOUD_LOCATION") or None
@@ -414,16 +422,13 @@ class GoogleProvider(PydanticProvider["PydanticGoogle"]):
"Set this env var if your project has region restrictions."
)
LOGGER.info(location_msg)
- # The type stubs don't have an overload that combines vertexai
- # with project/location, but the runtime supports it
- provider: PydanticGoogle = PydanticGoogle( # type: ignore[call-overload]
- vertexai=True,
+ return PydanticGoogleCloud(
project=project,
location=location,
)
- else:
- # Try default initialization which may work with environment variables
- provider = PydanticGoogle() # type: ignore[call-overload]
+
+ # Try default initialization which may work with environment variables
+ provider: PydanticGoogleProvider = PydanticGoogle() # type: ignore[call-overload]
return provider
@override
diff --git a/tests/_server/ai/test_providers.py b/tests/_server/ai/test_providers.py
index 1563e9fbd4..ab41747ba9 100644
--- a/tests/_server/ai/test_providers.py
+++ b/tests/_server/ai/test_providers.py
@@ -1,5 +1,6 @@
"""Tests for the LLM providers in marimo._server.ai.providers."""
+import os
from typing import Any, cast
from unittest.mock import AsyncMock, MagicMock, patch
@@ -395,6 +396,91 @@ def test_anthropic_thinking_payload_translation(
assert payload["budget_tokens"] > 0
+ (
+ "api_key",
+ "environment",
+ "expected_provider",
+ "expected_kwargs",
+ ),
+ [
+ pytest.param(
+ "test-key",
+ {"GOOGLE_GENAI_USE_VERTEXAI": "true"},
+ "google",
+ {"api_key": "test-key"},
+ id="api_key",
+ ),
+ pytest.param(
+ "",
+ {"GOOGLE_API_KEY": "environment-key"},
+ "google",
+ {},
+ id="default_google",
+ ),
+ pytest.param(
+ "",
+ {
+ "GOOGLE_GENAI_USE_VERTEXAI": "true",
+ "GOOGLE_CLOUD_PROJECT": "test-project",
+ "GOOGLE_CLOUD_LOCATION": "europe-west1",
+ },
+ "google-cloud",
+ {
+ "project": "test-project",
+ "location": "europe-west1",
+ },
+ id="vertex",
+ ),
+ pytest.param(
+ "",
+ {
+ "GOOGLE_GENAI_USE_VERTEXAI": "true",
+ "GOOGLE_CLOUD_PROJECT": "test-project",
+ },
+ "google-cloud",
+ {
+ "project": "test-project",
+ "location": None,
+ },
+ id="vertex_default_location",
+ ),
+ ],
+)
+ not DependencyManager.google_ai.has()
+ or not DependencyManager.pydantic_ai.has(),
+ reason="google or pydantic_ai not installed",
+)
+def test_google_provider_selection(
+ api_key: str,
+ environment: dict[str, str],
+ expected_provider: str,
+ expected_kwargs: dict[str, str | None],
+) -> None:
+ """Route API-key and Vertex configs to their matching providers."""
+ with (
+ patch.dict(os.environ, environment, clear=True),
+ patch("pydantic_ai.providers.google.GoogleProvider") as mock_google,
+ patch(
+ "pydantic_ai.providers.google_cloud.GoogleCloudProvider"
+ ) as mock_google_cloud,
+ ):
+ provider = GoogleProvider(
+ "gemini-2.5-flash",
+ AnyProviderConfig(api_key=api_key, base_url=None),
+ )
+
+ if expected_provider == "google":
+ mock_google.assert_called_once_with(**expected_kwargs)
+ mock_google_cloud.assert_not_called()
+ assert provider.provider is mock_google.return_value
+ else:
+ mock_google.assert_not_called()
+ mock_google_cloud.assert_called_once_with(**expected_kwargs)
+ assert provider.provider is mock_google_cloud.return_value
+
+
@pytest.mark.parametrize(
("model_name", "expected_thinking"),
[