Repository navigation
Allow passing a preconfigured google.genai.Client to google.adk.models.Gemini #2560
Description
When using the Python ADK with Gemini models, the Gemini class internally creates its own google.genai.Client based on environment variables or ADC.
There is no way to inject a preconfigured genai.Client instance directly.
This makes it impossible to control the underlying genai.Client entirely in code (e.g., to avoid environment variable reliance or to set vertexai=True, project, and location programmatically).
In contrast, the Java ADK’s Gemini wrapper accepts a Client object, which allows this flexibility and makes the model configuration explicit when called, as can be seen from the docs
// --- Example #2: using a powerful Gemini Pro model with API Key in model ---
LlmAgent agentGeminiPro =
LlmAgent.builder()
// Use the latest generally available Pro model identifier
.model(new Gemini("gemini-2.5-pro-preview-03-25",
Client.builder()
.vertexAI(false)
.apiKey("API_KEY") // Set the API Key (or) project/ location
.build()))
// Or, you can also directly pass the API_KEY
// .model(new Gemini("gemini-2.5-pro-preview-03-25", "API_KEY"))
.name("gemini_pro_agent")
.instruction("You are a powerful and knowledgeable Gemini assistant.")
// ... other agent parameters
.build();Is your feature request related to a problem? Please describe.
I need more fine-grained control over the configuration of Gemini models - specifically the ability to set different locations (and other client settings) for different agents running within the same Python ADK application. Currently, model configuration is determined globally via environment variables or ADC, which makes it difficult to run agents in the same process that target different Vertex AI regions.
Describe the solution you'd like
Add support for passing an existing google.genai.Client instance into the Gemini model constructor (similar to the Java ADK API).
Example desired usage:
from google import genai
from google.adk.models import Gemini
from google.adk.agents import LlmAgent
client = genai.Client(vertexai=True, project="my-project", location="us-central1")
gemini_model = Gemini(model="gemini-2.5-flash", client=client) # or api_client=client
agent = LlmAgent(name="my_agent", model=gemini_model, instruction="Be concise.")Describe alternatives you've considered
-
Environment variables: Setting
GOOGLE_GENAI_USE_VERTEXAI,GOOGLE_CLOUD_PROJECT, andGOOGLE_CLOUD_LOCATIONprogrammatically before importing ADK. This works, but is indirect, less explicit, and affects the entire process environment - e.g., it prevents using different locations for different model. -
Custom
BaseLlmwrapper: Creating aBaseLlmsubclass that usesgoogle.genai.Clientdirectly. This is more verbose, requires reimplementing request/response mapping logic that already exists in ADK, and adds maintenance overhead.
Additional context
Since this is already possible in the Java ADK, adding the same capability to the Python ADK would:
- Improve cross-language parity.
- Make the model configuration explicit and local to the code using it.
- Reduce reliance on environment variable management in containerized or sandboxed environments.
- Enable scenarios where multiple agents in the same application require different Vertex AI regions or other client-level settings.
Activity
mikeedjones commentedon Aug 29, 2025
The
Gemini._live_api_clientalso needs this feature.surfai commentedon Sep 4, 2025
We had resources mostly in same region not us-central1. As model endpoint and agent engine region are somehow attached (this issue). Agent engine also behaves differently in Asia-east1 and us-central as a result. live-api will certainly amplify such needs further. We hope at least to bring back agent engine to nearby region closer to assets specially cloud sql and users despite latency of inter continental Gemini calls before endpoints propagate to more regions!
Jacksunwei commentedon Sep 12, 2025
@martina-zxy could you confirm which language are you using? You sample reference Java, but you're stating you use ADK Python.
martina-zxy commentedon Sep 12, 2025
@Jacksunwei I’m using Python. I included the Java code snippet to show that this feature is already available in the Java SDK.
jwdjj commentedon Sep 30, 2025
suggesting a workaround here, let me know if this works.
6 remaining items
mikeedjones commentedon Nov 12, 2025
I'm not sure what fix you're referring to, but you can't set the client in the
Geminiinitialisation in 1.18.0 which was the next release follwing @GWeale 's comment.challenger71498 commentedon Feb 1, 2026
Hi @martina-zxy @mikeedjones cc. @GWeale,
I made a simple PR #4345 to support custom client injection when initializing
Gemini.I agree with what @mikeedjones mentioned, so I created a dedicated argument to inject the client when initializing the
Gemini. Also addedfrozen=Trueto make them immutable.The PR will not effect users not using custom clients. The import of
google.genai.Clientis moved out of theTYPE_CHECKINGscope for custom client attributes, but I think it would be fine.Although six months is a long time, but if you are interested with the improvement I've suggested, you may take a look at the PR I've opened, and give it a try. :)
Any suggestions and corrections are welcome.
fix: Add support for injecting a custom google.genai.Client into Gemi…
GWeale commentedon Feb 24, 2026
Reopening as this will be fixed in release 1.27 not release 1.26
fix: Add support for injecting a custom google.genai.Client into Gemi…
mynewestgitaccount commentedon Mar 11, 2026
@GWeale this needs to be opened again. The commit pulling it from 1.26 re-closed it.
jibone commentedon Mar 13, 2026
Hi, just wondering if this was added on release 1.27, I don't see it in the changelog.
mynewestgitaccount commentedon Mar 20, 2026
The comment saying it would be in 1.27 was wrong. @GWeale or @sasha-gitg any help?
fix: Add a fallback user message to LiteLLM requests if the last user…
fix: Add support for injecting a custom google.genai.Client into Gemi…
fix: Add support for injecting a custom google.genai.Client into Gemi…