mirror of
https://github.com/hwchase17/langchain.git
synced 2025-12-15 20:04:15 +00:00
Changes:
- ToolCall, InvalidToolCall and ToolCallChunk can all accept a "type"
parameter now
- LLM integration packages add "type" to all the above
- Tool supports ToolCall inputs that have "type" specified
- Tool outputs ToolMessage when a ToolCall is passed as input
- Tools can separately specify ToolMessage.content and
ToolMessage.raw_output
- Tools emit events for validation errors (using on_tool_error and
on_tool_end)
Example:
```python
@tool("structured_api", response_format="content_and_raw_output")
def _mock_structured_tool_with_raw_output(
arg1: int, arg2: bool, arg3: Optional[dict] = None
) -> Tuple[str, dict]:
"""A Structured Tool"""
return f"{arg1} {arg2}", {"arg1": arg1, "arg2": arg2, "arg3": arg3}
def test_tool_call_input_tool_message_with_raw_output() -> None:
tool_call: Dict = {
"name": "structured_api",
"args": {"arg1": 1, "arg2": True, "arg3": {"img": "base64string..."}},
"id": "123",
"type": "tool_call",
}
expected = ToolMessage("1 True", raw_output=tool_call["args"], tool_call_id="123")
tool = _mock_structured_tool_with_raw_output
actual = tool.invoke(tool_call)
assert actual == expected
tool_call.pop("type")
with pytest.raises(ValidationError):
tool.invoke(tool_call)
actual_content = tool.invoke(tool_call["args"])
assert actual_content == expected.content
```
---------
Co-authored-by: Erick Friis <erick@langchain.dev>
56 lines
2.0 KiB
Python
56 lines
2.0 KiB
Python
"""Test base tool child implementations."""
|
|
|
|
import inspect
|
|
import re
|
|
from typing import List, Type
|
|
|
|
import pytest
|
|
from langchain_core.tools import BaseTool
|
|
|
|
from langchain_community.tools.amadeus.base import AmadeusBaseTool
|
|
from langchain_community.tools.gmail.base import GmailBaseTool
|
|
from langchain_community.tools.office365.base import O365BaseTool
|
|
from langchain_community.tools.playwright.base import BaseBrowserTool
|
|
from langchain_community.tools.slack.base import SlackBaseTool
|
|
|
|
|
|
def get_non_abstract_subclasses(cls: Type[BaseTool]) -> List[Type[BaseTool]]:
|
|
to_skip = {
|
|
AmadeusBaseTool,
|
|
BaseBrowserTool,
|
|
GmailBaseTool,
|
|
O365BaseTool,
|
|
SlackBaseTool,
|
|
} # Abstract but not recognized
|
|
subclasses = []
|
|
for subclass in cls.__subclasses__():
|
|
if (
|
|
not getattr(subclass, "__abstract__", None)
|
|
and not subclass.__name__.startswith("_")
|
|
and subclass not in to_skip
|
|
):
|
|
subclasses.append(subclass)
|
|
sc = get_non_abstract_subclasses(subclass)
|
|
subclasses.extend(sc)
|
|
return subclasses
|
|
|
|
|
|
@pytest.mark.parametrize("cls", get_non_abstract_subclasses(BaseTool)) # type: ignore
|
|
def test_all_subclasses_accept_run_manager(cls: Type[BaseTool]) -> None:
|
|
"""Test that tools defined in this repo accept a run manager argument."""
|
|
# This wouldn't be necessary if the BaseTool had a strict API.
|
|
if cls._run is not BaseTool._run:
|
|
run_func = cls._run
|
|
params = inspect.signature(run_func).parameters
|
|
assert "run_manager" in params
|
|
pattern = re.compile(r"(?!Async)CallbackManagerForToolRun")
|
|
assert bool(re.search(pattern, str(params["run_manager"].annotation)))
|
|
assert params["run_manager"].default is None
|
|
|
|
if cls._arun is not BaseTool._arun:
|
|
run_func = cls._arun
|
|
params = inspect.signature(run_func).parameters
|
|
assert "run_manager" in params
|
|
assert "AsyncCallbackManagerForToolRun" in str(params["run_manager"].annotation)
|
|
assert params["run_manager"].default is None
|