Skip to content

Conversation

@challenger71498
Copy link

@challenger71498 challenger71498 commented Feb 1, 2026

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

2. Or, if no issue exists, describe the change:

Problem:
Users need the ability to inject custom Google GenAI Client instances for testing, custom authentication, or different configurations. Currently, the Gemini model class always creates its own internal clients for both standard API calls and Live API streaming, making it difficult to override client behavior without modifying internal code.

Solution:
Added two new optional parameters to the Gemini model class:

  • custom_api_client: Allows injecting a custom client for generate_content operations
  • custom_live_api_client: Allows injecting a custom client for Live API (bi-directional streaming) operations

When these parameters are provided, they override the default client creation logic in the api_client and _live_api_client properties respectively. This enables users to:

  • Use custom authentication configurations
  • Inject mock clients for testing
  • Configure different API versions or endpoints
  • Apply custom HTTP options or interceptors

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Summary of pytest results:
Added two comprehensive unit tests in tests/unittests/models/test_google_llm.py:

  1. test_custom_api_client_is_used_for_generate_content - Verifies that custom_api_client is properly used for standard API operations
  2. test_custom_live_api_client_is_used_for_connect - Verifies that custom_live_api_client is properly used for Live API streaming operations

Both tests use mock clients to verify the injection mechanism works correctly without making actual API calls.

Manual End-to-End (E2E) Tests:

To manually test these changes:

  1. Test custom_api_client:
from google.genai import Client
from google.adk.agents import Agent
from google.adk.models import Gemini

custom_client = Client(api_key="your_custom_key")
agent = Agent(
    name="test_agent",
    model=Gemini(custom_api_client=custom_client)
)
# Run agent and verify it uses the custom client
  1. Test custom_live_api_client:
from google.genai import Client, types
from google.adk.agents import Agent
from google.adk.models import Gemini

live_client = Client(
    http_options=types.HttpOptions(api_version="v1beta1")
)
agent = Agent(
    name="live_agent",
    model=Gemini(custom_live_api_client=live_client)
)
# Use runner.run_live() and verify the custom live client is used

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

Implementation details:

  • Both parameters are marked with Field(exclude=True, frozen=True, repr=False) to ensure they don't interfere with Pydantic serialization, are immutable after creation, and don't clutter string representations
  • The parameters are Optional[Client] and default to None
  • When None, the existing client creation logic is used (backward compatible)
  • Moved Client import from TYPE_CHECKING block to top-level imports since it's now used in type annotations for runtime fields
  • Added comprehensive docstrings with usage examples for both parameters
  • Cleaned up unused imports and simplified comments in follow-up commits

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @challenger71498, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the Gemini model's extensibility by allowing users to provide custom Google GenAI Client instances. This change addresses the limitation where the model previously created its own internal clients, making it difficult to customize behavior for testing, authentication, or specific configurations. By enabling client injection, the PR provides greater control and flexibility, allowing developers to tailor the Gemini model's interaction with the GenAI API to their specific needs without modifying the core library code.

Highlights

  • Custom Client Injection: Introduced custom_api_client and custom_live_api_client parameters to the Gemini model class, allowing users to inject their own Google GenAI Client instances for standard API calls and Live API streaming, respectively.
  • Enhanced Flexibility: This feature enables advanced use cases such as custom authentication, injecting mock clients for testing, configuring different API versions or endpoints, and applying custom HTTP options or interceptors.
  • Backward Compatibility & Pydantic Integration: The new parameters are optional, default to None, and are marked with Field(exclude=True, frozen=True, repr=False) to ensure they are compatible with Pydantic serialization, are immutable, and do not clutter string representations.
  • Comprehensive Testing: Two new unit tests (test_custom_api_client_is_used_for_generate_content and test_custom_live_api_client_is_used_for_connect) have been added to verify the correct usage and functionality of the custom client injection mechanism.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@adk-bot adk-bot added the core [Component] This issue is related to the core interface and implementation label Feb 1, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a valuable feature by adding support for custom google.genai.Client instances to the Gemini model class. This greatly improves flexibility for testing, custom authentication, and other advanced configurations. The implementation is clean, and the accompanying unit tests are thorough and well-written. I have a couple of minor suggestions to improve the code samples in the docstrings to make them more complete for users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core [Component] This issue is related to the core interface and implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow passing a preconfigured google.genai.Client to google.adk.models.Gemini

2 participants