Replies: 1 comment
-
🤖 Hello, To make your custom tool work with pydantic validation in the LangChain framework, you can use the from typing import Optional, Type
from langchain.callbacks.manager import CallbackManagerForToolRun
from langchain.tools import BaseTool
from pydantic.v1 import BaseModel, Field
class CustomTool(BaseTool):
name = "custom_tool"
description = "This is a custom tool"
def _run(
self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None
) -> str:
"""Use the tool."""
# Implement your tool's functionality here
return "Result of custom tool"
async def _arun(
self, query: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None
) -> str:
"""Use the tool asynchronously."""
raise NotImplementedError("custom_tool does not support async") In this example, the If you want to use pydantic validation, you can define a custom class CustomToolInput(BaseModel):
query: str = Field()
class CustomTool(BaseTool):
name = "custom_tool"
description = "This is a custom tool"
args_schema: Type[BaseModel] = CustomToolInput
def _run(
self, query: str, run_manager: Optional[CallbackManagerForToolRun] = None
) -> str:
"""Use the tool."""
# Implement your tool's functionality here
return "Result of custom tool"
async def _arun(
self, query: str, run_manager: Optional[AsyncCallbackManagerForToolRun] = None
) -> str:
"""Use the tool asynchronously."""
raise NotImplementedError("custom_tool does not support async") In this example, the In the context shared, it's important to note that LangChain is currently compatible with both Pydantic v1 and v2. However, you should avoid mixing v1 and v2 code in the same tool. If you're using Pydantic v1, make sure to import from If you want to create a tool from a function, you can use the I hope this helps! Let me know if you have any other questions. Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
while following https://python.langchain.com/docs/modules/agents/how_to/custom_llm_agent#set-up-tool, we also want to custom tool adding extra schema validation, from the example for the tool listed, we need name, description, and func field, for custom tool we only have name and description field, here's the custom tool code https://gist.github.com/elvis-cai/203487d2bfd11d57625d84f8122e875e,
this is the custom tool we are referring to https://python.langchain.com/docs/modules/agents/tools/custom_tools#subclassing-the-basetool-class.
not sure how to make it work with pydantic validation, thanks.
Beta Was this translation helpful? Give feedback.
All reactions