Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(model-runtime): handle array content in anthropic assistant messages (#11206)

Arvin Xub03845d006ea
diff --git a/packages/model-runtime/src/core/contextBuilders/anthropic.test.ts b/packages/model-runtime/src/core/contextBuilders/anthropic.test.ts
index fa37c563b1a..728784e305e 100644
--- a/packages/model-runtime/src/core/contextBuilders/anthropic.test.ts
+++ b/packages/model-runtime/src/core/contextBuilders/anthropic.test.ts
@@ -216,6 +216,33 @@ describe('anthropicHelpers', () => {
role: 'assistant',
});
});
+
+ it('should correctly convert assistant message with array content but no tool_calls', async () => {
+ const message: OpenAIChatMessage = {
+ content: [
+ { thinking: 'Let me think about this...', type: 'thinking', signature: 'sig123' },
+ { type: 'text', text: 'Here is my response.' },
+ ],
+ role: 'assistant',
+ };
+ const result = await buildAnthropicMessage(message);
+ expect(result).toEqual({
+ content: [
+ { thinking: 'Let me think about this...', type: 'thinking', signature: 'sig123' },
+ { type: 'text', text: 'Here is my response.' },
+ ],
+ role: 'assistant',
+ });
+ });
+
+ it('should return undefined for assistant message with empty array content', async () => {
+ const message: OpenAIChatMessage = {
+ content: [],
+ role: 'assistant',
+ };
+ const result = await buildAnthropicMessage(message);
+ expect(result).toBeUndefined();
+ });
});
describe('buildAnthropicMessages', () => {
diff --git a/packages/model-runtime/src/core/contextBuilders/anthropic.ts b/packages/model-runtime/src/core/contextBuilders/anthropic.ts
index ac8a0a95380..e6cd4788894 100644
--- a/packages/model-runtime/src/core/contextBuilders/anthropic.ts
+++ b/packages/model-runtime/src/core/contextBuilders/anthropic.ts
@@ -118,8 +118,15 @@ export const buildAnthropicMessage = async (
}
// or it's a plain assistant message
+ // Handle array content (e.g., content with thinking blocks)
+ if (Array.isArray(content)) {
+ const messageContent = await buildArrayContent(content);
+ if (messageContent.length === 0) return undefined;
+ return { content: messageContent, role: 'assistant' };
+ }
+
// Anthropic API requires non-empty content, filter out empty/whitespace-only content
- const textContent = (content as string)?.trim();
+ const textContent = content?.trim();
if (!textContent) return undefined;
return { content: textContent, role: 'assistant' };
}