crfeate_react_agent does not support already bound tools with gemini models #295

Closed
opened 2026-02-20 17:35:43 -05:00 by yindo · 1 comment
Owner

Originally created by @vidit-bhatia on GitHub (Oct 30, 2024).

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangGraph/LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangGraph/LangChain rather than my code.
  • I am sure this is better as an issue rather than a GitHub discussion, since this is a LangGraph bug and not a design question.

Example Code

from langchain_google_genai import ChatGoogleGenerativeAI
from configs.configs import llm_api_key
from langchain.tools import tool, BaseTool, StructuredTool
from typing import Annotated, List, Dict  
from string import Template 
import logging
from langchain_core.runnables.config import RunnableConfig
from json import loads, dumps
from typing import Optional, Type

llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash-8b",google_api_key=llm_api_key.gemini_api_key,temperature=1, tool_call  = True)

@tool
def authoring_tool(user_information: RunnableConfig,
           template: Annotated[str,"The template created by LLM to be used for personalization"]) -> str:
    """The tool gets a python string template as an input and the user information . Returns the result of personalizing the template with the user information"""
    # t = Template(template) 
    user = user_information.get("configurable", {}).get("user")
    # final_to_print = t.substitute(loads(user)) 
    final_to_print = template.format(user)
    logging.info(final_to_print)
    return final_to_print


authoring_tool_llm = llm.bind_tools(tools=[authoring_tool],tool_choice="any")
author_agent = create_react_agent(authoring_tool_llm, tools=[authoring_tool],state_modifier=author_agent_prompt)

Error Message and Stack Trace (if applicable)

