diff --git a/docs/how-to/create-custom-tools.mdx b/docs/how-to/create-custom-tools.mdx index 31fd09e3f..2caab716b 100644 --- a/docs/how-to/create-custom-tools.mdx +++ b/docs/how-to/create-custom-tools.mdx @@ -20,14 +20,21 @@ pip install 'crewai[tools]' ### Subclassing `BaseTool` -To create a personalized tool, inherit from `BaseTool` and define the necessary attributes and the `_run` method. +To create a personalized tool, inherit from `BaseTool` and define the necessary attributes, including the `args_schema` for input validation, and the `_run` method. ```python Code +from typing import Type from crewai_tools import BaseTool +from pydantic import BaseModel, Field + +class MyToolInput(BaseModel): + """Input schema for MyCustomTool.""" + argument: str = Field(..., description="Description of the argument.") class MyCustomTool(BaseTool): name: str = "Name of my tool" description: str = "What this tool does. It's vital for effective utilization." + args_schema: Type[BaseModel] = MyToolInput def _run(self, argument: str) -> str: # Your tool's logic here diff --git a/src/crewai/cli/templates/crew/tools/custom_tool.py b/src/crewai/cli/templates/crew/tools/custom_tool.py index b12529303..d38318051 100644 --- a/src/crewai/cli/templates/crew/tools/custom_tool.py +++ b/src/crewai/cli/templates/crew/tools/custom_tool.py @@ -1,11 +1,17 @@ +from typing import Type from crewai_tools import BaseTool +from pydantic import BaseModel, Field +class MyCustomToolInput(BaseModel): + """Input schema for MyCustomTool.""" + argument: str = Field(..., description="Description of the argument.") class MyCustomTool(BaseTool): name: str = "Name of my tool" description: str = ( "Clear description for what this tool is useful for, you agent will need this information to use it." ) + args_schema: Type[BaseModel] = MyCustomToolInput def _run(self, argument: str) -> str: # Implementation goes here diff --git a/src/crewai/cli/templates/flow/tools/custom_tool.py b/src/crewai/cli/templates/flow/tools/custom_tool.py index b12529303..030e575ec 100644 --- a/src/crewai/cli/templates/flow/tools/custom_tool.py +++ b/src/crewai/cli/templates/flow/tools/custom_tool.py @@ -1,4 +1,13 @@ +from typing import Type + from crewai_tools import BaseTool +from pydantic import BaseModel, Field + + +class MyCustomToolInput(BaseModel): + """Input schema for MyCustomTool.""" + + argument: str = Field(..., description="Description of the argument.") class MyCustomTool(BaseTool): @@ -6,6 +15,7 @@ class MyCustomTool(BaseTool): description: str = ( "Clear description for what this tool is useful for, you agent will need this information to use it." ) + args_schema: Type[BaseModel] = MyCustomToolInput def _run(self, argument: str) -> str: # Implementation goes here diff --git a/src/crewai/cli/templates/pipeline/tools/custom_tool.py b/src/crewai/cli/templates/pipeline/tools/custom_tool.py index b12529303..d38318051 100644 --- a/src/crewai/cli/templates/pipeline/tools/custom_tool.py +++ b/src/crewai/cli/templates/pipeline/tools/custom_tool.py @@ -1,11 +1,17 @@ +from typing import Type from crewai_tools import BaseTool +from pydantic import BaseModel, Field +class MyCustomToolInput(BaseModel): + """Input schema for MyCustomTool.""" + argument: str = Field(..., description="Description of the argument.") class MyCustomTool(BaseTool): name: str = "Name of my tool" description: str = ( "Clear description for what this tool is useful for, you agent will need this information to use it." ) + args_schema: Type[BaseModel] = MyCustomToolInput def _run(self, argument: str) -> str: # Implementation goes here diff --git a/src/crewai/cli/templates/pipeline_router/tools/custom_tool.py b/src/crewai/cli/templates/pipeline_router/tools/custom_tool.py index b12529303..d38318051 100644 --- a/src/crewai/cli/templates/pipeline_router/tools/custom_tool.py +++ b/src/crewai/cli/templates/pipeline_router/tools/custom_tool.py @@ -1,11 +1,17 @@ +from typing import Type from crewai_tools import BaseTool +from pydantic import BaseModel, Field +class MyCustomToolInput(BaseModel): + """Input schema for MyCustomTool.""" + argument: str = Field(..., description="Description of the argument.") class MyCustomTool(BaseTool): name: str = "Name of my tool" description: str = ( "Clear description for what this tool is useful for, you agent will need this information to use it." ) + args_schema: Type[BaseModel] = MyCustomToolInput def _run(self, argument: str) -> str: # Implementation goes here