{"record":{"id":"635f0913fd4590ef","repo":"Budibase/budibase","slug":"ai-tool-messages-are-not-supported","errorCode":null,"errorMessage":"AI tool messages are not supported","messagePattern":"AI tool messages are not supported","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/server/src/sdk/workspace/ai/llm/messages.ts","lineNumber":15,"sourceCode":"import type { Message } from \"@budibase/types\"\nimport type { ModelMessage } from \"ai\"\n\nexport function toPrompt(messages: Message[]): {\n  instructions?: string\n  messages: ModelMessage[]\n} {\n  const instructions: string[] = []\n  const modelMessages: ModelMessage[] = []\n  for (const message of messages) {\n    if (typeof message.content !== \"string\") {\n      throw new Error(\"AI message content must be a string\")\n    }\n    if (message.role === \"tool\") {\n      throw new Error(\"AI tool messages are not supported\")\n    }\n    if (message.role === \"system\") {\n      instructions.push(message.content)\n    } else {\n      modelMessages.push({\n        role: message.role,\n        content: message.content,\n      } as ModelMessage)\n    }\n  }\n  return {\n    instructions: instructions.join(\"\\n\\n\") || undefined,\n    messages: modelMessages,\n  }\n}\n","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/sdk/workspace/ai/llm/messages.ts#L1-L31","documentation":"During toPrompt conversion, any message with role \"tool\" is rejected with this Error because the prompt builder only supports system/user/assistant text messages. Tool-call result messages have no representation in the generated prompt, so the library fails fast rather than silently dropping context.","triggerScenarios":"Calling toPrompt with a conversation history that includes role \"tool\" messages — typically the output of a prior tool-calling LLM run that was replayed into the prompt builder.","commonSituations":"Replaying persisted chat history that contains tool-call turns; chaining multi-step agent runs where tool results are kept in the message list; passing Vercel AI SDK response messages directly to toPrompt.","solutions":["Remove role \"tool\" messages from the array before calling toPrompt.","Convert tool results into assistant/user text summaries if the context matters.","Use an API path that supports tool messages if tool context is required."],"exampleFix":"// before\ntoPrompt(messages)\n// after\ntoPrompt(messages.filter(m => m.role !== \"tool\"))","handlingStrategy":"validation","validationCode":"if (messages.some(m => m.role === \"tool\")) {\n  throw new Error(\"History contains tool messages; strip them before calling toPrompt\")\n}","typeGuard":"const hasToolMessages = (msgs: ModelMessage[]): boolean =>\n  msgs.some(m => m.role === \"tool\")","tryCatchPattern":"try {\n  return toPrompt(messages)\n} catch (e) {\n  if (e.message === \"AI tool messages are not supported\") {\n    return toPrompt(messages.filter(m => m.role !== \"tool\"))\n  }\n  throw e\n}","preventionTips":["Filter role \"tool\" messages out of history before prompt construction","Summarize tool results into assistant/user text if context is needed","Keep tool transcripts in a separate store from promptable history","Test multi-step agent flows for tool-message leakage into toPrompt"],"tags":["ai","messages","tool-calls"],"backgroundTag":"ai-tool-message-unsupported","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}