Skip to content

langchain-google-genai library fails to authenticate using an API key in Github Actions #1271

Description

@mikebz

Issue Summary:

The langchain-google-genai library fails to authenticate using an API key in an environment where Application Default
Credentials (ADC) are not available. Despite providing the GEMINI_API_KEY through multiple supported methods, the library defaults to
searching for ADC, resulting in a google.auth.exceptions.DefaultCredentialsError.

Steps to Reproduce:

  1. Set up github actions with the GEMINI_API_KEY environment variable defined, but without Application Default Credentials configured.
  2. Instantiate the ChatGoogleGenerativeAI class, providing the API key through any of the following methods:
    • Relying on the GEMINI_API_KEY environment variable.
    • Passing the API key directly to the google_api_key parameter.
    • Using the client_options={"api_key": api_key} parameter.
    • Explicitly setting the transport="rest" parameter.
  3. Attempt to invoke the model.
import os

from langchain.agents import create_agent
from langchain_google_genai import ChatGoogleGenerativeAI
from langgraph.graph.state import CompiledStateGraph

from .calculator import (
    base_calculate,...
)

SYSTEM_PROMPT = """You are a helpful agent
"""


def get_agent() -> CompiledStateGraph:
    api_key = os.environ.get("GEMINI_API_KEY")
    if api_key is None:
        raise ValueError("GEMINI_API_KEY environment variable not set.")

    # Set GOOGLE_API_KEY to ensure the Google SDK uses API key authentication
    os.environ["GOOGLE_API_KEY"] = api_key

    llm = ChatGoogleGenerativeAI(
        model="gemini-2.5-pro",
        transport="rest",
    )

    agent: CompiledStateGraph = create_agent(
        name="test_agent",
        model=llm,
        tools=[
           ....
        ],
        system_prompt=SYSTEM_PROMPT,
    )

    return agent
import pytest

from mort_ai.assistant import get_agent


@pytest.mark.skipif("GEMINI_API_KEY" not in os.environ, reason="GEMINI_API_KEY environment variable not set")
def test_create_works() -> None:
    agent = get_agent()
    assert agent is not None
    r = agent.invoke({"messages": [{"role": "user", "content": "What can you do?"}]})
    assert r is not None
    msgs = r["messages"]
    assert len(msgs) > 0
    assert msgs[-1].type == "ai"

Expected Behavior:

The ChatGoogleGenerativeAI class should prioritize the provided API key for authentication and successfully connect to the Google
Generative AI API.

Actual Behavior:

The library ignores the provided API key and attempts to find Application Default Credentials. When these are not available, it raises a
google.auth.exceptions.DefaultCredentialsError.

Analysis:

The traceback indicates that the google.auth.default() function is being called, which is responsible for the ADC search. This happens even
when an API key is explicitly provided to the ChatGoogleGenerativeAI class. This suggests that the library is not correctly prioritizing
the API key authentication path.

FAILED tests/test_agent.py::test_create_works - google.auth.exceptions.DefaultCredentialsError: Your default credentials were not found. To set up Application Default Credentials, see https://cloud.google.com/docs/authentication/external/set-up-adc for more information.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions