Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: ToolInvocation must have a result #822

Open
bfischer1121 opened this issue Feb 23, 2025 · 2 comments
Open

FIX: ToolInvocation must have a result #822

bfischer1121 opened this issue Feb 23, 2025 · 2 comments

Comments

@bfischer1121
Copy link

To avoid this error when the AI updates a document, change artifacts-model -> artifact-model

model: myProvider.languageModel('artifacts-model'),

@tha-hammer
Copy link

tha-hammer commented Feb 24, 2025

hey! This might help

At least I got mine working in the prior version.

Version 3.0 is still throwing the error but I think it's related to the Artifact trying to use the main update-document tool call instead of the /artifact/server.ts file...

Hope that thread helps. And sorry for not just putting it here but there was a flow to my frustration with arrogant programmers that might help.

@tha-hammer
Copy link

tha-hammer commented Feb 24, 2025

EDIT: The sanitize code I used here is needed in V3.0.

Yes, there is a sanitization function used onFinish but... It ain't workin bucko!

Here's what I used

  // Before conversion, let's sanitize the messages
  const sanitizedMessages = messages.filter(message => {
    if (!message.parts) return true;
  
    // Ensure all tool invocations within parts are complete
    return message.parts.every(part => {
      if (part.type !== 'tool-invocation') return true;
      return part.toolInvocation.state === 'result';
    });
  });
  const coreMessages = convertToCoreMessages(sanitizedMessages);
  log.debug('=== After Core Message Conversion ===', { 
    coreMessageCount: coreMessages.length,
    immediate: true 
  });

    log.debug('=== After Message Sanitization ===', { 
      originalCount: messages.length,
      sanitizedCount: sanitizedMessages.length,
      immediate: true 
    });

==============================
Before and after Logs

The text below is from my original post.
In 3.0 of the Chatbot, I'm seeing the same error. I did confirm that the error is throwing in the file below. (yes, really) (sigh... ok... how can you know since you're probably assuming I'm a total dumb4$$. Well, I commented out the log right before the createDataStreamResponse. sigh... again... Annnnd I renamed the log in the route.ts file for the chat route, happy?)

Also the experimental_repairToolCall is not getting hit because the log line is not printing... fwiw... (when the error is present)

Here are my logs and code:

Code: in /app/(chat)/api/chat/route.ts

return createDataStreamResponse({
  execute: async (dataStream) => {
    logger.info('Starting stream execution in Chat API Route', { 
      chatId: id, 
      model: selectedChatModel,
      state: currentState 
    });
    log.debug('Messages Before repairToolCall Chat API Route:', messages);

    try {
      const result = await streamText({
        model: myProvider.languageModel(selectedChatModel),
        system: getSystemPromptForState(currentState, chatTitle, parsedState.expansionProgress),
        messages: messages,
        maxSteps: 5,
        experimental_activeTools:
          selectedChatModel === 'chat-model-reasoning'
            ? []
            : [
                'getWeather',
                'createDocument',
                'updateDocument',
                'requestSuggestions',
                'manageState'
              ],
        experimental_transform: smoothStream({ chunking: 'word' }),
        experimental_generateMessageId: generateUUID,
        tools: {
          getWeather,
          createDocument: createDocument({ session, dataStream }),
          updateDocument: updateDocument({ session, dataStream }),
          requestSuggestions: requestSuggestions({
            session,
            dataStream,
          }),
          manageState: manageState({ session  })
        },
        experimental_repairToolCall: async ({
          toolCall,
          tools,
          error,
          messages,
          system
        }) => {
           log.debug('Messages in repairToolCall Chat API Route:', messages);
          const result = await generateText({
            model: myProvider.languageModel(selectedChatModel),
            system,
            messages: [
              ...messages,
              {
                role: 'assistant',
                content: [{
                  type: 'tool-call',
                  toolCallId: toolCall.toolCallId,
                  toolName: toolCall.toolName,
                  args: toolCall.args
                }]
              },
              {
                role: 'tool' as const,
                content: [{
                  type: 'tool-result',
                  toolCallId: toolCall.toolCallId,
                  toolName: toolCall.toolName,
                  result: error.message
                }]
              }
            ],
            tools
          });

          const newToolCall = result.toolCalls?.find(
            newCall => newCall.toolName === toolCall.toolName
          );

          return newToolCall != null ? {
            toolCallType: 'function' as const,
            toolCallId: toolCall.toolCallId,
            toolName: toolCall.toolName,
            args: JSON.stringify(newToolCall.args)
          } : null;
        },
        onFinish: async ({ response, reasoning }) => {
          if (session.user?.id) {
            try {
              const sanitizedResponseMessages = sanitizeResponseMessages({
                messages: response.messages,
                reasoning,
              });

              await saveMessages({
                messages: sanitizedResponseMessages.map((message) => ({
                  id: message.id,
                  chatId: id,
                  role: message.role,
                  content: message.content,
                  createdAt: new Date(),
                })),
              });
              logger.info('Chat messages saved successfully', { chatId: id });
            } catch (error) {
              logger.error('Failed to save chat messages', {
                chatId: id,
                error: error instanceof Error ? error.message : 'Unknown error'
              });
            }
          }
        },
        experimental_telemetry: {
          isEnabled: true,
          functionId: 'stream-text',
        },
      });


   
      log.debug('=== After StreamText Setup ===', { 
        hasResult: !!result,
        immediate: true 
      });
      
      result.consumeStream();

      result.mergeIntoDataStream(dataStream, {
        sendReasoning: true,
      });
    } catch (error) {
      logger.error('Error during stream execution', {
        chatId: id,
        error: error instanceof Error ? error.message : 'Unknown error'
      });
      abortController.abort();
    }
  },
  onError: (error) => {
    logger.error('Stream error occurred', {
      chatId: id,
      error: error instanceof Error ? error.message : 'Unknown error'
    });
    return 'Oops, an error occurred!';
  },
});

} catch (error) {
logger.error('Unexpected error in POST handler', {
error: error instanceof Error ? error.message : 'Unknown error'
});
return new Response('Internal Server Error', { status: 500 });
} finally {
abortController.abort();
}
}

