Skip to content

Commit 63abf74

Browse files
fix default arg when type hint not available
1 parent 96f1e95 commit 63abf74

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

dspy/primitives/tool.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def _parse_function(self, func: Callable, arg_desc: dict[str, str] = None):
119119
v_json_schema = self._resolve_pydantic_schema(v)
120120
args[k] = v_json_schema
121121
else:
122-
args[k] = TypeAdapter(v).json_schema() or "Any"
122+
args[k] = TypeAdapter(v).json_schema()
123123
if default_values[k] is not inspect.Parameter.empty:
124124
args[k]["default"] = default_values[k]
125125
if arg_desc and k in arg_desc:
@@ -137,7 +137,8 @@ def __call__(self, **kwargs):
137137
raise ValueError(f"Arg {k} is not in the tool's args.")
138138
try:
139139
instance = v.model_dump() if hasattr(v, "model_dump") else v
140-
if not self.args[k] == "Any":
140+
type_str = self.args[k].get("type")
141+
if type_str is not None and type_str != "Any":
141142
validate(instance=instance, schema=self.args[k])
142143
except ValidationError as e:
143144
raise ValueError(f"Arg {k} is invalid: {e.message}")

tests/primitives/test_tool.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,12 @@ def test_invalid_function_call():
150150
def test_parameter_desc():
151151
tool = Tool(dummy_function, arg_desc={"x": "The x parameter"})
152152
assert tool.args["x"]["description"] == "The x parameter"
153+
154+
155+
def test_tool_with_default_args_without_type_hints():
156+
def foo(x=100):
157+
return x
158+
159+
tool = Tool(foo)
160+
assert tool.args["x"]["default"] == 100
161+
assert not hasattr(tool.args["x"], "type")

0 commit comments

Comments
 (0)