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

custom tool input error #157

Open
farahFif opened this issue Dec 20, 2024 · 2 comments
Open

custom tool input error #157

farahFif opened this issue Dec 20, 2024 · 2 comments

Comments

@farahFif
Copy link

farahFif commented Dec 20, 2024

Hello,

I am creating a custom tool. The problem is that sometimes it can run the tool and sometimes it raises a problem with the input. I don't understand why such behaviour. Thanks for helping.

I encountered an error while trying to use the tool. This was the error: Arguments validation failed: 1 validation error for MyCustomToolSchema
argument
Input should be a valid string [type=string_type, input_value={'description': 'My perio...r only?', 'type': 'str'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.10/v/string_type.
Tool data retreiver accepts these inputs: Tool Name: data retreiver
Tool Arguments: {'argument': {'description': None, 'type': 'str'}}

this is my tool :


class MyCustomTool(BaseTool):
    name: str = "data retreiver"
    description: str = (
        "This tool searches for the most relevant answers given the question."
    )

    def _run(self, argument: str) -> str:
        # Implementation goes here
        url = "some private url"
        headers = {
        'x-api-key': "private"}

        response = requests.get(url, headers=headers).json()
        response = response['data']
        response = [r['answer'].replace("\n","") for r in response]
                
        return response

@reproduce-bot
Copy link

The following script is generated by AI Agent to help reproduce the issue:

# crewAI-tools/reproduce.py
from pydantic import BaseModel, ValidationError
from typing import Type
import requests

class BaseTool:
    name: str
    description: str
    args_schema: Type[BaseModel]

    def run(self, *args, **kwargs):
        raise NotImplementedError()

    def to_langchain(self):
        raise NotImplementedError()

class MyCustomToolSchema(BaseModel):
    argument: str

class MyCustomTool(BaseTool):
    name: str = "data retriever"
    description: str = (
        "This tool searches for the most relevant answers given the question."
    )
    args_schema: Type[BaseModel] = MyCustomToolSchema

    def _run(self, argument: str) -> str:
        # Implementation goes here
        url = "some private url"
        headers = {
            'x-api-key': "private"
        }

        response = requests.get(url, headers=headers).json()
        response = response['data']
        response = [r['answer'].replace("\n", "") for r in response]

        return response

def test_my_custom_tool():
    my_tool = MyCustomTool()

    # Test with valid input
    try:
        valid_input = {"argument": "valid string input"}
        my_tool.args_schema(**valid_input)
        print("Test passed successfully with no errors!")
    except ValidationError as e:
        raise AssertionError(e)

    # Test with invalid input
    try:
        invalid_input = {"argument": {"description": "Invalid input", "type": "str"}}
        my_tool.args_schema(**invalid_input)
    except ValidationError as e:
        raise AssertionError(e)

if __name__ == "__main__":
    test_my_custom_tool()

How to run:

python3 crewAI-tools/reproduce.py

Thank you for your valuable contribution to this project and we appreciate your feedback! Please respond with an emoji if you find this script helpful. Feel free to comment below if any improvements are needed.

Best regards from an AI Agent!
@farahFif

@danielfsbarreto
Copy link

Check the type output of the tool, @farahFif.

def _run(self, argument: str) -> str:

Here you are declaring the output of the tool is a string.

response = [r['answer'].replace("\n","") for r in response]

Doesn't it make the actual type of the output a List?

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

No branches or pull requests

3 participants