Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 3 additions & 43 deletions pydantic_ai_slim/pydantic_ai/providers/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

try:
from google.auth.credentials import Credentials
from google.genai._api_client import BaseApiClient
from google.genai.client import Client, DebugConfig
from google.genai.client import Client
from google.genai.types import HttpOptions
except ImportError as _import_error:
raise ImportError(
Expand Down Expand Up @@ -115,16 +114,14 @@ def __init__(
base_url=base_url,
headers={'User-Agent': get_user_agent()},
httpx_async_client=http_client,
# TODO: Remove once https://github.com/googleapis/python-genai/issues/1565 is solved.
async_client_args={'transport': httpx.AsyncHTTPTransport()},
)
if not vertexai:
if api_key is None:
raise UserError(
'Set the `GOOGLE_API_KEY` environment variable or pass it via `GoogleProvider(api_key=...)`'
'to use the Google Generative Language API.'
)
self._client = _SafelyClosingClient(vertexai=False, api_key=api_key, http_options=http_options)
self._client = Client(vertexai=False, api_key=api_key, http_options=http_options)
else:
if vertex_ai_args_used:
api_key = None
Expand All @@ -138,7 +135,7 @@ def __init__(
# For more details, check: https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations#available-regions
location = location or os.getenv('GOOGLE_CLOUD_LOCATION') or 'us-central1'

self._client = _SafelyClosingClient(
self._client = Client(
vertexai=True,
api_key=api_key,
project=project,
Expand Down Expand Up @@ -184,40 +181,3 @@ def __init__(
"""Regions available for Vertex AI.
More details [here](https://cloud.google.com/vertex-ai/generative-ai/docs/learn/locations#genai-locations).
"""


class _SafelyClosingClient(Client):
@staticmethod
def _get_api_client(
vertexai: bool | None = None,
api_key: str | None = None,
credentials: Credentials | None = None,
project: str | None = None,
location: str | None = None,
debug_config: DebugConfig | None = None,
http_options: HttpOptions | None = None,
) -> BaseApiClient:
return _NonClosingApiClient(
vertexai=vertexai,
api_key=api_key,
credentials=credentials,
project=project,
location=location,
http_options=http_options,
)

def close(self) -> None:
# This is called from `Client.__del__`, even if `Client.__init__` raised an error before `self._api_client` is set, which would raise an `AttributeError` here.
# TODO: Remove once https://github.com/googleapis/python-genai/issues/1567 is solved.
try:
super().close()
except AttributeError:
pass


class _NonClosingApiClient(BaseApiClient):
async def aclose(self) -> None:
# The original implementation also calls `await self._async_httpx_client.aclose()`, but we don't want to close our `cached_async_http_client` or the one the user passed in.
# TODO: Remove once https://github.com/googleapis/python-genai/issues/1566 is solved.
if self._aiohttp_session:
await self._aiohttp_session.close() # pragma: no cover
2 changes: 1 addition & 1 deletion pydantic_ai_slim/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ logfire = ["logfire[httpx]>=3.14.1"]
openai = ["openai>=1.107.2"]
cohere = ["cohere>=5.18.0; platform_system != 'Emscripten'"]
vertexai = ["google-auth>=2.36.0", "requests>=2.32.2"]
google = ["google-genai>=1.46.0"]
google = ["google-genai>=1.48.0"]
anthropic = ["anthropic>=0.70.0"]
groq = ["groq>=0.25.0"]
mistral = ["mistralai>=1.9.10"]
Expand Down
18 changes: 14 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading