Skip to content

Commit 2f39380

Browse files
committed
fix: extract all text parts when AIMessage content is a list (CopilotKit#1748)
When Anthropic models return multi-part content lists, only the first element was used and the rest discarded. Now iterates all parts and concatenates text blocks, preserving the full message content.
1 parent 9686a71 commit 2f39380

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

sdk-python/copilotkit/langgraph.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,19 @@ def langchain_messages_to_copilotkit(
142142
if hasattr(message, "content"):
143143
content = message.content
144144

145-
# Check if content is a list and use the first element
145+
# Content can be a list of content blocks (e.g. Anthropic models).
146+
# Extract and concatenate all text parts instead of only taking
147+
# the first element.
146148
if isinstance(content, list):
147-
content = content[0] if content else ""
149+
text_parts = []
150+
for part in content:
151+
if isinstance(part, str):
152+
text_parts.append(part)
153+
elif isinstance(part, dict) and part.get("type") == "text":
154+
text_parts.append(part.get("text", ""))
155+
elif isinstance(part, dict) and "text" in part:
156+
text_parts.append(part.get("text", ""))
157+
content = "".join(text_parts)
148158

149159
# Anthropic models return a dict with a "text" key
150160
if isinstance(content, dict):

0 commit comments

Comments
 (0)