fix: add support for capturing usage details with ChatBedRock #1267
+13
−4
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.
This PR is a fix for Issue 7657 where the usage details for ChatbedRock were not captured properly while it was working fine for ChatbedRockConverse.
I have mentioned the cause for the bug in the issue comment but I am pasting the gist of it here.
Cause of the Bug
The error for this seems to be at langchain/CallbackHandler.py due to mainly two reasons
The problem specifically is with the function _parse_usage_model.
When it comes to ChatBedrockConverse, the input passed to this function looks like the following
while with ChatBedrock, the input passed to this function looks like the following
Fix for the Bug
Here is a screenshot of the issue before fixing this
Here is a screenshot of the trace after fixing this issue
I tried running the existing tests but some of them are failing because they require me to provide an OpenAI API key but I don't think they have a bearing on this fix. But do let me know if the team requires me to do some additional testing beyond this.
Important
Fixes usage details capture for ChatBedRock by adding key mappings and differentiating keys from OpenAI in
CallbackHandler.py
._parse_usage_model()
inCallbackHandler.py
.prompt_tokens
toinput
andcompletion_tokens
tooutput
.completion_token_details
andprompt_token_details
to OpenAI keys.This description was created by
for 09e0acf. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
This PR addresses a bug in the LangChain callback handler where token usage metrics were not being properly captured for Amazon's ChatBedrock model. The issue arose because ChatBedrock uses similar token usage keys to OpenAI (
prompt_tokens
,completion_tokens
), which were being skipped as they were assumed to be OpenAI-specific formats handled server-side.The fix adds proper key mapping for ChatBedrock's format and improves the detection of genuine OpenAI responses by checking for additional OpenAI-specific keys (
completion_token_details
,prompt_token_details
). This ensures ChatBedrock's usage metrics are properly captured while maintaining special handling for OpenAI responses.Confidence score: 4/5
langfuse/langchain/CallbackHandler.py
: The changes look good but should be tested with both ChatBedrock and OpenAI to ensure no regressions