Skip to content

Support service_tier (Flex/Priority tiers) in ChatGoogleGenerativeAI #1682

Description

@matbdev

Package (Required)

  • langchain-google-genai
  • langchain-google-vertexai
  • langchain-google-community
  • Other / not sure / general

Checked other resources

  • I added a descriptive title to this issue
  • I searched the LangChain documentation and API reference (linked above)
  • I used the GitHub search to find a similar issue and didn't find it
  • I am sure this is a feature request and not a bug report or question

Feature Description

Google recently introduced the Flex inference tier for the Gemini API, which offers a 50% cost reduction in exchange for variable latency. This is highly desirable for latency-tolerant workloads like asynchronous data cataloging or batch processing.

In the official google-genai SDK, this is implemented by passing config={'service_tier': 'flex'} to the generate_content request.

Currently, in langchain-google-genai, there seems to be no native way to propagate the service_tier parameter down to the underlying GenerateContentConfig.

Use Case

I am building a batch data cataloging project using gemini-3-flash-preview with structured outputs. Since this runs in the background and doesn't require real-time latency, the 50% cost savings from the Flex tier is vital for optimizing the budget.

Proposed Solution

Allow service_tier to be defined during ChatGoogleGenerativeAI initialization or captured via .bind(), and ensure it gets properly mapped to the GenerateContentConfig object inside the _build_request_config method.

Alternatives Considered

The only current workaround is to bypass LangChain entirely and use the official google-genai SDK directly. However, this is not ideal as it breaks existing LCEL chains, callback handlers, and the highly convenient .with_structured_output() abstraction, requiring a significant rewrite of the application logic.

Code Example of Attempted Usage

Here is how I intuitively tried to implement it using LangChain, but the configuration isn't passed down to the Google API:

from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.callbacks.base import AsyncCallbackHandler

class VerboseCallbackHandler(AsyncCallbackHandler):
    async def on_llm_error(self, error, **kwargs):
        print(f"  [x] [LangChain] Gemini Request Failed: {type(error).__name__}: {error}", flush=True)

# Initialize the model
llm = ChatGoogleGenerativeAI(
    model="gemini-3-flash-preview",
    temperature=0,
    callbacks=[VerboseCallbackHandler()],
    timeout=1800,
    max_retries=3
)

# Attempt to bind the Flex service tier
llm_flex = llm.bind(service_tier="flex") # or "FLEX"

# Use with structured output
structured_llm = llm_flex.with_structured_output(pydantic_schema, include_raw=True)

Additional Context

A similar issue was recently opened for the JS ecosystem: langchain-ai/langchainjs#10655
Link for the Google page that talks about Flex: https://ai.google.dev/gemini-api/docs/flex-inference

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgenai`langchain-google-genai` package

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions