Skip to content

Conversation

SYED-M-HUSSAIN
Copy link
Contributor

Fix ChatBedrockConverse streaming content format

Summary

Fixes #486 - ChatBedrockConverse.astream_events() was returning AIMessageChunk objects with content as a list instead of a string, which is inconsistent with other LangChain chat models and the expected interface.

Problem

When using astream_events() with ChatBedrockConverse, streaming chunks returned content in this format:

content=[{'type': 'text', 'text': 'Hello', 'index': 0}]

Instead of the expected string format used by other chat models:

content='Hello'

This inconsistency broke compatibility with downstream code expecting string content and violated the LangChain chat model interface.

Solution

Modified _parse_stream_event() to use the same content formatting logic as the non-streaming _parse_response() function:

  • Applied _str_if_single_text_block() to content blocks in contentBlockStart and contentBlockDelta events
  • Changed all empty content from [] to "" across all event types
  • Preserved existing tool call functionality unchanged

Changes

Modified Files

  • libs/partners/aws/langchain_aws/chat_models/bedrock_converse.py

Key Changes

  1. contentBlockStart/contentBlockDelta events: Use _str_if_single_text_block([block]) instead of [block]
  2. All other events: Change content=[] to content=""
  3. Consistency: Streaming now uses identical content formatting as non-streaming responses

Testing

Before Fix

# Returns list content
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content=[{'type': 'text', 'text': 'Hello', 'index': 0}])}}

After Fix

# Returns string content
{'event': 'on_chat_model_stream', 'data': {'chunk': AIMessageChunk(content='Hello')}}

@SYED-M-HUSSAIN
Copy link
Contributor Author

@michaelnchin please review this PR, thanks

@blanketspy99
Copy link

I guess this would break who is already consuming it as a list and we couldn't identify if the response chunk is part of text or tool_use or reasoning_content.

Probably it's better to check if content is list type then pick the text block else yield the str directly.

…e original consistency issue. It's a more robust solution that considers both current and future use cases.
@SYED-M-HUSSAIN
Copy link
Contributor Author

@blanketspy99 feedback incorporated

@SYED-M-HUSSAIN
Copy link
Contributor Author

@michaelnchin please review this PR, thanks

@3coins
Copy link
Collaborator

3coins commented May 29, 2025

I will take a look at this by tomorrow.

@3coins 3coins self-requested a review May 29, 2025 18:34
@3coins
Copy link
Collaborator

3coins commented Jun 2, 2025

@yonekura-dc
Copy link

Any updates on this change?
Migrating from other LLM providers to bedrock requires refactoring agent logic due to different formats.

@michaelnchin
Copy link
Collaborator

Closing as this has been superseded by #589.

michaelnchin pushed a commit that referenced this pull request Aug 21, 2025
…ist (#589)

Fixes #486

Supersedes PR #491 -
full credit goes to SYED-M-HUSSAIN for the original fix

---------

Co-authored-by: Hussainbeam <[email protected]>
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.

on_chat_model_stream event chunk content in astream_events() is list not str
5 participants