-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't use ChatGoogleGenerativeAI with tools in Langraph #6833
Comments
Hello @cnarte! I'm here to help you with any bugs, questions, or contributions you have. Let's tackle this issue together! I found a similar issue where users encountered a SolutionTo address this issue, you should ensure that the response from the API is correctly structured and contains the Here is a potential fix in the async _generate(
messages: BaseMessage[],
options: this["ParsedCallOptions"],
runManager?: CallbackManagerForLLMRun
): Promise<ChatResult> {
const prompt = convertBaseMessagesToContent(
messages,
this._isMultimodalModel
);
const parameters = this.invocationParams(options);
// Handle streaming
if (this.streaming) {
const tokenUsage: TokenUsage = {};
const stream = this._streamResponseChunks(messages, options, runManager);
const finalChunks: Record<number, ChatGenerationChunk> = {};
for await (const chunk of stream) {
if (chunk.text) {
// Process the chunk
} else {
console.error("Received chunk without text property:", chunk);
}
}
} else {
const response = await this.client.generateContent({
contents: [prompt],
...parameters,
});
if (response && response.text) {
return mapGenerateContentResultToChatResult(response);
} else {
throw new Error("Response does not contain text property");
}
}
} This code adds checks to ensure that the Additional Steps
By following these steps, you should be able to avoid the |
But how can I edit the Package implementation? |
To edit the package implementation, you can follow these steps:
Here is a code snippet to guide you through the process: # Extract the package
yarn patch @langchain/google-genai
# Make your changes in the extracted directory
# (The directory path will be displayed in the terminal output)
# Commit your changes
yarn patch-commit -s <path-to-extracted-directory> This will store a patch file based on your changes, which you can then apply to your project [1]. |
@dosu that wont solve problem just throw a new error. |
To resolve the issue with Here is a revised version of the code with additional checks to ensure that the properties are correctly accessed: import { StructuredTool } from "@langchain/core/tools";
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
import { z } from "zod";
const model = new ChatGoogleGenerativeAI({
model: "gemini-pro",
});
// Define your tool
class FakeBrowserTool extends StructuredTool {
schema = z.object({
url: z.string(),
query: z.string().optional(),
});
name = "fake_browser_tool";
description =
"useful for when you need to find something on the web or summarize a webpage.";
async _call(_: z.infer<this["schema"]>): Promise<string> {
return "fake_browser_tool";
}
}
const tool = new FakeBrowserTool();
// Bind your tools to the model
const modelWithTools = model.withStructuredOutput(tool.schema, {
name: tool.name, // this is optional
});
// Optionally, you can pass just a Zod schema, or JSONified Zod schema
// const modelWithTools = model.withStructuredOutput(
// zodSchema,
// );
const res = await modelWithTools.invoke([
[
"human",
"Search the web and tell me what the weather will be like tonight in new york. use a popular weather website",
],
]);
if (res && res.url && res.query) {
console.log(res);
} else {
console.error("Unexpected response structure:", res);
}
/*
{
url: 'https://www.accuweather.com/en/us/new-york-ny/10007/night-weather-forecast/349014',
query: 'weather tonight'
}
*/ This code includes a check to ensure that Additionally, make sure you have installed the npm install @langchain/google-genai Refer to the LangChain documentation for more details on using the |
I'd need to dig into the A slightly biased suggestion, but you may wish to switch to using the Make sure you are using version 0.1.0 of either and version 0.1.1 of |
@cnarte can you provide some code I can use to reproduce this error? |
I'm trying to use google generative-ai package with langraph but getting a unhandled response for llm call inside the package with tool call.
Example Code
The following code File < node_modules@langchain\google-genai\dist\chat_models.js > ::
The text was updated successfully, but these errors were encountered: