Skip to content

An AI-powered search engine with a generative UI

fix: fix Anthropic model message structure for tool-call/tool-result separation

Yoshiki Miura9130b9e21108
diff --git a/lib/agents/generate-related-questions.ts b/lib/agents/generate-related-questions.ts
index a743c766..23360b9f 100644
--- a/lib/agents/generate-related-questions.ts
+++ b/lib/agents/generate-related-questions.ts
@@ -1,6 +1,7 @@
import { convertToModelMessages, generateObject, type UIMessage } from 'ai'
import { z } from 'zod'
+import { convertMessagesForAnthropic } from '../utils/anthropic-message-conversion'
import { getModel } from '../utils/registry'
const relatedQuestionsSchema = z.object({
@@ -19,7 +20,13 @@ export async function generateRelatedQuestions(
abortSignal?: AbortSignal
) {
// Convert UIMessages to ModelMessages
- const modelMessages = convertToModelMessages(messages)
+ let modelMessages = convertToModelMessages(messages)
+
+ // Apply Anthropic-specific conversion if needed
+ if (model.startsWith('anthropic:')) {
+ modelMessages = convertMessagesForAnthropic(modelMessages)
+ }
+
const systemPrompt = `You are a professional web researcher tasked with generating follow-up questions. Based on the conversation history and search results, create 3 DIFFERENT related questions that:
1. Explore NEW aspects not covered in the original query
diff --git a/lib/db/actions.ts b/lib/db/actions.ts
index e5d39f34..c1d48058 100644
--- a/lib/db/actions.ts
+++ b/lib/db/actions.ts
@@ -178,6 +178,7 @@ export async function loadChatWithMessages(
const uiMessages = messagesResult.map(msg =>
buildUIMessageFromDB(msg, msg.parts)
)
+
const result = { ...chat, messages: uiMessages }
chatCache.set(cacheKey, result)
return result
diff --git a/lib/streaming/create-chat-stream-response.ts b/lib/streaming/create-chat-stream-response.ts
index 7ded70e2..ea1f2312 100644
--- a/lib/streaming/create-chat-stream-response.ts
+++ b/lib/streaming/create-chat-stream-response.ts
@@ -11,6 +11,7 @@ import { researcher } from '@/lib/agents/researcher'
import { getChat as getChatAction } from '../actions/chat'
import { generateChatTitle } from '../agents/title-generator'
+import { convertMessagesForAnthropic } from '../utils/anthropic-message-conversion'
import {
getMaxAllowedTokens,
shouldTruncateMessages,
@@ -88,6 +89,11 @@ export async function createChatStreamResponse(
// Convert to model messages and apply context window management
let modelMessages = convertToModelMessages(messagesToModel)
+ // Apply Anthropic-specific conversion if needed
+ if (model.providerId === 'anthropic') {
+ modelMessages = convertMessagesForAnthropic(modelMessages)
+ }
+
if (shouldTruncateMessages(modelMessages, model)) {
const maxTokens = getMaxAllowedTokens(model)
const originalCount = modelMessages.length
diff --git a/lib/utils/__tests__/anthropic-message-conversion.test.ts b/lib/utils/__tests__/anthropic-message-conversion.test.ts
new file mode 100644
index 00000000..502a2077
--- /dev/null
+++ b/lib/utils/__tests__/anthropic-message-conversion.test.ts
@@ -0,0 +1,301 @@
+import { describe, expect, it } from 'vitest'
+import type { ModelMessage } from 'ai'
+
+import { convertMessagesForAnthropic } from '../anthropic-message-conversion'
+
+describe('convertMessagesForAnthropic', () => {
+ it('should handle messages without tool-calls', () => {
+ const messages: ModelMessage[] = [
+ {
+ role: 'user',
+ content: [{ type: 'text', text: 'Hello' }]
+ },
+ {
+ role: 'assistant',
+ content: [{ type: 'text', text: 'Hi there!' }]
+ }
+ ]
+
+ const result = convertMessagesForAnthropic(messages)
+ expect(result).toEqual(messages)
+ })
+
+ it('should split assistant message with text after tool-call', () => {
+ const messages: ModelMessage[] = [
+ {
+ role: 'user',
+ content: [{ type: 'text', text: 'Search for something' }]
+ },
+ {
+ role: 'assistant',
+ content: [
+ { type: 'text', text: 'Let me search for that.' },
+ {
+ type: 'tool-call',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ args: { query: 'test' }
+ },
+ { type: 'text', text: 'Based on the results...' }
+ ]
+ },
+ {
+ role: 'tool',
+ content: [
+ {
+ type: 'tool-result',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ result: { data: 'results' }
+ }
+ ]
+ }
+ ]
+
+ const result = convertMessagesForAnthropic(messages)
+
+ expect(result).toHaveLength(4)
+ expect(result[0]).toEqual(messages[0]) // User message unchanged
+ expect(result[1]).toEqual({
+ role: 'assistant',
+ content: [
+ { type: 'text', text: 'Let me search for that.' },
+ {
+ type: 'tool-call',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ args: { query: 'test' }
+ }
+ ]
+ })
+ expect(result[2]).toEqual(messages[2]) // Tool result
+ expect(result[3]).toEqual({
+ role: 'assistant',
+ content: [{ type: 'text', text: 'Based on the results...' }]
+ })
+ })
+
+ it('should handle tool-call at the end of message (no text after)', () => {
+ const messages: ModelMessage[] = [
+ {
+ role: 'assistant',
+ content: [
+ { type: 'text', text: 'Let me search.' },
+ {
+ type: 'tool-call',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ args: { query: 'test' }
+ }
+ ]
+ },
+ {
+ role: 'tool',
+ content: [
+ {
+ type: 'tool-result',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ result: { data: 'results' }
+ }
+ ]
+ }
+ ]
+
+ const result = convertMessagesForAnthropic(messages)
+ expect(result).toEqual(messages) // No changes needed
+ })
+
+ it('should handle multiple tool-calls in a single message', () => {
+ const messages: ModelMessage[] = [
+ {
+ role: 'assistant',
+ content: [
+ { type: 'text', text: 'Let me search and fetch.' },
+ {
+ type: 'tool-call',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ args: { query: 'test' }
+ },
+ {
+ type: 'tool-call',
+ toolCallId: 'call_456',
+ toolName: 'fetch',
+ args: { url: 'http://example.com' }
+ },
+ { type: 'text', text: 'Here are the results...' }
+ ]
+ },
+ {
+ role: 'tool',
+ content: [
+ {
+ type: 'tool-result',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ result: { data: 'search results' }
+ }
+ ]
+ }
+ ]
+
+ const result = convertMessagesForAnthropic(messages)
+
+ // Should split at the first tool-call
+ expect(result).toHaveLength(3)
+ expect(result[0].content).toHaveLength(3) // text + 2 tool-calls
+ expect(result[2]).toEqual({
+ role: 'assistant',
+ content: [{ type: 'text', text: 'Here are the results...' }]
+ })
+ })
+
+ it('should handle messages with only tool-calls (no text)', () => {
+ const messages: ModelMessage[] = [
+ {
+ role: 'assistant',
+ content: [
+ {
+ type: 'tool-call',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ args: { query: 'test' }
+ }
+ ]
+ },
+ {
+ role: 'tool',
+ content: [
+ {
+ type: 'tool-result',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ result: { data: 'results' }
+ }
+ ]
+ }
+ ]
+
+ const result = convertMessagesForAnthropic(messages)
+ expect(result).toEqual(messages) // No changes needed
+ })
+
+ it('should handle non-assistant messages', () => {
+ const messages: ModelMessage[] = [
+ {
+ role: 'user',
+ content: [{ type: 'text', text: 'Hello' }]
+ },
+ {
+ role: 'system',
+ content: [{ type: 'text', text: 'You are a helpful assistant' }]
+ },
+ {
+ role: 'tool',
+ content: [
+ {
+ type: 'tool-result',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ result: { data: 'results' }
+ }
+ ]
+ }
+ ]
+
+ const result = convertMessagesForAnthropic(messages)
+ expect(result).toEqual(messages) // No changes to non-assistant messages
+ })
+
+ it('should handle empty content arrays', () => {
+ const messages: ModelMessage[] = [
+ {
+ role: 'assistant',
+ content: []
+ }
+ ]
+
+ const result = convertMessagesForAnthropic(messages)
+ expect(result).toEqual(messages)
+ })
+
+ it('should handle missing tool-result message gracefully', () => {
+ const messages: ModelMessage[] = [
+ {
+ role: 'assistant',
+ content: [
+ { type: 'text', text: 'Let me search.' },
+ {
+ type: 'tool-call',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ args: { query: 'test' }
+ },
+ { type: 'text', text: 'Based on the results...' }
+ ]
+ },
+ {
+ role: 'user', // Not a tool message
+ content: [{ type: 'text', text: 'What did you find?' }]
+ }
+ ]
+
+ const result = convertMessagesForAnthropic(messages)
+
+ // Should still split but won't include the tool message after splitting
+ expect(result).toHaveLength(3)
+ expect(result[0].content).toHaveLength(2) // text + tool-call
+ expect(result[1]).toEqual({
+ role: 'assistant',
+ content: [{ type: 'text', text: 'Based on the results...' }]
+ })
+ expect(result[2]).toEqual(messages[1]) // User message comes after
+ })
+
+ it('should handle whitespace-only text after tool-call', () => {
+ const messages: ModelMessage[] = [
+ {
+ role: 'assistant',
+ content: [
+ { type: 'text', text: 'Searching...' },
+ {
+ type: 'tool-call',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ args: { query: 'test' }
+ },
+ { type: 'text', text: ' \n\t ' } // Only whitespace
+ ]
+ },
+ {
+ role: 'tool',
+ content: [
+ {
+ type: 'tool-result',
+ toolCallId: 'call_123',
+ toolName: 'search',
+ result: { data: 'results' }
+ }
+ ]
+ }
+ ]
+
+ const result = convertMessagesForAnthropic(messages)
+
+ // Should not split because text after tool-call is whitespace only
+ expect(result).toEqual(messages)
+ })
+
+ it('should handle string content (not array)', () => {
+ const messages: ModelMessage[] = [
+ {
+ role: 'assistant',
+ content: 'This is a simple string message'
+ }
+ ]
+
+ const result = convertMessagesForAnthropic(messages)
+ expect(result).toEqual(messages)
+ })
+})
\ No newline at end of file
diff --git a/lib/utils/anthropic-message-conversion.ts b/lib/utils/anthropic-message-conversion.ts
new file mode 100644
index 00000000..277db4b1
--- /dev/null
+++ b/lib/utils/anthropic-message-conversion.ts
@@ -0,0 +1,72 @@
+import type { ModelMessage } from 'ai'
+
+/**
+ * Fix message structure after convertToModelMessages for proper tool-call/tool-result separation.
+ *
+ * convertToModelMessages creates a single assistant message containing all parts (text, tool-call, text),
+ * but the expected structure requires tool-call to be immediately followed by tool-result in separate messages.
+ * This function splits assistant messages that contain text after tool-call parts.
+ */
+export function convertMessagesForAnthropic(
+ messages: ModelMessage[]
+): ModelMessage[] {
+ const result: ModelMessage[] = []
+
+ for (let i = 0; i < messages.length; i++) {
+ const message = messages[i]
+
+ if (message.role === 'assistant' && Array.isArray(message.content)) {
+ // Check if this assistant message contains both tool-call and text after it
+ let toolCallIndex = -1
+ let hasContentAfterToolCall = false
+
+ for (let j = 0; j < message.content.length; j++) {
+ const part = message.content[j]
+ if (part.type === 'tool-call') {
+ toolCallIndex = j
+ } else if (
+ toolCallIndex !== -1 &&
+ part.type === 'text' &&
+ part.text?.trim()
+ ) {
+ hasContentAfterToolCall = true
+ break
+ }
+ }
+
+ if (hasContentAfterToolCall && toolCallIndex !== -1) {
+ // Split the message: content before and including tool-call stays
+ const beforeToolCall = message.content.slice(0, toolCallIndex + 1)
+ const afterToolCall = message.content.slice(toolCallIndex + 1)
+
+ // Add the first part (up to and including tool-call)
+ result.push({
+ ...message,
+ content: beforeToolCall
+ })
+
+ // Add the tool-result message (should be the next message)
+ if (i + 1 < messages.length && messages[i + 1].role === 'tool') {
+ result.push(messages[i + 1])
+ i++ // Skip the tool message since we've already added it
+ }
+
+ // Add the remaining content as a new assistant message
+ if (afterToolCall.length > 0) {
+ result.push({
+ role: 'assistant',
+ content: afterToolCall
+ })
+ }
+ } else {
+ // No splitting needed
+ result.push(message)
+ }
+ } else {
+ // Not an assistant message, add as-is
+ result.push(message)
+ }
+ }
+
+ return result
+}
diff --git a/lib/utils/message-mapping.ts b/lib/utils/message-mapping.ts
index d636964e..86d05364 100644
--- a/lib/utils/message-mapping.ts
+++ b/lib/utils/message-mapping.ts
@@ -368,13 +368,43 @@ export function mapDBPartToUIMessagePart(
// Special handling for tool parts that maintain their type
if (['search', 'fetch', 'question'].includes(toolName)) {
- return {
- type: part.type as any,
- toolCallId: part.tool_toolCallId || '',
- state: part.tool_state || 'input-available',
- input: part[inputColumn],
- output: part[outputColumn],
- errorText: part.tool_errorText
+ if (!part.tool_state) {
+ throw new Error(`${toolName}_state is undefined`)
+ }
+
+ switch (part.tool_state) {
+ case 'input-streaming':
+ return {
+ type: part.type as any,
+ state: 'input-streaming',
+ toolCallId: part.tool_toolCallId || '',
+ input: part[inputColumn]
+ }
+ case 'input-available':
+ return {
+ type: part.type as any,
+ state: 'input-available',
+ toolCallId: part.tool_toolCallId || '',
+ input: part[inputColumn]
+ }
+ case 'output-available':
+ return {
+ type: part.type as any,
+ state: 'output-available',
+ toolCallId: part.tool_toolCallId || '',
+ input: part[inputColumn],
+ output: part[outputColumn]
+ }
+ case 'output-error':
+ return {
+ type: part.type as any,
+ state: 'output-error',
+ toolCallId: part.tool_toolCallId || '',
+ input: part[inputColumn],
+ errorText: part.tool_errorText
+ }
+ default:
+ throw new Error(`Unknown tool state: ${part.tool_state}`)
}
}