Skip to content
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

Tool calling broken for Gemini with legacy agent #711

Open
Finndersen opened this issue Jan 24, 2025 · 3 comments
Open

Tool calling broken for Gemini with legacy agent #711

Finndersen opened this issue Jan 24, 2025 · 3 comments

Comments

@Finndersen
Copy link
Contributor

Example Code

from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
from langchain.agents.agent import AgentExecutor
from langchain.agents import create_tool_calling_agent

model = ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp")
prompt = ChatPromptTemplate.from_messages(
        [
            ("system", PROMPT_TEMPLATE),
            ("placeholder", "{chat_history}"),
            ("human", "{input}"),
            ("placeholder", "{agent_scratchpad}"),
        ]
    )
tools = [my_tool, ...]

agent = create_tool_calling_agent(model, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

response = agent_executor.invoke({"input": "call my_tool"})

Error Message and Stack Trace (if applicable)

langchain_google_genai.chat_models.ChatGoogleGenerativeAIError: Invalid argument provided to Gemini: 400 * GenerateContentRequest.contents[2].parts[0].function_response.name: Name cannot be empty.

Description

Tool calling with Gemini requires the tool name to be returned in the tool response, but ToolMessage.name is not being set when created by a legacy agent.

Instead, ToolMessage.additional_kwargs is set to: {"name": "my_tool"}, but this isn't used when building the message Parts to send to Gemini.

So this function needs to be updated to use name = ToolMessage.additional_kwargs["name"]

@haard7
Copy link

haard7 commented Jan 26, 2025

I am having the same error for multi agent environment where I have system prompt as well.

ChatGoogleGenerativeAI is not able to fullfil that, while chatVertexAI is capable enough sometime.

@dhairyakataria
Copy link

I am having the same error when trying to execute agent_executor using ChatGoogleGenerativeAI.

Does anyone has any solution for this?

@Finndersen
Copy link
Contributor Author

Downgrading to langchain-google-genai==2.0.7 should fix it until a new version is released with the fix I made

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants