dspy.Tool
dspy.Tool(func: Callable, name: Optional[str] = None, desc: Optional[str] = None, args: Optional[dict[str, Any]] = None, arg_types: Optional[dict[str, Any]] = None, arg_desc: Optional[dict[str, str]] = None)
Bases: BaseType
Tool class.
This class is used to simplify the creation of tools for tool calling (function calling) in LLMs. Only supports functions for now.
Initialize the Tool class.
Users can choose to specify the name
, desc
, args
, and arg_types
, or let the dspy.Tool
automatically infer the values from the function. For values that are specified by the user, automatic inference
will not be performed on them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Callable
|
The actual function that is being wrapped by the tool. |
required |
name
|
Optional[str]
|
The name of the tool. Defaults to None. |
None
|
desc
|
Optional[str]
|
The description of the tool. Defaults to None. |
None
|
args
|
Optional[dict[str, Any]]
|
The args and their schema of the tool, represented as a dictionary from arg name to arg's json schema. Defaults to None. |
None
|
arg_types
|
Optional[dict[str, Any]]
|
The argument types of the tool, represented as a dictionary from arg name to the type of the argument. Defaults to None. |
None
|
arg_desc
|
Optional[dict[str, str]]
|
Descriptions for each arg, represented as a dictionary from arg name to description string. Defaults to None. |
None
|
Example:
def foo(x: int, y: str = "hello"):
return str(x) + y
tool = Tool(foo)
print(tool.args)
# Expected output: {'x': {'type': 'integer'}, 'y': {'type': 'string', 'default': 'hello'}}
Source code in dspy/adapters/types/tool.py
Functions
__call__(**kwargs)
Source code in dspy/adapters/types/tool.py
acall(**kwargs)
async
Source code in dspy/adapters/types/tool.py
description() -> str
classmethod
extract_custom_type_from_annotation(annotation)
classmethod
Extract all custom types from the annotation.
This is used to extract all custom types from the annotation of a field, while the annotation can
have arbitrary level of nesting. For example, we detect Tool
is in list[dict[str, Tool]]
.
Source code in dspy/adapters/types/base_type.py
format_as_litellm_function_call()
Source code in dspy/adapters/types/tool.py
from_langchain(tool: BaseTool) -> Tool
classmethod
Build a DSPy tool from a LangChain tool.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tool
|
BaseTool
|
The LangChain tool to convert. |
required |
Returns:
Type | Description |
---|---|
Tool
|
A Tool object. |
Example:
import asyncio
import dspy
from langchain.tools import tool as lc_tool
@lc_tool
def add(x: int, y: int):
"Add two numbers together."
return x + y
dspy_tool = dspy.Tool.from_langchain(add)
async def run_tool():
return await dspy_tool.acall(x=1, y=2)
print(asyncio.run(run_tool()))
# 3
Source code in dspy/adapters/types/tool.py
from_mcp_tool(session: mcp.client.session.ClientSession, tool: mcp.types.Tool) -> Tool
classmethod
Build a DSPy tool from an MCP tool and a ClientSession.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session
|
ClientSession
|
The MCP session to use. |
required |
tool
|
Tool
|
The MCP tool to convert. |
required |
Returns:
Type | Description |
---|---|
Tool
|
A Tool object. |