Skip to content

Commit 7938277

Browse files
committed
Fix handling of messages from Anthropic models. AIMessageChunk content generated is a list of dict and message contains tool_calls
1 parent 18501d9 commit 7938277

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

backend/app/core/graph/messages.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,21 @@ def event_to_response(event: StreamEvent) -> ChatResponse | None:
4444
id = event["run_id"]
4545
if kind == "on_chat_model_stream":
4646
name = event["metadata"]["langgraph_node"]
47-
message_chunk = event["data"]["chunk"]
48-
content: str = message_chunk.content
47+
message_chunk: AIMessageChunk = event["data"]["chunk"]
4948
type = get_message_type(message_chunk)
49+
content: str = ""
50+
if isinstance(message_chunk.content, list):
51+
for c in message_chunk.content:
52+
if isinstance(c, str):
53+
content += c
54+
elif isinstance(c, dict):
55+
content += c.get("text", "")
56+
else:
57+
content = message_chunk.content
58+
tool_calls = message_chunk.tool_calls
5059
if content and type:
5160
return ChatResponse(
52-
type=type,
53-
id=id,
54-
name=name,
55-
content=content,
61+
type=type, id=id, name=name, content=content, tool_calls=tool_calls
5662
)
5763
elif kind == "on_chat_model_end":
5864
message: AIMessage = event["data"]["output"]

frontend/src/components/Teams/ChatTeam.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const MessageBox = ({ message, onResume }: MessageBoxProps) => {
9797
<Container pt={2}>
9898
{content && <Markdown>{content}</Markdown>}
9999
{tool_calls?.map((tool_call, index) => (
100-
<Box key={index}>
100+
<Box key={index} mt={4}>
101101
<Tag colorScheme="purple" mb={2}>
102102
{tool_call.name}
103103
</Tag>
@@ -269,7 +269,7 @@ const ChatTeam = () => {
269269
// only content is streamable in chunks
270270
content: currentMessage.content
271271
? currentMessage.content + response.content
272-
: null,
272+
: "",
273273
tool_output: response.tool_output,
274274
}
275275
} else {

0 commit comments

Comments
 (0)