return home

Combining Gemini 2 with LangGraph’s Create REACT Agent

While working on Inflection’s new VC research project, Kepler, I wanted to use Gemini 2’s grounding capabilities to help with the research (as opposed to using a tool like Tavily).

Unfortunately, documentation for Gemini 2’s usage is sparse and LangGraph’s documentation is sparse and the combination of the two is even more sparse.

It also doesn’t help that we’re doing this in TypeScript which is poorly supported by everyone except Vercel’s AI SDK.

Gemini 2

Gemini 2 is Google’s latest generation of AI models. It’s pretty cool in that you can also have it ground it’s responses in its search results. This can help ensure the responses are accurate and relevant, without needing an external tool like Tavily.

LangGraph Create REACT Agent

LangGraph is a framework for building agents. It comes with a prebuilt Create REACT Agent that you can use to build agents which will use tools until they can generate a response.

Combining the Two

Problem

In the documentation for using Google’s Gemini models with LangGraph, it suggests the following:

export const model = new ChatGoogleGenerativeAI({
  model: "gemini-2.0-flash-exp",
  temperature: 0,
  maxRetries: 2,
  apiKey: process.env.GOOGLE_API_KEY,
}).bindTools([
  {
    googleSearch: {},
  },
]);

This won’t work with the createReactAgent function, as it doesn’t support binding tools to the model.

Solution

If you’d like to use Gemini 2’s grounding capabilities with LangGraph’s Create REACT Agent, you can use the following code as a starting point. This is a simplified example just to highlight how to use Gemini 2’s grounding capabilities with LangGraph’s Create REACT Agent.

It’s quite a simple fix, actually, but took a while to figure out.

import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
import { SystemMessage } from "@langchain/core/messages";
import { RunnableConfig } from "@langchain/core/runnables";
import { Tool } from "@langchain/core/tools";
import { createReactAgent } from "@langchain/langgraph/prebuilt";

export const model = new ChatGoogleGenerativeAI({
  model: "gemini-2.0-flash-exp",
  temperature: 0,
  maxRetries: 2,
  apiKey: process.env.GOOGLE_API_KEY,
});

const googleSearchTool = {
  googleSearch: {},
} as unknown as Tool; // Required for search grounding, as Google generates it's own tool call schema.

const agent = createReactAgent({
  llm: model,
  tools: [googleSearchTool /* ...other tools */],
  stateModifier: new SystemMessage("..."),
});

export const node = async (
  state: typeof explorationGraphState.State,
  config?: RunnableConfig
) => {
  const result = await agent.invoke(state, config);
  const lastMessage = result.messages[result.messages.length - 1];
  return {
    messages: [
      new HumanMessage({ content: lastMessage.content, name: "agent" }),
    ],
  };
};