DSPy: The framework for programming—not prompting—language models
Python
fix(base_lm): guard response.usage access to handle missing usage field (#9638)
Isaac Miller92e7f43e792f
diff --git a/dspy/clients/base_lm.py b/dspy/clients/base_lm.pyindex 3770c4c2b..f35fd1671 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(response.usage),+ "usage": dict(getattr(response, "usage", {})),"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.pyindex 3921cc889..6c98fcd55 100644--- a/dspy/clients/lm.py+++ b/dspy/clients/lm.py@@ -193,8 +193,8 @@ class LM(BaseLM):self._check_truncation(results)- if not getattr(results, "cache_hit", False) and dspy.settings.usage_tracker and hasattr(results, "usage"):- settings.usage_tracker.add_usage(self.model, dict(results.usage))+ if not getattr(results, "cache_hit", False) and dspy.settings.usage_tracker:+ settings.usage_tracker.add_usage(self.model, dict(getattr(results, "usage", {})))return resultsasync def aforward(@@ -234,8 +234,8 @@ class LM(BaseLM):self._check_truncation(results)- if not getattr(results, "cache_hit", False) and dspy.settings.usage_tracker and hasattr(results, "usage"):- settings.usage_tracker.add_usage(self.model, dict(results.usage))+ if not getattr(results, "cache_hit", False) and dspy.settings.usage_tracker:+ settings.usage_tracker.add_usage(self.model, dict(getattr(results, "usage", {})))return resultsdef launch(self, launch_kwargs: dict[str, Any] | None = None):