Dave Aitel
08/25/2024, 5:11 PMJeremiah
args_schema
as args_schema: type[BaseModel] = CommandRunnerInput
instead of leaving it unannotated I think it will work. This is how LangChain's docs show BaseTool use as well: https://python.langchain.com/v0.2/docs/how_to/custom_tools/#subclass-basetool.
LangChain appears to strip the unannotated args_schema. I'm not sure why it's working for you in LC, though they seem to dive into the raw Pydantic annotations at one point during subclass initialization so maybe they grab it there. I think that's why you setting it forcefully in your init worked as well.
This check runs without error for me, asserting the args schema is not present unless annotated in pure langchain and pydantic v1:
from pydantic.v1 import BaseModel
from langchain_core.tools import BaseTool
class LCBaseToolInput(BaseModel):
x: int
class LCBaseTool(BaseTool):
name = "TestTool"
description = "A test tool"
args_schema = LCBaseToolInput
def _run(self, x: int) -> str:
return str(x)
class LCBaseToolWithAnnotation(LCBaseTool):
# Note the annotation here
args_schema: type[BaseModel] = LCBaseToolInput
assert not LCBaseTool().args_schema
assert LCBaseToolWithAnnotation().args_schema
Jeremiah
Jeremiah
Dave Aitel
09/01/2024, 5:57 PMJeremiah
Jeremiah
Dave Aitel
09/01/2024, 5:58 PMDave Aitel
09/01/2024, 6:00 PMDave Aitel
09/01/2024, 6:34 PM