Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

hassiebp
Copy link
Contributor

@hassiebp hassiebp commented Aug 15, 2025

Important

Updates _parse_usage_model in CallbackHandler.py to filter usage model values, retaining only integers, potentially causing data loss for float values.

  • Behavior:
    • Updates _parse_usage_model in CallbackHandler.py to filter usage model values, retaining only integers.
    • Previous logic excluded strings but allowed other types; now only integers are allowed.
  • Potential Issues:
    • May cause data loss by excluding legitimate float values representing costs or fractional metrics.
    • Could break functionality for models returning float-based usage data.

This description was created by Ellipsis for 3bb9c43. You can customize this summary. It will automatically update as commits are pushed.


Disclaimer: Experimental PR review

Greptile Summary

This PR modifies the usage parsing logic in the LangChain callback handler (CallbackHandler.py) to be more restrictive when filtering usage model values. The change on line 968 updates the filtering condition from not isinstance(v, str) to isinstance(v, int), meaning only integer values are now kept in the parsed usage model.

Based on the PR title referencing "qwen", this change appears to address an issue with Qwen models that return non-numeric string values in their usage metadata. The previous logic excluded only strings but allowed other types (including floats) to pass through. The new logic takes a more restrictive approach by only allowing integers.

This change integrates with Langfuse's usage tracking system, which uses Pydantic models like Usage and OpenAiUsage to structure token and cost data. These models typically accept optional integer fields for counts like input, output, total, and optional float fields for costs like input_cost, output_cost, total_cost. The filtering happens before this data reaches the Pydantic models.

Confidence score: 2/5

  • This PR introduces potential data loss by filtering out legitimate float values that could represent costs or fractional usage metrics
  • Score lowered due to overly restrictive filtering logic that may break existing functionality for models that return float-based usage data
  • Pay close attention to langfuse/langchain/CallbackHandler.py line 968 and consider testing with various model providers that return float usage values

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, 1 comment

Edit Code Review Bot Settings | Greptile

hassiebp and others added 3 commits August 15, 2025 14:10
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
@hassiebp hassiebp merged commit 79eb901 into main Aug 15, 2025
7 of 10 checks passed
@hassiebp hassiebp deleted the fix-usage-parsing-qwen branch August 15, 2025 16:10
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.

1 participant