{"record":{"id":"f676df208e7915d6","repo":"CopilotKit/CopilotKit","slug":"cannot-extract-agent-name-from-message-with-role-f676df","errorCode":null,"errorMessage":"Cannot extract agent name from message with role ${message.role}","messagePattern":"Cannot extract agent name from message with role (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/runtime/src/v1-deprecated/graphql/message-conversion/agui-to-gql.ts","lineNumber":8,"sourceCode":"import * as gql from \"../types/converted/index\";\nimport { MessageRole } from \"../types/enums\";\nimport * as agui from \"@copilotkit/shared\"; // named agui for clarity, but this only includes agui message types\n\n// Helper function to extract agent name from message\nfunction extractAgentName(message: agui.Message): string {\n  if (message.role !== \"assistant\") {\n    throw new Error(\n      `Cannot extract agent name from message with role ${message.role}`,\n    );\n  }\n\n  return message.agentName || \"unknown\";\n}\n\n// Type guard for agent state message\nfunction isAgentStateMessage(message: agui.Message): boolean {\n  return (\n    message.role === \"assistant\" && \"agentName\" in message && \"state\" in message\n  );\n}\n\n// Type guard for messages with image property\nfunction hasImageProperty(\n  message: agui.Message,\n): message is agui.Message & { image: agui.ImageData } {","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/CopilotKit/CopilotKit/blob/68fbe97d87420bd84e8196965c71bd6e394a3f7a/packages/runtime/src/v1-deprecated/graphql/message-conversion/agui-to-gql.ts#L1-L26","documentation":"Internal helper that extracts the agent name from an AG-UI message. It only works on messages whose role is 'assistant'; any other role (user, tool, system, developer) is rejected because agent attribution only exists on assistant messages. Thrown during GraphQL message conversion when the code path expects an assistant message but receives something else.","triggerScenarios":"Calling agentName extraction (internally via aguiToGQL flows) on a message where message.role !== 'assistant', e.g. a user or tool message passed into a path that assumes assistant output.","commonSituations":"Custom message pipelines or misordered messages where a user/tool message is fed into assistant-only conversion logic; usually a bug in calling code rather than user config.","solutions":["Verify the message role before passing it to code that expects assistant messages","Filter messages by role === 'assistant' before conversion","Set agentName explicitly on assistant messages when constructing them"],"exampleFix":"// before\nconst name = extractAgentName(msg); // msg.role === 'user' -> throws\n// after\nif (msg.role === \"assistant\") {\n  const name = msg.agentName || \"unknown\";\n}","handlingStrategy":"type-guard","validationCode":"const isAssistant = (m) => m.role === \"assistant\";","typeGuard":"function isAssistantMessage(m: agui.Message): m is Extract<agui.Message, { role: \"assistant\" }> {\n  return m.role === \"assistant\";\n}","tryCatchPattern":null,"preventionTips":["Filter message lists by role before conversion","Set agentName on assistant messages you construct"],"tags":["agui","message-conversion","graphql"],"backgroundTag":"message-schema-validation-failed","analyzedSha":"68fbe97d87420bd84e8196965c71bd6e394a3f7a","analyzedAt":"2026-08-27T04:03:26.537Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}