Skip to content

Conversation

kalanyuz
Copy link
Contributor

With response API you get additional usage token information in the form of litellm's InputTokenDetails and OutputTokenDetails.

Because these are objects the current _merge_usage_entries cannot handle them and thus cause the entire request to fail.
This PR fixes that.

This pull request improves the aggregation logic for usage tracking and adds comprehensive tests to ensure correct merging of token usage details, including handling of custom objects and None values.

Improvements to usage aggregation logic

  • Enhanced the _merge_usage_entries method in dspy/utils/usage_tracker.py to correctly merge usage entries when values are custom objects (with a __dict__), ensuring that details from types like InputTokensDetails and OutputTokensDetails are properly aggregated.
  • Updated the method signature for _merge_usage_entries for better readability and consistency.

Expanded test coverage

  • Added new tests in tests/utils/test_usage_tracker.py to verify that input and output token details are accurately aggregated across multiple usage entries, including cases with missing or None values.
  • Included imports for InputTokensDetails and OutputTokensDetails from litellm.types.llms.openai to support new test scenarios.

@kalanyuz
Copy link
Contributor Author

kalanyuz commented Sep 3, 2025

@okhat @TomeHirata please have a look at this too 🙇🏼

@@ -40,6 +42,8 @@ def _merge_usage_entries(self, usage_entry1: dict[str, Any] | None, usage_entry2
current_v = result.get(k)
if isinstance(v, dict) or isinstance(current_v, dict):
result[k] = self._merge_usage_entries(current_v, v)
elif hasattr(v, "__dict__") or hasattr(current_v, "__dict__"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to expose input_tokens_details or output_tokens_details in dspy? It might be confusing if we use both chat completion and responses together. Can we just filter out these new fields for now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants