fix(langchain): prefer metadata ls_provider in SummarizationMiddleware token check#36604
Open
Michael Chin (michaelnchin) wants to merge 1 commit intolangchain-ai:masterfrom
Conversation
Comment on lines
+1241
to
+1242
| "model_provider": "bedrock_converse", | ||
| "ls_provider": "amazon_bedrock", |
Collaborator
Contributor
Author
There was a problem hiding this comment.
It's not in a langchain-aws release yet, but ChatBedrock and ChatBedrockConverse will both start adding response_metadata["ls_provider"] = "amazon_bedrock" to outgoing messages with this PR: langchain-ai/langchain-aws#981
model_provider and ls_params can't be directly updated in the Bedrock chat models, as they have distinct values that other mechanisms are dependent on (context here: langchain-ai/langchain-aws#946 (comment)), so this extra metadata field is a workaround to positively identify the base ls_provider directly from messages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

Fixes langchain-ai/langchain-aws#946
Part 2 of the proposed fix noted in langchain-ai/langchain-aws#946 (comment). The first PR in
langchain-awsaddsresponse_metadata["ls_provider"] = "amazon_bedrock"to messages generated by ChatBedrock and ChatBedrockConverse.This follow-up PR will update
SummarizationMiddleware's_should_summarize_based_on_reported_tokensmethod to preferresponse_metadata["ls_provider"]overmodel_providerwhen checking if a message came from the current provider.This is specifically unblocks optimal use with Bedrock chat models, where Converse API and Invoke API requests require unique
model_providervalues (bedrock/bedrock_converse), but share the same tracingls_provider(amazon_bedrock) for token pricing. For messages from other providers that don't setls_providerin metadata,model_providerwill still be used as the fallback.