Skip to content

fix(bedrock): return string content in ChatBedrockConverse streaming #491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

SYED-M-HUSSAIN
Copy link

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
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
Author

@blanketspy99 feedback incorporated

@SYED-M-HUSSAIN
Copy link
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

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
3 participants