Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

fix(lm): handle None usage from Responses API on truncated responses (#9718)

Isaac Millerfaa99d1f784a
diff --git a/dspy/clients/base_lm.py b/dspy/clients/base_lm.py
index f35fd1671..760a2679b 100644
--- a/dspy/clients/base_lm.py
+++ b/dspy/clients/base_lm.py
@@ -106,7 +106,7 @@ class BaseLM:
"kwargs": kwargs,
"response": response,
"outputs": outputs,
- "usage": dict(getattr(response, "usage", {})),
+ "usage": dict(getattr(response, "usage", {}) or {}),
"cost": getattr(response, "_hidden_params", {}).get("response_cost"),
"timestamp": datetime.datetime.now().isoformat(),
"uuid": str(uuid.uuid4()),
diff --git a/dspy/clients/lm.py b/dspy/clients/lm.py
index 365b1b47c..b6e1c992a 100644
--- a/dspy/clients/lm.py
+++ b/dspy/clients/lm.py
@@ -194,7 +194,7 @@ class LM(BaseLM):
self._check_truncation(results)
if not getattr(results, "cache_hit", False) and dspy.settings.usage_tracker:
- settings.usage_tracker.add_usage(self.model, dict(getattr(results, "usage", {})))
+ settings.usage_tracker.add_usage(self.model, dict(getattr(results, "usage", {}) or {}))
return results
async def aforward(
@@ -235,7 +235,7 @@ class LM(BaseLM):
self._check_truncation(results)
if not getattr(results, "cache_hit", False) and dspy.settings.usage_tracker:
- settings.usage_tracker.add_usage(self.model, dict(getattr(results, "usage", {})))
+ settings.usage_tracker.add_usage(self.model, dict(getattr(results, "usage", {}) or {}))
return results
def launch(self, launch_kwargs: dict[str, Any] | None = None):
diff --git a/tests/clients/test_lm.py b/tests/clients/test_lm.py
index dcb22ea45..13a7e96ef 100644
--- a/tests/clients/test_lm.py
+++ b/tests/clients/test_lm.py
@@ -1029,6 +1029,119 @@ def test_responses_api_with_pydantic_model_input():
}
+def test_responses_api_with_none_usage():
+ """Responses API returns usage=None for incomplete/truncated responses (e.g. max_output_tokens hit)."""
+ api_response = ResponsesAPIResponse(
+ id="resp_1",
+ created_at=0.0,
+ error=None,
+ incomplete_details={"reason": "max_output_tokens"},
+ instructions=None,
+ model="openai/gpt-5-mini",
+ object="response",
+ output=[
+ ResponseOutputMessage(
+ **{
+ "id": "msg_1",
+ "type": "message",
+ "role": "assistant",
+ "status": "incomplete",
+ "content": [
+ {"type": "output_text", "text": "Partial response that was truncated", "annotations": []}
+ ],
+ },
+ ),
+ ],
+ metadata={},
+ parallel_tool_calls=False,
+ temperature=1.0,
+ tool_choice="auto",
+ tools=[],
+ top_p=1.0,
+ max_output_tokens=100,
+ previous_response_id=None,
+ reasoning=None,
+ status="incomplete",
+ text=None,
+ truncation="disabled",
+ usage=None,
+ user=None,
+ )
+
+ with mock.patch("litellm.responses", autospec=True, return_value=api_response):
+ lm = dspy.LM(
+ model="openai/gpt-5-mini",
+ model_type="responses",
+ cache=False,
+ temperature=1.0,
+ max_tokens=16000,
+ )
+
+ with track_usage() as tracker:
+ result = lm("test query")
+
+ assert result == [{"text": "Partial response that was truncated"}]
+ assert lm.history[-1]["usage"] == {}
+ assert tracker.get_total_tokens() == {}
+
+
+async def test_responses_api_with_none_usage_async():
+ """Async path: Responses API returns usage=None for incomplete/truncated responses."""
+ api_response = ResponsesAPIResponse(
+ id="resp_1",
+ created_at=0.0,
+ error=None,
+ incomplete_details={"reason": "max_output_tokens"},
+ instructions=None,
+ model="openai/gpt-5-mini",
+ object="response",
+ output=[
+ ResponseOutputMessage(
+ **{
+ "id": "msg_1",
+ "type": "message",
+ "role": "assistant",
+ "status": "incomplete",
+ "content": [
+ {"type": "output_text", "text": "Partial async response", "annotations": []}
+ ],
+ },
+ ),
+ ],
+ metadata={},
+ parallel_tool_calls=False,
+ temperature=1.0,
+ tool_choice="auto",
+ tools=[],
+ top_p=1.0,
+ max_output_tokens=100,
+ previous_response_id=None,
+ reasoning=None,
+ status="incomplete",
+ text=None,
+ truncation="disabled",
+ usage=None,
+ user=None,
+ )
+
+ with mock.patch("litellm.aresponses", autospec=True, return_value=api_response):
+ lm = dspy.LM(
+ model="openai/gpt-5-mini",
+ model_type="responses",
+ cache=False,
+ temperature=1.0,
+ max_tokens=16000,
+ )
+
+ with track_usage() as tracker:
+ result = await lm.acall("test query")
+
+ assert result == [{"text": "Partial async response"}]
+ assert lm.history[-1]["usage"] == {}
+ assert tracker.get_total_tokens() == {}
+
+
@pytest.mark.asyncio
async def test_streaming_passes_headers_correctly():
from dspy.clients.lm import _get_stream_completion_fn