langchain-ai / langchain-google

MIT License
100 stars 119 forks source link

Error:  Invalid argument provided to Gemini: 400 * GenerateContentRequest.tools[0].function_declarations[0].name #471

Open Nandhan-augment opened 2 weeks ago

Nandhan-augment commented 2 weeks ago

### Description: In version 1.0.10 of the langchain-google-genai package, there is an error when using Gemini tools. The error message returned is:

Error: Invalid argument provided to Gemini: 400 * GenerateContentRequest.tools[0].function_declarations[0].name: Invalid function name. Must start with a letter or an underscore. Must be alphameric (a-z, A-Z, 0-9), underscores (_), dots (.) or dashes (-), with a maximum length of 64.

Relevant Code Snippet:

from pydantic import BaseModel, Field
from langchain.tools import BaseTool

class MyCustomTool(BaseTool):
    name = "valid_function_name"  # Name follows the correct conventions
    description = "This is a custom tool for Gemini."
    args_schema: Type[BaseModel] = MyCustomToolInput

    def _run(self):
        # Tool logic
        pass

Affected Version: 1.0.10 Working Version: 1.0.8

Package Information:

langchain==0.2.15 langchain-community==0.2.14 langchain-core==0.2.36 langchain-google-genai==1.0.10 langchain-text-splitters==0.2.2 langchainplus-sdk==0.0.20 langserve==0.2.2 langsmith==0.1.107

lkuligin commented 2 weeks ago

first, you should be importing from langchain_core.tools import BaseTool afaik

can you share a full snippet with reproducible error, please? this runs fine on my end:

from pydantic import BaseModel, Field
from langchain_core.tools import BaseTool
from typing import Type

class MyCustomTool(BaseTool):
    name = "valid_function_name"  # Name follows the correct conventions
    description = "This is a custom tool for Gemini."
    args_schema: Type[BaseModel] = {}

    def _run(self):
        # Tool logic
        pass

from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.documents import Document
from langchain_core.pydantic_v1 import BaseModel, Field

from typing import Optional

llm = ChatGoogleGenerativeAI(
    model='gemini-1.5-pro-001', 
    google_api_key="XXX")
llm1 = llm.bind_tools([MyCustomTool()])
res = llm1.invoke("test tool")
print(res.tool_calls)

returns

[{'name': 'valid_function_name', 'args': {'__arg1': 'foo_bar'}, 'id': '8a0fa7d2-1f65-489e-b6a3-e89527594aee', 'type': 'tool_call'}]