{"record":{"id":"abeef5df7770c4de","repo":"microsoft/semantic-kernel","slug":"message-content-cannot-be-null","errorCode":null,"errorMessage":"Message content cannot be null.","messagePattern":"Message content cannot be null\\.","errorType":"validation","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/Bedrock/BedrockAgentChannel.cs","lineNumber":223,"sourceCode":"    }\n\n    private SessionState ParseHistoryToSessionState()\n    {\n        SessionState sessionState = new();\n\n        // We don't take the last message as it needs to be sent separately in another parameter.\n        if (this._history.Count > 1)\n        {\n            sessionState.ConversationHistory = new()\n            {\n                Messages = []\n            };\n\n            foreach (var message in this._history.Take(this._history.Count - 1))\n            {\n                if (message.Content is null)\n                {\n                    throw new InvalidOperationException(\"Message content cannot be null.\");\n                }\n                if (message.Role != AuthorRole.Assistant && message.Role != AuthorRole.User)\n                {\n                    throw new InvalidOperationException(\"Message role must be either Assistant or User.\");\n                }\n\n                sessionState.ConversationHistory.Messages.Add(new()\n                {\n                    Role = message.Role == AuthorRole.Assistant\n                        ? Amazon.BedrockAgentRuntime.ConversationRole.Assistant\n                        : Amazon.BedrockAgentRuntime.ConversationRole.User,\n                    Content = [\n                        new Amazon.BedrockAgentRuntime.Model.ContentBlock()\n                        {\n                            Text = message.Content,\n                        },\n                    ],\n                });","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Bedrock/BedrockAgentChannel.cs#L205-L241","documentation":"Thrown by BedrockAgentChannel while building the Bedrock session-state conversation history from the channel's message backlog. For every message except the last (which is sent separately), Content must be non-null. A null Content breaks the Bedrock message model, so InvalidOperationException is thrown. Note this runs over this._history, i.e., the accumulated channel history, not just the input.","triggerScenarios":"The agent channel history contains a ChatMessageContent whose Content is null (e.g., a message carrying only function-call metadata with no text, or a placeholder). This is checked before role validation in the same loop.","commonSituations":"A tool-call or function-step message with null Content was added to the channel history; a deserialized/round-tripped message lost its content; an empty assistant ack got persisted.","solutions":["Ensure every message added to the channel history has non-null Content (use string.Empty rather than null).","Filter out metadata-only messages before they enter the Bedrock channel history.","If converting from another provider, default null Content to an empty string."],"exampleFix":"// before\nhistory.Add(new ChatMessageContent(AuthorRole.Assistant, content: null)); // -> throws 179\n\n// after\nhistory.Add(new ChatMessageContent(AuthorRole.Assistant, content: \"\") );","handlingStrategy":"validation","validationCode":"foreach (var m in history.Take(history.Count - 1))\n{\n    if (m.Content is null)\n        throw new InvalidOperationException($\"Message (role {m.Role}) has null Content; use string.Empty.\");\n}","typeGuard":"static bool HistoryHasNoNullContent(IReadOnlyList<ChatMessageContent> h) =>\n    h.Take(h.Count - 1).All(m => m.Content is not null);","tryCatchPattern":"try { /* invoke bedrock agent */ }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Message content cannot be null\"))\n{ /* replace null Content with string.Empty in the channel history */ }","preventionTips":["Never add a ChatMessageContent with null Content to Bedrock channel history; use string.Empty.","Filter out metadata-only/function-step messages before they enter the history.","Default null Content to empty string when converting from other providers."],"tags":["validation","bedrock","agent","null-reference","history"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}