Skip to content

LangChain Google GenAI ​

ChatGoogleGenerativeAI from langchain-google-genai, on plugboard's Gemini API.

Connect ​

base_url is the plugboard origin; the client adds /v1beta:

py
return ChatGoogleGenerativeAI(
    model=wire(model),
    google_api_key=api_key or os.environ.get("PLUGBOARD_KEY", ""),
    base_url=BASE_URL,
    max_output_tokens=1024,
    max_retries=0,
)

Chat ​

py
response = client(model).invoke([HumanMessage("Reply with exactly: pong")])

Tool calls ​

py
@tool
def get_weather(city: str) -> str:
    """Current weather for a city."""
    return TOOL_RESULT
py
llm = client(model).bind_tools([get_weather])
first = llm.invoke(messages)
call = first.tool_calls[0]  # {"name", "args", "id"}; the id goes back on the ToolMessage
messages += [first, ToolMessage(content=get_weather.invoke(call["args"]), tool_call_id=call["id"])]
second = llm.invoke(messages)

LangChain makes up the tool call id; Gemini sends none.

Streaming ​

py
text = ""
finish = None
usage = None
for chunk in client(model).stream([HumanMessage("Reply with exactly: pong")]):
    text += str(chunk.content)  # each chunk carries the next text delta
    finish = chunk.response_metadata.get("finish_reason") or finish
    usage = dict(chunk.usage_metadata) if chunk.usage_metadata else usage

Images ​

py
# A 32px red PNG as a data URL (examples/_shared/red32.dataurl.txt). LangChain converts it to inline data for Gemini.
red = RED_PNG.read_text().strip()
response = client(model).invoke(
    [HumanMessage(content=[{"type": "text", "text": "What color is this image? One word."}, {"type": "image_url", "image_url": {"url": red}}])]
)

Structured output ​

py
llm = client(model).with_structured_output(Meal)
meal = llm.invoke([HumanMessage("Estimate: 2 boiled eggs and a slice of toast.")])

Model names ​

A # in a model name must be sent as %23; a / goes as it is. See Gemini model names.

Limits ​

  • Errors raise ChatGoogleGenerativeAIError, with Google's error body in the message.
  • The JavaScript package @langchain/google-genai cannot cancel a stream cleanly: an abort reports Error reading from the stream, raised by the @google/generative-ai SDK it uses. To cancel a stream from JavaScript, use @google/genai.