{"record":{"id":"26d66354927aea52","repo":"mastra-ai/mastra","slug":"bug-add-handling-for-message-role-message-role","errorCode":null,"errorMessage":"BUG: add handling for message role ${message.role} in message ${JSON.stringify(message, null, 2)}","messagePattern":"BUG: add handling for message role (.+?) in message (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/agent/message-list/detection/TypeDetector.ts","lineNumber":278,"sourceCode":"      if (part.type === 'reasoning' && 'signature' in part) return false;\n      if (part.type === 'redacted-reasoning') return false;\n    }\n\n    // If no distinguishing features are found, the message shape is still\n    // compatible with the v5 model format.\n    return true;\n  }\n\n  /**\n   * Get the normalized role for a message\n   * Maps `tool` to `assistant` because tool messages are displayed as part of\n   * the assistant conversation.\n   */\n  static getRole(message: MessageInput): MastraDBMessage['role'] {\n    if (message.role === 'assistant' || message.role === 'tool') return 'assistant';\n    if (message.role === 'user') return 'user';\n    if (message.role === 'system') return 'system';\n    throw new Error(\n      `BUG: add handling for message role ${message.role} in message ${JSON.stringify(message, null, 2)}`,\n    );\n  }\n}\n","sourceCodeStart":260,"sourceCodeEnd":283,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/agent/message-list/detection/TypeDetector.ts#L260-L283","documentation":"TypeDetector.getRole maps input message roles to the internal MastraDBMessage role ('user' | 'assistant' | 'system'); roles outside 'assistant' | 'tool' | 'user' | 'system' hit this 'BUG:' assertion. It is an internal invariant guard flagging an unsupported role that upstream code failed to filter.","triggerScenarios":"fromUIMessage or fromCoreMessage receiving a message whose role is anything other than user/assistant/tool/system — e.g. role 'function' from a legacy CoreMessage or a custom role string.","commonSituations":"Feeding AI SDK v3/v4 'function'-role messages into modern Memory; custom storage adapters returning stale message shapes; corrupt or hand-edited persistence records.","solutions":["Filter or remap unsupported roles ('function' -> merge into assistant tool-call history) before adding messages","Migrate stored messages to the supported role set","Validate message shape at your ingestion boundary","Report the triggering input as a bug if it comes from a supported AI SDK version"],"exampleFix":"// before\nadd({ role: 'function', name: 'x', content: '...' })\n// after\nadd({ role: 'tool', content: [{ type: 'tool-result', toolName: 'x', ... }] })","handlingStrategy":"type-guard","validationCode":"const ACCEPTED = ['user','assistant','tool','system'];\nif (!ACCEPTED.includes(msg.role)) throw new Error(`Role ${msg.role} not supported by MessageList`);","typeGuard":"function isSupportedRole(r: string): r is 'user'|'assistant'|'tool'|'system' { return ['user','assistant','tool','system'].includes(r); }","tryCatchPattern":"try { memory.addMessages([msg]); } catch (e) { if (e instanceof Error && e.message.startsWith('BUG: add handling for message role')) { console.error('Unsupported message role in input:', msg.role); } throw e; }","preventionTips":["Migrate legacy 'function'-role messages before ingestion","Validate message roles when reading from custom storage","Test custom adapters against MessageList with all role shapes"],"tags":["message-list","role-detection","internal-invariant"],"backgroundTag":"invalid-message-role","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}