fix(langchain): take only int values in parsed usage #1299
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Important
Updates
_parse_usage_model
inCallbackHandler.py
to filter usage model values, retaining only integers, potentially causing data loss for float values._parse_usage_model
inCallbackHandler.py
to filter usage model values, retaining only integers.This description was created by
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 fromnot isinstance(v, str)
toisinstance(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
andOpenAiUsage
to structure token and cost data. These models typically accept optional integer fields for counts likeinput
,output
,total
, and optional float fields for costs likeinput_cost
,output_cost
,total_cost
. The filtering happens before this data reaches the Pydantic models.Confidence score: 2/5
langfuse/langchain/CallbackHandler.py
line 968 and consider testing with various model providers that return float usage values