Skip to content

fix(langchain): prefer metadata ls_provider in SummarizationMiddleware token check#36604

Open
Michael Chin (michaelnchin) wants to merge 1 commit intolangchain-ai:masterfrom
michaelnchin:fix/summarization-middleware-ls-provider
Open

fix(langchain): prefer metadata ls_provider in SummarizationMiddleware token check#36604
Michael Chin (michaelnchin) wants to merge 1 commit intolangchain-ai:masterfrom
michaelnchin:fix/summarization-middleware-ls-provider

Conversation

@michaelnchin
Copy link
Copy Markdown
Contributor

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-aws adds response_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_tokens method to prefer response_metadata["ls_provider"] over model_provider when 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_provider values (bedrock/bedrock_converse), but share the same tracing ls_provider (amazon_bedrock) for token pricing. For messages from other providers that don't set ls_provider in metadata, model_provider will still be used as the fallback.

@github-actions github-actions bot added fix For PRs that implement a fix langchain `langchain` package issues & PRs size: S 50-199 LOC labels Apr 8, 2026
@org-membership-reviewer org-membership-reviewer bot added trusted-contributor >= 5 merged PRs in the `langchain-ai/langchain` repo external labels Apr 8, 2026
Comment on lines +1241 to +1242
"model_provider": "bedrock_converse",
"ls_provider": "amazon_bedrock",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

are both these keys in message metadata? I only see model_provider. Do we actually need to update ls_params or message.response_metadata["model_provider"] to make them consistent?

Image

Copy link
Copy Markdown
Contributor Author

@michaelnchin Michael Chin (michaelnchin) Apr 8, 2026

Choose a reason for hiding this comment

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

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.

@ccurme ccurme (ccurme) self-assigned this Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external fix For PRs that implement a fix langchain `langchain` package issues & PRs size: S 50-199 LOC trusted-contributor >= 5 merged PRs in the `langchain-ai/langchain` repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ChatBedrock: response_metadata["model_provider"] disagrees with _get_ls_params()["ls_provider"]

2 participants