{"record":{"id":"5bd2aed7954cf4e5","repo":"microsoft/autogen","slug":"system-message-is-not-supported-when-message-from","errorCode":null,"errorMessage":"System message is not supported when message.From is the same with agent","messagePattern":"System message is not supported when message\\.From is the same with agent","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.OpenAI.V1/Extension/MessageExtension.cs","lineNumber":161,"sourceCode":"                }\n                else\n                {\n                    var userMessage = new ChatRequestUserMessage(msg.Content ?? throw new ArgumentException(\"Content is null\"));\n                    return [userMessage];\n                }\n            }\n            else\n            {\n                throw new ArgumentException($\"Unknown message type: {message.GetType()}\");\n            }\n        }\n        else\n        {\n            if (message is TextMessage textMessage)\n            {\n                if (textMessage.Role == Role.System)\n                {\n                    throw new ArgumentException(\"System message is not supported when message.From is the same with agent\");\n                }\n\n                return [new ChatRequestAssistantMessage(textMessage.Content)];\n            }\n            else if (message is ToolCallMessage toolCallMessage)\n            {\n                var assistantMessage = new ChatRequestAssistantMessage(string.Empty);\n                var toolCalls = toolCallMessage.ToolCalls.Select(tc => new ChatCompletionsFunctionToolCall(tc.FunctionName, tc.FunctionName, tc.FunctionArguments));\n                foreach (var tc in toolCalls)\n                {\n                    assistantMessage.ToolCalls.Add(tc);\n                }\n\n                return [assistantMessage];\n            }\n            else if (message is AggregateMessage<ToolCallMessage, ToolCallResultMessage> aggregateMessage)\n            {\n                var toolCallMessage1 = aggregateMessage.Message1;","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.OpenAI.V1/Extension/MessageExtension.cs#L143-L179","documentation":"In the own-message branch (message.From == agent.Name) of the deprecated conversion, a TextMessage with Role.System throws. Messages attributed to the agent map to ChatRequestAssistantMessage; an assistant-role slot cannot carry a system prompt, so the combination is rejected instead of silently mislabeling it.","triggerScenarios":"Replaying a system message whose From was set to the agent's name (e.g. echoing the whole conversation, including the system turn, back with From rewritten) into the same agent.","commonSituations":"History replay loops that stamp From = agent.Name on every prior turn; copying the agent's system message into the chat as a TextMessage and then attributing it to the agent.","solutions":["Keep system prompts out of the replayed history; supply them via systemMessage at agent construction","When echoing history, set From = null (or the true sender) for system-role messages","Filter Role.System messages out of anything you forward back to the agent"],"exampleFix":"// before\nvar replayed = new TextMessage(Role.System, systemPrompt, from: agent.Name);\nawait agent.SendAsync(replayed);\n\n// after\n// system prompt handled at construction:\nvar agent = new OpenAIChatAgent(client, \"gpt\", options, systemMessage: systemPrompt);\n// history replay contains only user/assistant messages","handlingStrategy":"validation","validationCode":"bool ok = textMsg.Role != Role.System || textMsg.From != agent.Name;\nif (!ok) textMsg = new TextMessage(Role.User, textMsg.Content, from: null);","typeGuard":"static bool IsOwnSystemMessage(TextMessage m, string agentName) =>\n    m.Role == Role.System && m.From == agent.Name;","tryCatchPattern":"catch (ArgumentException ex) when (ex.Message.Contains(\"System message is not supported\"))\n{\n    var demoted = new TextMessage(Role.User, textMsg.Content, from: null);\n    return await agent.SendAsync(demoted);\n}","preventionTips":["Never attribute system messages to the agent itself","Set system prompts at construction time","Filter Role.System turns during history replay"],"tags":["openai","system-message","history-replay","role"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}