Skip to content

on_chat_model_stream event chunk content in astream_events() is list not str #486

Open
@Maxew42

Description

@Maxew42

When using astream_events() with ChatBedrockConverse, the on_chat_model_stream events do not have the format I was expecting. The field data.chunk.content is a list rather then a string.
This is not aligned with the behaviour presented here for chat models. This is also not aligned with the behaviour of the ChatBedrock model, which gives a string. Its also not aligned with the behaviour of ChatBedrockConverse described in the table below here.

Is this intentional ?

Code:

from langchain_aws import ChatBedrockConverse
import os

async def chat_langchain_astream_events():
    llm = ChatBedrockConverse(
        model="anthropic.claude-3-5-sonnet-20240620-v1:0",
        region_name="eu-central-1",
        aws_access_key_id=os.environ["AWS_ACCESS_KEY_ID"],
        aws_secret_access_key=os.environ["AWS_SECRET_ACCESS_KEY"],
        )

    message = [("user", "Hey, how are you?")]
    events = []
    async for event in llm.astream_events(message, version="v2"):
        events.append(event)
    print(events[1:3])

Output:

[{'event': 'on_chat_model_stream', 'run_id': '6915e633-7473-42d0-bbab-b894920929c8', 'name': 'ChatBedrockConverse', 'tags': [], 'metadata': {'ls_provider': 'amazon_bedrock', 'ls_model_name': 'anthropic.claude-3-5-sonnet-20240620-v1:0', 'ls_model_type': 'chat', 'ls_temperature': None}, 'data': {'chunk': AIMessageChunk(content=[], additional_kwargs={}, response_metadata={}, id='run--6915e633-7473-42d0-bbab-b894920929c8')}, 'parent_ids': []}, 
 {'event': 'on_chat_model_stream', 'run_id': '6915e633-7473-42d0-bbab-b894920929c8', 'name': 'ChatBedrockConverse', 'tags': [], 'metadata': {'ls_provider': 'amazon_bedrock', 'ls_model_name': 'anthropic.claude-3-5-sonnet-20240620-v1:0', 'ls_model_type': 'chat', 'ls_temperature': None}, 'data': {'chunk': AIMessageChunk(content=[{'type': 'text', 'text': 'Hello', 'index': 0}], additional_kwargs={}, response_metadata={}, id='run--6915e633-7473-42d0-bbab-b894920929c8')}, 'parent_ids': []}]

in particular ...'data': {'chunk': AIMessageChunk(content=[{'type': 'text', 'text': 'Hello', 'index': 0}], ...
Where I would expect ...'data': {'chunk': AIMessageChunk(content='Hello', ... as

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions