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

Subclassing BaseTool: args_schema always None #3297

Closed
danielchalef opened this issue Apr 21, 2023 · 2 comments
Closed

Subclassing BaseTool: args_schema always None #3297

danielchalef opened this issue Apr 21, 2023 · 2 comments

Comments

@danielchalef
Copy link
Contributor

Following the example here, when subclassing BaseTool, args_schema is always None.

langchain 0.0.145

class SendMessageInput(BaseModel):
    email: str = Field(description="email")
    message: str = Field(description="the message to send")


class SendMessageTool(BaseTool):
    name = "send_message_tool"
    description = "useful for when you need to send a message to a human"
    args_schema = SendMessageInput

    def _run(self, query: str) -> str:
        """Use the tool."""
        return f"Sent {query}"

    async def _arun(self, query: str) -> str:
        """Use the tool asynchronously."""
        return f"Sent {query}"
In [4]: smt = SendMessageTool()
In [5]: smt.args_schema == None
Out[5]: True
@vowelparrot
Copy link
Contributor

vowelparrot commented Apr 21, 2023

The immediate fix is that it works if you add the type hint in the subclass.

class SendMessageTool(BaseTool):
    name = "send_message_tool"
    description = "useful for when you need to send a message to a human"
    args_schema: Type[BaseModel] = SendMessageInput # NOTE THE ": Type[BaseModel]"

    def _run(self, query: str) -> str:
        """Use the tool."""
        return f"Sent {query}"

    async def _arun(self, query: str) -> str:
        """Use the tool asynchronously."""
        return f"Sent {query}"

We will work to fix this behavior. It's 'by design' in pydantic and dataclasses but I even if we improve the docs here, it'll be a frequent "gotcha" if it isn't fixed.

Thank you for contributing to the project!

@danielchalef
Copy link
Contributor Author

Thanks. That's a nasty gotcha. Just cut a pr to update the notebook with a type hint: #3323

vowelparrot pushed a commit that referenced this issue Apr 21, 2023
per #3297

Co-authored-by: Daniel Chalef <daniel.chalef@private.org>
vowelparrot added a commit that referenced this issue Apr 24, 2023
- Proactively raise error if a tool subclasses BaseTool, defines its
own schema, but fails to add the type-hints
- fix the auto-inferred schema of the decorator to strip the
unneeded virtual kwargs from the schema dict

Helps avoid silent instances of #3297
wertycn pushed a commit to wertycn/langchain-zh that referenced this issue Apr 26, 2023
per langchain-ai/langchain#3297

Co-authored-by: Daniel Chalef <daniel.chalef@private.org>
vowelparrot pushed a commit that referenced this issue Apr 26, 2023
per #3297

Co-authored-by: Daniel Chalef <daniel.chalef@private.org>
vowelparrot added a commit that referenced this issue Apr 26, 2023
- Proactively raise error if a tool subclasses BaseTool, defines its
own schema, but fails to add the type-hints
- fix the auto-inferred schema of the decorator to strip the
unneeded virtual kwargs from the schema dict

Helps avoid silent instances of #3297
vowelparrot pushed a commit that referenced this issue Apr 28, 2023
per #3297

Co-authored-by: Daniel Chalef <daniel.chalef@private.org>
vowelparrot added a commit that referenced this issue Apr 28, 2023
- Proactively raise error if a tool subclasses BaseTool, defines its
own schema, but fails to add the type-hints
- fix the auto-inferred schema of the decorator to strip the
unneeded virtual kwargs from the schema dict

Helps avoid silent instances of #3297
samching pushed a commit to samching/langchain that referenced this issue May 1, 2023
per langchain-ai#3297

Co-authored-by: Daniel Chalef <daniel.chalef@private.org>
samching pushed a commit to samching/langchain that referenced this issue May 1, 2023
- Proactively raise error if a tool subclasses BaseTool, defines its
own schema, but fails to add the type-hints
- fix the auto-inferred schema of the decorator to strip the
unneeded virtual kwargs from the schema dict

Helps avoid silent instances of langchain-ai#3297
yanghua pushed a commit to yanghua/langchain that referenced this issue May 9, 2023
per langchain-ai#3297

Co-authored-by: Daniel Chalef <daniel.chalef@private.org>
yanghua pushed a commit to yanghua/langchain that referenced this issue May 9, 2023
- Proactively raise error if a tool subclasses BaseTool, defines its
own schema, but fails to add the type-hints
- fix the auto-inferred schema of the decorator to strip the
unneeded virtual kwargs from the schema dict

Helps avoid silent instances of langchain-ai#3297
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

2 participants