DSPy: The framework for programming—not prompting—language models
Python
Support default args in dspy.Tool (#8082)
Chen Qian01a386329e80
diff --git a/dspy/predict/react.py b/dspy/predict/react.pyindex 395a8f5ea..c01a6875a 100644--- a/dspy/predict/react.py+++ b/dspy/predict/react.py@@ -47,7 +47,7 @@ class ReAct(Module):args = getattr(tool, "args")desc = (f", whose description is <desc>{tool.desc}</desc>." if tool.desc else ".").replace("\n", " ")desc += f" It takes arguments {args} in JSON format."- instr.append(f"({idx+1}) {tool.name}{desc}")+ instr.append(f"({idx + 1}) {tool.name}{desc}")react_signature = (dspy.Signature({**signature.input_fields}, "\n".join(instr))diff --git a/dspy/primitives/tool.py b/dspy/primitives/tool.pyindex 56663b431..3e9a71822 100644--- a/dspy/primitives/tool.py+++ b/dspy/primitives/tool.py@@ -25,16 +25,31 @@ class Tool:):"""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.+Args:func (Callable): The actual function that is being wrapped by the tool.name (Optional[str], optional): The name of the tool. Defaults to None.desc (Optional[str], optional): The description of the tool. Defaults to None.- args (Optional[dict[str, Any]], optional): The args of the tool, represented as a dictionary- from arg name to arg's json schema. Defaults to None.+ args (Optional[dict[str, Any]], optional): The args and their schema of the tool, represented as a+ dictionary from arg name to arg's json schema. Defaults to None.arg_types (Optional[dict[str, Any]], optional): The argument types of the tool, represented as a dictionaryfrom arg name to the type of the argument. Defaults to None.arg_desc (Optional[dict[str, str]], optional): Descriptions for each arg, represented as adictionary from arg name to description string. Defaults to None.++ Example:++ ```python+ 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'}}+ ```"""self.func = funcself.name = name@@ -90,6 +105,7 @@ class Tool:available_hints = get_type_hints(annotations_func)# Build a dictionary of arg name -> type (defaulting to Any when missing)hints = {param_name: available_hints.get(param_name, Any) for param_name in sig.parameters.keys()}+ default_values = {param_name: sig.parameters[param_name].default for param_name in sig.parameters.keys()}# Process each argument's type to generate its JSON schema.for k, v in hints.items():@@ -104,6 +120,8 @@ class Tool:args[k] = v_json_schemaelse:args[k] = TypeAdapter(v).json_schema() or "Any"+ if default_values[k] is not inspect.Parameter.empty:+ args[k]["default"] = default_values[k]if arg_desc and k in arg_desc:args[k]["description"] = arg_desc[k]diff --git a/tests/predict/test_react.py b/tests/predict/test_react.pyindex c0c11b737..a3f0ecdca 100644--- a/tests/predict/test_react.py+++ b/tests/predict/test_react.py@@ -127,32 +127,6 @@ import litellm# assert react.react[0].signature.instructions.startswith("You are going to generate output based on input.")-def test_tool_from_function():- def foo(a: int, b: int) -> int:- """Add two numbers."""- return a + b-- tool = react.Tool(foo)- assert tool.name == "foo"- assert tool.desc == "Add two numbers."- assert tool.args == {"a": {"type": "integer"}, "b": {"type": "integer"}}---def test_tool_from_class():- class Foo:- def __init__(self, user_id: str):- self.user_id = user_id-- def foo(self, a: int, b: int) -> int:- """Add two numbers."""- return a + b-- tool = react.Tool(Foo("123").foo)- assert tool.name == "foo"- assert tool.desc == "Add two numbers."- assert tool.args == {"a": {"type": "integer"}, "b": {"type": "integer"}}--def test_tool_calling_with_pydantic_args():class CalendarEvent(BaseModel):name: strdiff --git a/tests/primitives/test_tool.py b/tests/primitives/test_tool.pyindex dc30f2d72..6b49f24f3 100644--- a/tests/primitives/test_tool.py+++ b/tests/primitives/test_tool.py@@ -5,7 +5,7 @@ from typing import Any, Optional# Test fixtures-def dummy_function(x: int, y: str) -> str:+def dummy_function(x: int, y: str = "hello") -> str:"""A dummy function for testing.Args:@@ -16,7 +16,7 @@ def dummy_function(x: int, y: str) -> str:class DummyModel(BaseModel):- field1: str+ field1: str = "hello"field2: int@@ -84,6 +84,7 @@ def test_tool_from_function():assert "y" in tool.argsassert tool.args["x"]["type"] == "integer"assert tool.args["y"]["type"] == "string"+ assert tool.args["y"]["default"] == "hello"def test_tool_from_class():@@ -109,6 +110,22 @@ def test_tool_from_function_with_pydantic():assert tool.args["model"]["type"] == "object"assert "field1" in tool.args["model"]["properties"]assert "field2" in tool.args["model"]["properties"]+ assert tool.args["model"]["properties"]["field1"]["default"] == "hello"+++def test_tool_from_function_with_pydantic_nesting():+ tool = Tool(complex_dummy_function)++ assert tool.name == "complex_dummy_function"+ assert "profile" in tool.args+ assert "priority" in tool.args+ assert "notes" in tool.args+ assert tool.args["profile"]["type"] == "object"+ assert tool.args["profile"]["properties"]["user_id"]["type"] == "integer"+ assert tool.args["profile"]["properties"]["name"]["type"] == "string"+ assert tool.args["profile"]["properties"]["age"]["anyOf"] == [{"type": "integer"}, {"type": "null"}]+ assert tool.args["profile"]["properties"]["contact"]["type"] == "object"+ assert tool.args["profile"]["properties"]["contact"]["properties"]["email"]["type"] == "string"def test_tool_callable():