Traceback (most recent call last):
  File "/Users/vibhatia/anaconda3/lib/python3.11/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vibhatia/anaconda3/lib/python3.11/runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "/Users/vibhatia/.vscode/extensions/ms-python.debugpy-2024.12.0-darwin-arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 71, in <module>
    cli.main()
  File "/Users/vibhatia/.vscode/extensions/ms-python.debugpy-2024.12.0-darwin-arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 501, in main
    run()
  File "/Users/vibhatia/.vscode/extensions/ms-python.debugpy-2024.12.0-darwin-arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 351, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/Users/vibhatia/.vscode/extensions/ms-python.debugpy-2024.12.0-darwin-arm64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 310, in run_path
    return _run_module_code(code, init_globals, run_name, pkg_name=pkg_name, script_name=fname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vibhatia/.vscode/extensions/ms-python.debugpy-2024.12.0-darwin-arm64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 127, in _run_module_code
    _run_code(code, mod_globals, init_globals, mod_name, mod_spec, pkg_name, script_name)
  File "/Users/vibhatia/.vscode/extensions/ms-python.debugpy-2024.12.0-darwin-arm64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 118, in _run_code
    exec(code, run_globals)
  File "/Users/vibhatia/Documents/external/Agents/MarketingAgent/marketingagents/src/usecases/B2BResearchMarketing/main.py", line 6, in <module>
    from Agents import  author_node, supervisor_agent, send_email_node,members
  File "/Users/vibhatia/Documents/external/Agents/MarketingAgent/marketingagents/src/usecases/B2BResearchMarketing/Agents.py", line 33, in <module>
    author_agent = create_react_agent(authoring_tool_llm, tools=[authoring_tool,send_email_tool],state_modifier=author_agent_prompt)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vibhatia/anaconda3/lib/python3.11/site-packages/langgraph/_api/deprecation.py", line 80, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vibhatia/anaconda3/lib/python3.11/site-packages/langgraph/prebuilt/chat_agent_executor.py", line 511, in create_react_agent
    if _should_bind_tools(model, tool_classes):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/vibhatia/anaconda3/lib/python3.11/site-packages/langgraph/prebuilt/chat_agent_executor.py", line 139, in _should_bind_tools
    raise ValueError(
ValueError: Number of tools in the model.bind_tools() and tools passed to create_react_agent must match

Description

I am trying to use the gemini models ChatGoogleGenerativeAI library from langchain_google_genai to create a react agent using the pre build create_react_agent function . But I need to bind the tools before calling the create_react_agent as I nned to pass some specific tool config. when I do this _should_bind_tools gives error raise ValueError(f"Missing tools '{missing_tools}' in the model.bind_tools()")

System Info

langchain==0.3.4
langchain-core==0.3.13
langchain-google-genai==2.0.1
langchain-openai==0.2.4
langchain-text-splitters==0.3.0
langgraph==0.2.39
langgraph-checkpoint==2.0.1
langgraph-sdk==0.1.33

Originally created by @vidit-bhatia on GitHub (Oct 30, 2024). ### Checked other resources - [X] I added a very descriptive title to this issue. - [X] I searched the [LangGraph](https://langchain-ai.github.io/langgraph/)/LangChain documentation with the integrated search. - [X] I used the GitHub search to find a similar question and didn't find it. - [X] I am sure that this is a bug in LangGraph/LangChain rather than my code. - [X] I am sure this is better as an issue [rather than a GitHub discussion](https://github.com/langchain-ai/langgraph/discussions/new/choose), since this is a LangGraph bug and not a design question. ### Example Code ```python from langchain_google_genai import ChatGoogleGenerativeAI from configs.configs import llm_api_key from langchain.tools import tool, BaseTool, StructuredTool from typing import Annotated, List, Dict from string import Template import logging from langchain_core.runnables.config import RunnableConfig from json import loads, dumps from typing import Optional, Type llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash-8b",google_api_key=llm_api_key.gemini_api_key,temperature=1, tool_call = True) @tool def authoring_tool(user_information: RunnableConfig, template: Annotated[str,"The template created by LLM to be used for personalization"]) -> str: """The tool gets a python string template as an input and the user information . Returns the result of personalizing the template with the user information""" # t = Template(template) user = user_information.get("configurable", {}).get("user") # final_to_print = t.substitute(loads(user)) final_to_print = template.format(user) logging.info(final_to_print) return final_to_print authoring_tool_llm = llm.bind_tools(tools=[authoring_tool],tool_choice="any") author_agent = create_react_agent(authoring_tool_llm, tools=[authoring_tool],state_modifier=author_agent_prompt) ``` ### Error Message and Stack Trace (if applicable) ```shell Traceback (most recent call last): File "/Users/vibhatia/anaconda3/lib/python3.11/runpy.py", line 198, in _run_module_as_main return _run_code(code, main_globals, None, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/vibhatia/anaconda3/lib/python3.11/runpy.py", line 88, in _run_code exec(code, run_globals) File "/Users/vibhatia/.vscode/extensions/ms-python.debugpy-2024.12.0-darwin-arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 71, in <module> cli.main() File "/Users/vibhatia/.vscode/extensions/ms-python.debugpy-2024.12.0-darwin-arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 501, in main run() File "/Users/vibhatia/.vscode/extensions/ms-python.debugpy-2024.12.0-darwin-arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 351, in run_file runpy.run_path(target, run_name="__main__") File "/Users/vibhatia/.vscode/extensions/ms-python.debugpy-2024.12.0-darwin-arm64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 310, in run_path return _run_module_code(code, init_globals, run_name, pkg_name=pkg_name, script_name=fname) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/vibhatia/.vscode/extensions/ms-python.debugpy-2024.12.0-darwin-arm64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 127, in _run_module_code _run_code(code, mod_globals, init_globals, mod_name, mod_spec, pkg_name, script_name) File "/Users/vibhatia/.vscode/extensions/ms-python.debugpy-2024.12.0-darwin-arm64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 118, in _run_code exec(code, run_globals) File "/Users/vibhatia/Documents/external/Agents/MarketingAgent/marketingagents/src/usecases/B2BResearchMarketing/main.py", line 6, in <module> from Agents import author_node, supervisor_agent, send_email_node,members File "/Users/vibhatia/Documents/external/Agents/MarketingAgent/marketingagents/src/usecases/B2BResearchMarketing/Agents.py", line 33, in <module> author_agent = create_react_agent(authoring_tool_llm, tools=[authoring_tool,send_email_tool],state_modifier=author_agent_prompt) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/vibhatia/anaconda3/lib/python3.11/site-packages/langgraph/_api/deprecation.py", line 80, in wrapper return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/Users/vibhatia/anaconda3/lib/python3.11/site-packages/langgraph/prebuilt/chat_agent_executor.py", line 511, in create_react_agent if _should_bind_tools(model, tool_classes): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/vibhatia/anaconda3/lib/python3.11/site-packages/langgraph/prebuilt/chat_agent_executor.py", line 139, in _should_bind_tools raise ValueError( ValueError: Number of tools in the model.bind_tools() and tools passed to create_react_agent must match ``` ### Description I am trying to use the gemini models ChatGoogleGenerativeAI library from langchain_google_genai to create a react agent using the pre build create_react_agent function . But I need to bind the tools before calling the create_react_agent as I nned to pass some specific tool config. when I do this _should_bind_tools gives error raise ValueError(f"Missing tools '{missing_tools}' in the model.bind_tools()") ### System Info langchain==0.3.4 langchain-core==0.3.13 langchain-google-genai==2.0.1 langchain-openai==0.2.4 langchain-text-splitters==0.3.0 langgraph==0.2.39 langgraph-checkpoint==2.0.1 langgraph-sdk==0.1.33
yindo closed this issue 2026-02-20 17:35:45 -05:00
Author
Owner

@vbarda commented on GitHub (Oct 31, 2024):

hi @vidit-bhatia -- please update to langchain-google-genai==2.0.3. let me know if that doesn't resolve the issue for you. i confirmed with the code below that it's working as intended

from langchain_core.tools import tool
from langchain_google_genai import ChatGoogleGenerativeAI
from langgraph.prebuilt import create_react_agent

llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash-8b", tool_call=True)

@tool
def add(a: int, b: int):
    """Add two numbers"""
    return a + b

llm_with_tools = llm.bind_tools([add])
# this has "tools" key
llm_with_tools.kwargs

# this works as expected for me
agent = create_react_agent(llm_with_tools, [add])
agent.invoke({"messages": [("user", "what's 2 + 3. use tools")]})```

i am using the latest version of `langchain-google-genai` (2.0.3)
@vbarda commented on GitHub (Oct 31, 2024): hi @vidit-bhatia -- please update to `langchain-google-genai==2.0.3`. let me know if that doesn't resolve the issue for you. i confirmed with the code below that it's working as intended ```python from langchain_core.tools import tool from langchain_google_genai import ChatGoogleGenerativeAI from langgraph.prebuilt import create_react_agent llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash-8b", tool_call=True) @tool def add(a: int, b: int): """Add two numbers""" return a + b llm_with_tools = llm.bind_tools([add]) # this has "tools" key llm_with_tools.kwargs # this works as expected for me agent = create_react_agent(llm_with_tools, [add]) agent.invoke({"messages": [("user", "what's 2 + 3. use tools")]})``` i am using the latest version of `langchain-google-genai` (2.0.3)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#295