Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
01bd549
add dep
mdrxy Nov 12, 2025
2084f0e
remove unnecessary type ignores
mdrxy Nov 12, 2025
b3627d2
add helpful context to `base_url`
mdrxy Nov 12, 2025
fe53502
rfc: `load_part`
mdrxy Nov 12, 2025
8cd2ab9
remove gemini 1 from `_supports_code_execution`, prep for upcoming
mdrxy Nov 12, 2025
edd27c0
update enums
mdrxy Nov 12, 2025
f3743c0
remove check for `model/` prefix
mdrxy Nov 12, 2025
e674d08
fix `safety_settings` usage example import path
mdrxy Nov 12, 2025
d7de4e5
style/cleanup
mdrxy Nov 12, 2025
4aea216
remove unnecessary `lc_secrets`
mdrxy Nov 12, 2025
5dd2527
add clarifying comment to `build_extra`
mdrxy Nov 12, 2025
55ca9a8
handle potential `None` value in `total_tokens` return
mdrxy Nov 12, 2025
81dc395
handle potentially uninitialized client in `count_tokens`
mdrxy Nov 12, 2025
349e687
address `models/` prefix regression following f3743c07a4e74303ef47c09…
mdrxy Nov 12, 2025
4c28450
update some docstrings
mdrxy Nov 13, 2025
93e5afe
validate client presence on generate and stream methods
mdrxy Nov 13, 2025
2fef210
rfc: retry logic
mdrxy Nov 13, 2025
4fdf6c8
rfc: use `Blob` in data content block in `_convert_to_parts`
mdrxy Nov 13, 2025
0066691
fix a typing error
mdrxy Nov 13, 2025
439f047
another typing fix
mdrxy Nov 13, 2025
835d09e
use `ToolCodeExecution`
mdrxy Nov 13, 2025
b5a34bf
typing, linting use `model_dump` instead of `proto.Message.to_dict`
mdrxy Nov 13, 2025
12b2a45
new function call
mdrxy Nov 13, 2025
6b7d5d9
rearrange `_common` fields and remove `client_options`
mdrxy Nov 13, 2025
95d9b7f
big kahuna
mdrxy Nov 13, 2025
4acddd2
Merge branch 'main' into mdrxy/migrate-google-genai
mdrxy Nov 13, 2025
d0f085c
test temp fallback
mdrxy Nov 13, 2025
27c41e4
update unit test
mdrxy Nov 13, 2025
ac04692
Merge branch 'main' into mdrxy/migrate-google-genai
mdrxy Nov 13, 2025
8609ba8
Merge branch 'main' into mdrxy/migrate-google-genai
mdrxy Nov 13, 2025
a915eb2
Merge branch 'main' into mdrxy/migrate-google-genai
mdrxy Nov 13, 2025
377de89
Merge branch 'main' into mdrxy/migrate-google-genai
mdrxy Nov 13, 2025
5e662d3
Merge branch 'main' into mdrxy/migrate-google-genai
mdrxy Nov 14, 2025
24c7e57
Merge branch 'main' into mdrxy/migrate-google-genai
mdrxy Nov 14, 2025
3aeba83
Merge branch 'main' into mdrxy/migrate-google-genai
mdrxy Nov 14, 2025
13fdf1f
finish removal of aqa
mdrxy Nov 14, 2025
e6c4b9c
bump for CI
mdrxy Nov 14, 2025
bdd76d0
bump for CI
mdrxy Nov 14, 2025
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
122 changes: 57 additions & 65 deletions libs/genai/langchain_google_genai/_common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from importlib import metadata
from typing import Any, TypedDict
from typing import Any

from google.api_core.gapic_v1.client_info import ClientInfo
from langchain_core.utils import secret_from_env
Expand All @@ -11,6 +11,7 @@
HarmCategory,
MediaResolution,
Modality,
SafetySetting,
)

_TELEMETRY_TAG = "remote_reasoning_engine"
Expand Down Expand Up @@ -39,19 +40,70 @@ class _BaseGoogleGenerativeAI(BaseModel):
["GOOGLE_API_KEY", "GEMINI_API_KEY"], default=None
),
)
"""Google AI API key.
"""Google AI API key. Used for Gemini API.

If not specified, will check the env vars `GOOGLE_API_KEY` and `GEMINI_API_KEY` with
precedence given to `GOOGLE_API_KEY`.

!!! warning "Vertex AI"

To use `langchain-google-genai` with Vertex AI, you must provide a `credentials`
object instead of an API key.
"""

credentials: Any = None
"""The default custom credentials to use when making API calls.
"""The default custom credentials to use when making API calls. Used for Vertex AI.

If not provided, credentials will be ascertained from the `GOOGLE_API_KEY`
or `GEMINI_API_KEY` env vars with precedence given to `GOOGLE_API_KEY`.
"""

base_url: str | dict | None = Field(default=None, alias="client_options")
"""Base URL to use for the API client.

If not provided, will default to the public API at
`https://generativelanguage.googleapis.com`.


- **REST transport** (`transport="rest"`): Accepts full URLs with paths

- `https://api.example.com/v1/path`
- `https://webhook.site/unique-path`

- **gRPC transports** (`transport="grpc"` or `transport="grpc_asyncio"`): Only
accepts `hostname:port` format

- `api.example.com:443`
- `custom.googleapis.com:443`
- `https://api.example.com` (auto-formatted to `api.example.com:443`)
- NOT `https://webhook.site/path` (paths are not supported in gRPC)
- NOT `api.example.com/path` (paths are not supported in gRPC)

!!! note

Typed to accept `dict` to support backwards compatiblity for the (now removed)
`client_options` param.

If a `dict` is passed in, it will only extract the `'api_endpoint'` key.
"""

transport: str | None = Field(
default=None,
alias="api_transport",
)
"""A string, one of: `['rest', 'grpc', 'grpc_asyncio']`.

The Google client library defaults to `'grpc'` for sync clients.

For async clients, `'rest'` is converted to `'grpc_asyncio'` unless
a custom endpoint is specified.
"""

additional_headers: dict[str, str] | None = Field(
default=None,
)
"""Key-value dictionary representing additional headers for the model call"""

temperature: float = 0.7
"""Run inference with this temperature.

Expand Down Expand Up @@ -95,63 +147,6 @@ class _BaseGoogleGenerativeAI(BaseModel):
timeout: float | None = Field(default=None, alias="request_timeout")
"""The maximum number of seconds to wait for a response."""

client_options: dict | None = Field(
default=None,
)
"""A dictionary of client options to pass to the Google API client.

Example: `api_endpoint`

!!! warning

If both `client_options['api_endpoint']` and `base_url` are specified,
the `api_endpoint` in `client_options` takes precedence.
"""

base_url: str | None = Field(
default=None,
)
"""Base URL to use for the API client.

This is a convenience alias for `client_options['api_endpoint']`.

- **REST transport** (`transport="rest"`): Accepts full URLs with paths

- `https://api.example.com/v1/path`
- `https://webhook.site/unique-path`

- **gRPC transports** (`transport="grpc"` or `transport="grpc_asyncio"`): Only
accepts `hostname:port` format

- `api.example.com:443`
- `custom.googleapis.com:443`
- `https://api.example.com` (auto-formatted to `api.example.com:443`)
- NOT `https://webhook.site/path` (paths are not supported in gRPC)
- NOT `api.example.com/path` (paths are not supported in gRPC)

!!! warning

If `client_options` already contains an `api_endpoint`, this parameter will be
ignored in favor of the existing value.
"""

transport: str | None = Field(
default=None,
alias="api_transport",
)
"""A string, one of: `['rest', 'grpc', 'grpc_asyncio']`.

The Google client library defaults to `'grpc'` for sync clients.

For async clients, `'rest'` is converted to `'grpc_asyncio'` unless
a custom endpoint is specified.
"""

additional_headers: dict[str, str] | None = Field(
default=None,
)
"""Key-value dictionary representing additional headers for the model call"""

response_modalities: list[Modality] | None = Field(
default=None,
)
Expand All @@ -178,7 +173,7 @@ class _BaseGoogleGenerativeAI(BaseModel):
!!! example

```python
from google.generativeai.types.safety_types import HarmBlockThreshold, HarmCategory
from google.genai.types import HarmBlockThreshold, HarmCategory

safety_settings = {
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
Expand Down Expand Up @@ -238,7 +233,4 @@ def get_client_info(module: str | None = None) -> "ClientInfo":
)


class SafetySettingDict(TypedDict):
category: HarmCategory

threshold: HarmBlockThreshold
SafetySettingDict = SafetySetting
46 changes: 40 additions & 6 deletions libs/genai/langchain_google_genai/_enums.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
import google.ai.generativelanguage_v1beta as genai
from google.genai.types import (
BlockedReason,
HarmBlockThreshold,
HarmCategory,
MediaModality,
MediaResolution,
Modality,
SafetySetting,
)

HarmBlockThreshold = genai.SafetySetting.HarmBlockThreshold
HarmCategory = genai.HarmCategory
Modality = genai.GenerationConfig.Modality
MediaResolution = genai.GenerationConfig.MediaResolution
BlockedReason = BlockedReason
HarmBlockThreshold = HarmBlockThreshold
HarmCategory = HarmCategory
MediaModality = MediaModality
MediaResolution = MediaResolution
SafetySetting = SafetySetting

__all__ = ["HarmBlockThreshold", "HarmCategory", "MediaResolution", "Modality"]
__all__ = [
"BlockedReason",
"HarmBlockThreshold",
"HarmCategory",
"MediaModality",
"MediaResolution",
"Modality",
"SafetySetting",
]

# Migration notes:
# - Added:
# - `BlockedReason`
# - `SafetySetting`
#
# Parity between generativelanguage_v1beta and genai.types
# - `HarmBlockThreshold`: equivalent
# - `HarmCategory`: there are a few Vertex-only and categories not supported by Gemini
# - `MediaResolution`: equivalent
#
# `MediaModality` has additional modalities not present in `Modality`:
# - `VIDEO`
# - `DOCUMENT`
#
# TODO: investigate why both? Or not just use `MediaModality` everywhere?
Loading
Loading