DSPy: The framework for programming—not prompting—language models
Python
Resolve "$def" for nested tool arg type (#8534)
Chen Qian0d0af3c4696c
diff --git a/dspy/adapters/types/tool.py b/dspy/adapters/types/tool.pyindex b3e6b4510..843eceed1 100644--- a/dspy/adapters/types/tool.py+++ b/dspy/adapters/types/tool.py@@ -103,7 +103,7 @@ class Tool(Type):v_json_schema = _resolve_json_schema_reference(v.model_json_schema())args[k] = v_json_schemaelse:- args[k] = TypeAdapter(v).json_schema()+ args[k] = _resolve_json_schema_reference(TypeAdapter(v).json_schema())if default_values[k] is not inspect.Parameter.empty:args[k]["default"] = default_values[k]if arg_desc and k in arg_desc:diff --git a/tests/adapters/test_tool.py b/tests/adapters/test_tool.pyindex 20f59604e..185714539 100644--- a/tests/adapters/test_tool.py+++ b/tests/adapters/test_tool.py@@ -50,7 +50,12 @@ class UserProfile(BaseModel):tags: list[str] = []-def complex_dummy_function(profile: UserProfile, priority: int, notes: str | None = None) -> dict[str, Any]:+class Note(BaseModel):+ content: str+ author: str+++def complex_dummy_function(profile: UserProfile, priority: int, notes: list[Note] | None = None) -> dict[str, Any]:"""Process user profile with complex nested structure.Args:@@ -89,7 +94,9 @@ async def async_dummy_with_pydantic(model: DummyModel) -> str:async def async_complex_dummy_function(- profile: UserProfile, priority: int, notes: str | None = None+ profile: UserProfile,+ priority: int,+ notes: list[Note] | None = None,) -> dict[str, Any]:"""Process user profile with complex nested structure asynchronously.@@ -167,6 +174,7 @@ def test_tool_from_function_with_pydantic_nesting():tool = Tool(complex_dummy_function)assert tool.name == "complex_dummy_function"+assert "profile" in tool.argsassert "priority" in tool.argsassert "notes" in tool.args@@ -177,6 +185,13 @@ def test_tool_from_function_with_pydantic_nesting():assert tool.args["profile"]["properties"]["contact"]["type"] == "object"assert tool.args["profile"]["properties"]["contact"]["properties"]["email"]["type"] == "string"+ # Reference should be resolved for nested pydantic models+ assert "$defs" not in str(tool.args["notes"])+ assert tool.args["notes"]["anyOf"][0]["type"] == "array"+ assert tool.args["notes"]["anyOf"][0]["items"]["type"] == "object"+ assert tool.args["notes"]["anyOf"][0]["items"]["properties"]["content"]["type"] == "string"+ assert tool.args["notes"]["anyOf"][0]["items"]["properties"]["author"]["type"] == "string"+def test_tool_callable():tool = Tool(dummy_function)@@ -319,11 +334,11 @@ async def test_async_tool_with_complex_pydantic():),)- result = await tool.acall(profile=profile, priority=1, notes="Test note")+ result = await tool.acall(profile=profile, priority=1, notes=[Note(content="Test note", author="Test author")])assert result["user_id"] == 1assert result["name"] == "Test User"assert result["priority"] == 1- assert result["notes"] == "Test note"+ assert result["notes"] == [Note(content="Test note", author="Test author")]assert result["primary_address"]["street"] == "123 Main St"@@ -382,42 +397,39 @@ TOOL_CALL_TEST_CASES = [([], [{"type": "tool_calls", "tool_calls": []}]),([{"name": "search", "args": {"query": "hello"}}],- [{- "type": "tool_calls",- "tool_calls": [{- "type": "function",- "function": {"name": "search", "arguments": {"query": "hello"}}- }]- }],+ [+ {+ "type": "tool_calls",+ "tool_calls": [{"type": "function", "function": {"name": "search", "arguments": {"query": "hello"}}}],+ }+ ],),([{"name": "search", "args": {"query": "hello"}},- {"name": "translate", "args": {"text": "world", "lang": "fr"}}+ {"name": "translate", "args": {"text": "world", "lang": "fr"}},+ ],+ [+ {+ "type": "tool_calls",+ "tool_calls": [+ {"type": "function", "function": {"name": "search", "arguments": {"query": "hello"}}},+ {+ "type": "function",+ "function": {"name": "translate", "arguments": {"text": "world", "lang": "fr"}},+ },+ ],+ }],- [{- "type": "tool_calls",- "tool_calls": [- {- "type": "function",- "function": {"name": "search", "arguments": {"query": "hello"}}- },- {- "type": "function",- "function": {"name": "translate", "arguments": {"text": "world", "lang": "fr"}}- }- ]- }],),([{"name": "get_time", "args": {}}],- [{- "type": "tool_calls",- "tool_calls": [{- "type": "function",- "function": {"name": "get_time", "arguments": {}}- }]- }],+ [+ {+ "type": "tool_calls",+ "tool_calls": [{"type": "function", "function": {"name": "get_time", "arguments": {}}}],+ }+ ],),]@@ -431,11 +443,12 @@ def test_tool_calls_format_basic(tool_calls_data, expected):assert result == expected+def test_tool_calls_format_from_dict_list():"""Test format works with ToolCalls created from from_dict_list."""tool_calls_dicts = [{"name": "search", "args": {"query": "hello"}},- {"name": "translate", "args": {"text": "world", "lang": "fr"}}+ {"name": "translate", "args": {"text": "world", "lang": "fr"}},]tool_calls = ToolCalls.from_dict_list(tool_calls_dicts)