Skip to main content
Google AI offers a number of different chat models, including the powerful Gemini series. For information on the latest models, their features, context windows, etc. head to the Google AI docs. This will help you getting started with ChatGoogleGenerativeAI chat models. For detailed documentation of all ChatGoogleGenerativeAI features and configurations head to the API reference.
This library will be deprecatedThis library is based on a deprecated library from Google and will be replaced by the ChatGoogle library. New implementations should use the ChatGoogle library instead and existing implementations should consider migrating.

Overview

Integration details

Model features

See the links in the table headers below for guides on how to use specific features.

Setup

You can access Google’s gemini and gemini-vision models, as well as other generative models in LangChain through ChatGoogleGenerativeAI class in the @langchain/google-genai integration package.
You can also access Google’s gemini family of models via the LangChain VertexAI and VertexAI-web integrations. See the Vertex AI integration docs.

Credentials

Get an API key here: https://ai.google.dev/tutorials/setup Then set the GOOGLE_API_KEY environment variable:
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Installation

The LangChain ChatGoogleGenerativeAI integration lives in the @langchain/google-genai package:

Instantiation

Now we can instantiate our model object and generate chat completions:

Invocation

Safety settings

Gemini models have default safety settings that can be overridden. If you are receiving lots of “Safety Warnings” from your models, you can try tweaking the safety_settings attribute of the model. For example, to turn off safety blocking for dangerous content, you can import enums from the @google/generative-ai package, then construct your LLM as follows:

Tool calling

Tool calling with Google AI is mostly the same as tool calling with other models, but has a few restrictions on schema. The Google AI API does not allow tool schemas to contain an object with unknown properties. For example, the following Zod schemas will throw an error: const invalidSchema = z.object({ properties: z.record(z.unknown()) }); and const invalidSchema2 = z.record(z.unknown()); Instead, you should explicitly define the properties of the object field. Here’s an example:

Built in Google search retrieval

Google also offers a built in search tool which you can use to ground content generation in real-world information. Here’s an example of how to use it:
The response also includes metadata about the search result:

Code execution

Google Generative AI also supports code execution. Using the built in CodeExecutionTool, you can make the model generate code, execute it, and use the results in a final completion:
You can also pass this generation back to the model as chat history:

Context caching

Context caching allows you to pass some content to the model once, cache the input tokens, and then refer to the cached tokens for subsequent requests to reduce cost. You can create a CachedContent object using GoogleAICacheManager class and then pass the CachedContent object to your ChatGoogleGenerativeAIModel with enableCachedContent() method.
Note
  • The minimum input token count for context caching is 32,768, and the maximum is the same as the maximum for the given model.

Gemini prompting FAQs

As of the time this doc was written (2023/12/12), Gemini has some restrictions on the types and structure of prompts it accepts. Specifically:
  1. When providing multimodal (image) inputs, you are restricted to at most 1 message of “human” (user) type. You cannot pass multiple messages (though the single human message may have multiple content entries)
  2. System messages are not natively supported, and will be merged with the first human message if present.
  3. For regular chat conversations, messages must follow the human/ai/human/ai alternating pattern. You may not provide 2 AI or human messages in sequence.
  4. Message may be blocked if they violate the safety checks of the LLM. In this case, the model will return an empty response.

API reference

For detailed documentation of all ChatGoogleGenerativeAI features and configurations head to the API reference.