DSPy: The framework for programming—not prompting—language models
Python
fix(lm): omit absent Responses tool fields (#10014)
Isaac Miller24ec85de4af4
diff --git a/dspy/clients/base_lm.py b/dspy/clients/base_lm.pyindex f11715a1d..f7405ab9f 100644--- a/dspy/clients/base_lm.py+++ b/dspy/clients/base_lm.py@@ -873,7 +873,7 @@ class BaseLM:for content_item in output_item.content:text_outputs.append(content_item.text)elif output_item_type == "function_call":- tool_calls.append(output_item.model_dump())+ tool_calls.append(output_item.model_dump(exclude_none=True))elif output_item_type == "reasoning":if getattr(output_item, "content", None) and len(output_item.content) > 0:for content_item in output_item.content:diff --git a/tests/clients/test_lm.py b/tests/clients/test_lm.pyindex db70b379f..bcb1430f2 100644--- a/tests/clients/test_lm.py+++ b/tests/clients/test_lm.py@@ -1214,9 +1214,21 @@ def test_lm_replaces_system_with_developer_role():assert mock_completion.call_args.kwargs["request"]["messages"][0]["role"] == "developer"-def test_responses_api_tool_calls(litellm_test_server):+ ("provider_fields", "expected_provider_fields"),+ [+ ({}, {}),+ ({"caller": None, "namespace": None}, {}),+ (+ {"caller": {"type": "direct"}, "namespace": "collaboration"},+ {"caller": {"type": "direct"}, "namespace": "collaboration"},+ ),+ ],+ ids=["legacy", "empty-provider-fields", "populated-provider-fields"],+)+def test_responses_api_tool_calls(litellm_test_server, provider_fields, expected_provider_fields):api_base, _ = litellm_test_server- expected_tool_call = {+ base_tool_call = {"type": "function_call","name": "get_weather","arguments": json.dumps({"city": "Paris"}),@@ -1224,10 +1236,10 @@ def test_responses_api_tool_calls(litellm_test_server):"status": "completed","id": "call_1",}- expected_response = [{"tool_calls": [expected_tool_call]}]+ expected_response = [{"tool_calls": [{**base_tool_call, **expected_provider_fields}]}]api_response = make_response(- output_blocks=[expected_tool_call],+ output_blocks=[{**base_tool_call, **provider_fields}],)with mock.patch("litellm.responses", autospec=True, return_value=api_response) as dspy_responses: