{"record":{"id":"f48a27869fd1c1f7","repo":"wavetermdev/waveterm","slug":"message-d-expected-anthropicchatmessage-got-t","errorCode":null,"errorMessage":"message %d: expected *anthropicChatMessage, got %T","messagePattern":"message (.+?): expected \\*anthropicChatMessage, got %T","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/aiusechat/anthropic/anthropic-convertmessage.go","lineNumber":825,"sourceCode":"\treturn &anthropicChatMessage{\n\t\tMessageId: uuid.New().String(),\n\t\tRole:      \"user\",\n\t\tContent:   contentBlocks,\n\t}, nil\n}\n\n// ConvertAIChatToUIChat converts an AIChat to a UIChat for Anthropic\nfunc ConvertAIChatToUIChat(aiChat uctypes.AIChat) (*uctypes.UIChat, error) {\n\tif aiChat.APIType != uctypes.APIType_AnthropicMessages {\n\t\treturn nil, fmt.Errorf(\"APIType must be '%s', got '%s'\", uctypes.APIType_AnthropicMessages, aiChat.APIType)\n\t}\n\n\tuiMessages := make([]uctypes.UIMessage, 0, len(aiChat.NativeMessages))\n\n\tfor i, nativeMsg := range aiChat.NativeMessages {\n\t\tanthropicMsg, ok := nativeMsg.(*anthropicChatMessage)\n\t\tif !ok {\n\t\t\treturn nil, fmt.Errorf(\"message %d: expected *anthropicChatMessage, got %T\", i, nativeMsg)\n\t\t}\n\n\t\tuiMsg := anthropicMsg.ConvertToUIMessage()\n\t\tif uiMsg != nil {\n\t\t\tuiMessages = append(uiMessages, *uiMsg)\n\t\t}\n\t}\n\n\treturn &uctypes.UIChat{\n\t\tChatId:     aiChat.ChatId,\n\t\tAPIType:    aiChat.APIType,\n\t\tModel:      aiChat.Model,\n\t\tAPIVersion: aiChat.APIVersion,\n\t\tMessages:   uiMessages,\n\t}, nil\n}\n\nfunc GetFunctionCallInputByToolCallId(aiChat uctypes.AIChat, toolCallId string) *uctypes.AIFunctionCallInput {","sourceCodeStart":807,"sourceCodeEnd":843,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/pkg/aiusechat/anthropic/anthropic-convertmessage.go#L807-L843","documentation":"ConvertAIChatToUIChat type-asserts each entry of aiChat.NativeMessages to *anthropicChatMessage. A native message of any other concrete type cannot be converted to the Anthropic UI representation, so the loop fails fast with the index and dynamic type of the offending message.","triggerScenarios":"ConvertAIChatToUIChat called on an AIChat whose NativeMessages slice contains a message not created by this package (e.g. *openaiChatMessage, a generic struct, or nil of a different type).","commonSituations":"Appending messages from another provider into a shared NativeMessages slice; custom message wrappers stored in the chat; cache deserialization yielding the wrong concrete type.","solutions":["Only include *anthropicChatMessage values in NativeMessages for Anthropic chats","Rebuild the chat messages via the Anthropic converter so they get the right concrete type","Filter or convert foreign messages before calling ConvertAIChatToUIChat"],"exampleFix":"// before\naiChat.NativeMessages = append(aiChat.NativeMessages, someOpenAIMessage)\n// after\nanthMsg, err := anthropic.ConvertAIMessageToAnthropicChatMessage(aiMsg)\naiChat.NativeMessages = append(aiChat.NativeMessages, anthMsg)","handlingStrategy":"type-guard","validationCode":"for i, m := range aiChat.NativeMessages {\n    if _, ok := m.(*anthropicChatMessage); !ok {\n        return fmt.Errorf(\"native message %d is not an anthropic message\", i)\n    }\n}","typeGuard":"func asAnthropicMessage(m uctypes.NativeMessage) (*anthropicChatMessage, bool) {\n    msg, ok := m.(*anthropicChatMessage)\n    return msg, ok\n}","tryCatchPattern":"uiChat, err := anthropic.ConvertAIChatToUIChat(aiChat)\nif err != nil {\n    var idx int; var typ string\n    if _, scan := fmt.Sscanf(err.Error(), \"message %d: expected *anthropicChatMessage, got %s\", &idx, &typ); scan == nil {\n        return fmt.Errorf(\"chat contains foreign message at index %d (%s)\", idx, typ)\n    }\n    return err\n}","preventionTips":["Only append messages produced by the Anthropic converter to NativeMessages","Avoid hand-rolled structs in NativeMessages slices","Add tests asserting every NativeMessage type-asserts to *anthropicChatMessage"],"tags":["go","anthropic","type-assertion"],"backgroundTag":"unexpected-message-type","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}