Logs

"11": {
"id": "42886ca5-2288-4ec8-9d72-b47ec39a331a",
"createdAt": "2025-02-24T18:14:17.467Z",
"role": "assistant",
"content": "I'll create a new document of type "checklist".\n\nNow, I'll update it with a basic checklist ",
"parts": [
{
"type": "text",
"text": "I'll create a new document of type "checklist"."
},
{
"type": "tool-invocation",
"toolInvocation": {
"state": "result",
"step": 0,
"toolCallId": "toolu_01Y83LUPnFJSwvszQv8Uw3bv",
"toolName": "createDocument",
"args": {
"title": "Test Checklist",
"kind": "checklist"
},
"result": {
"id": "45b14881-76c6-484f-84c8-f1f951d23bd3",
"title": "Test Checklist",
"kind": "checklist",
"content": "A document was created and is now visible to the user."
}
}
},
{
"type": "text",
"text": "\n\nNow, I'll update it with a basic checklist "
},
{
"type": "tool-invocation",
"toolInvocation": {
"state": "call",
"step": 1,
"toolCallId": "toolu_01QtW2M7x28CsRF8SaAyea1C",
"toolName": "updateDocument",
"args": {
"id": "45b14881-76c6-484f-84c8-f1f951d23bd3",
"description": "Create a checklist with the following items:\n\n# Big Idea\n- [ ] Define the main concept\n- [ ] Identify unique angle\n\n# The USP\n- [ ] List unique selling points\n- [ ] Define competitive advantage\n\n# Your Appeal\n- [ ] Identify emotional triggers\n- [ ] Define core message\n\n# Your Hook\n- [ ] Create attention-grabbing opener\n- [ ] Develop compelling story\n\n# Your Promise\n- [ ] State main benefit\n- [ ] Define expected outcome\n\n# Wants, Needs, Features, Benefits & Costs\n- [ ] List customer wants\n- [ ] Identify customer needs\n- [ ] Detail product features\n- [ ] Explain benefits\n- [ ] Address costs/investment\n\n# Claims & Proof\n## Specific Claims\n- [ ] List main claims\n- [ ] Detail supporting evidence\n\n## Proof Of Those Claims\n- [ ] Gather testimonials\n- [ ] Compile case studies\n\n## Who Is This For? Bullseye Clients\n- [ ] Define ideal customer profile\n- [ ] List qualifying characteristics"
}
}
}
],
"toolInvocations": [
{
"state": "result",
"step": 0,
"toolCallId": "toolu_01Y83LUPnFJSwvszQv8Uw3bv",
"toolName": "createDocument",
"args": {
"title": "Test Checklist",
"kind": "checklist"
},
"result": {
"id": "45b14881-76c6-484f-84c8-f1f951d23bd3",
"title": "Test Checklist",
"kind": "checklist",
"content": "A document was created and is now visible to the user."
}
},
{
"state": "call",
"step": 1,
"toolCallId": "toolu_01QtW2M7x28CsRF8SaAyea1C",
"toolName": "updateDocument",
"args": {
"id": "45b14881-76c6-484f-84c8-f1f951d23bd3",
"description": "Create a checklist with the following items:\n\n# Big Idea\n- [ ] Define the main concept\n- [ ] Identify unique angle\n\n# The USP\n- [ ] List unique selling points\n- [ ] Define competitive advantage\n\n# Your Appeal\n- [ ] Identify emotional triggers\n- [ ] Define core message\n\n# Your Hook\n- [ ] Create attention-grabbing opener\n- [ ] Develop compelling story\n\n# Your Promise\n- [ ] State main benefit\n- [ ] Define expected outcome\n\n# Wants, Needs, Features, Benefits & Costs\n- [ ] List customer wants\n- [ ] Identify customer needs\n- [ ] Detail product features\n- [ ] Explain benefits\n- [ ] Address costs/investment\n\n# Claims & Proof\n## Specific Claims\n- [ ] List main claims\n- [ ] Detail supporting evidence\n\n## Proof Of Those Claims\n- [ ] Gather testimonials\n- [ ] Compile case studies\n\n## Who Is This For? Bullseye Clients\n- [ ] Define ideal customer profile\n- [ ] List qualifying characteristics"
}
}
],
"revisionId": "78df805d-08c2-4b84-a1ab-de2e3e79389f"
},
"12": {
"id": "64a192e6-a6bb-410d-8e30-93784a8bb4cb",
"createdAt": "2025-02-24T18:15:34.372Z",
"role": "user",
"content": "same result, no update. try again please",
"parts": [
{
"type": "text",
"text": "same result, no update. try again please"
}
]
},
"13": {
"id": "97494459-186d-493d-8274-5b2a4b4062db",
"createdAt": "2025-02-24T18:22:03.711Z",
"role": "user",
"content": "hmmm... try again please",
"parts": [
{
"type": "text",
"text": "hmmm... try again please"
}
]
},
"14": {
"id": "4550a6ab-37e4-4378-bde3-8de3988c2356",
"createdAt": "2025-02-24T18:22:36.248Z",
"role": "user",
"content": "hmmm... try again please",
"parts": [
{
"type": "text",
"text": "hmmm... try again please"
}
]
},
"15": {
"id": "bf3aea6f-f405-405f-bc77-a95e750ee65c",
"createdAt": "2025-02-24T18:24:55.004Z",
"role": "user",
"content": "hmmm... try again please",
"parts": [
{
"type": "text",
"text": "hmmm... try again please"
}
]
},
"16": {
"id": "456a77f3-d639-49ac-b737-31425201dba5",
"createdAt": "2025-02-24T18:26:44.083Z",
"role": "user",
"content": "hmmm... try again please",
"parts": [
{
"type": "text",
"text": "hmmm... try again please"
}
]
},
"17": {
"id": "6749656d-a487-465e-b873-7c6599c71f3c",
"createdAt": "2025-02-24T18:29:21.820Z",
"role": "user",
"content": "still errors. again please",
"parts": [
{
"type": "text",
"text": "still errors. again please"
}
]
},
"18": {
"id": "0bfd29c1-3196-4ae7-a9c9-269058871f27",
"createdAt": "2025-02-24T18:31:30.363Z",
"role": "user",
"content": "still errors. again please",
"parts": [
{
"type": "text",
"text": "still errors. again please"
}
]
},
"19": {
"id": "0880d8be-4fbc-44c3-b96a-4ba281db687e",
"createdAt": "2025-02-24T18:32:34.100Z",
"role": "user",
"content": "still errors. again please",
"parts": [
{
"type": "text",
"text": "still errors. again please"
}
]
},
"20": {
"id": "d68c2809-ac06-4b67-bc98-4752a03397ff",
"createdAt": "2025-02-24T18:34:33.180Z",
"role": "user",
"content": "still errors. again please\n\n",
"parts": [
{
"type": "text",
"text": "still errors. again please\n\n"
}
]
},
"21": {
"id": "1904f4ed-1d3b-4fb6-832e-5b18247679bc",
"createdAt": "2025-02-24T18:35:31.692Z",
"role": "user",
"content": "still errors. again please",
"parts": [
{
"type": "text",
"text": "still errors. again please"
}
]
},
"timestamp": "2025-02-24T18:35:32.449Z",
"level": "DEBUG",
"message": "Messages in repairToolCall:"
}
{
"timestamp": "2025-02-24T18:35:32.456Z",
"level": "DEBUG",
"message": "Chat title",
"title": "User Identifies as an Admin"
}
{
"timestamp": "2025-02-24T18:35:32.460Z",
"level": "ERROR",
"message": "Error during stream execution",
"chatId": "ebf0f635-22c3-4f19-ac97-1021193833fc",
"error": "ToolInvocation must have a result: {"state":"call","step":1,"toolCallId":"toolu_01QtW2M7x28CsRF8SaAyea1C","toolName":"updateDocument","args":{"id":"45b14881-76c6-484f-84c8-f1f951d23bd3","description":"Create a checklist with the following items:\n\n# Big Idea\n- [ ] Define the main concept\n- [ ] Identify unique angle\n\n# The USP\n- [ ] List unique selling points\n- [ ] Define competitive advantage\n\n# Your Appeal\n- [ ] Identify emotional triggers\n- [ ] Define core message\n\n# Your Hook\n- [ ] Create attention-grabbing opener\n- [ ] Develop compelling story\n\n# Your Promise\n- [ ] State main benefit\n- [ ] Define expected outcome\n\n# Wants, Needs, Features, Benefits & Costs\n- [ ] List customer wants\n- [ ] Identify customer needs\n- [ ] Detail product features\n- [ ] Explain benefits\n- [ ] Address costs/investment\n\n# Claims & Proof\n## Specific Claims\n- [ ] List main claims\n- [ ] Detail supporting evidence\n\n## Proof Of Those Claims\n- [ ] Gather testimonials\n- [ ] Compile case studies\n\n## Who Is This For? Bullseye Clients\n- [ ] Define ideal customer profile\n- [ ] List qualifying characteristics"}}"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants