{"record":{"id":"00f9f33b8c016a98","repo":"microsoft/autogen","slug":"messages-should-not-be-provided-in-options-00f9f3","errorCode":null,"errorMessage":"Messages should not be provided in options","messagePattern":"Messages should not be provided in options","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.OpenAI.V1/Agent/OpenAIChatAgent.cs","lineNumber":84,"sourceCode":"    {\n    }\n\n    /// <summary>\n    /// Create a new instance of <see cref=\"OpenAIChatAgent\"/>.\n    /// </summary>\n    /// <param name=\"openAIClient\">openai client</param>\n    /// <param name=\"name\">agent name</param>\n    /// <param name=\"systemMessage\">system message</param>\n    /// <param name=\"options\">chat completion option. The option can't contain messages</param>\n    public OpenAIChatAgent(\n        OpenAIClient openAIClient,\n        string name,\n        ChatCompletionsOptions options,\n        string systemMessage = \"You are a helpful AI assistant\")\n    {\n        if (options.Messages is { Count: > 0 })\n        {\n            throw new ArgumentException(\"Messages should not be provided in options\");\n        }\n\n        this.openAIClient = openAIClient;\n        this.Name = name;\n        this.options = options;\n        this.systemMessage = systemMessage;\n    }\n\n    public string Name { get; }\n\n    public async Task<IMessage> GenerateReplyAsync(\n        IEnumerable<IMessage> messages,\n        GenerateReplyOptions? options = null,\n        CancellationToken cancellationToken = default)\n    {\n        var settings = this.CreateChatCompletionsOptions(options, messages);\n        var reply = await this.openAIClient.GetChatCompletionsAsync(settings, cancellationToken);\n","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.OpenAI.V1/Agent/OpenAIChatAgent.cs#L66-L102","documentation":"OpenAIChatAgent's constructor rejects ChatCompletionsOptions that already contain messages. The agent injects its system message and the per-call message list itself; pre-populated messages in the options would be silently mixed into every request, so the constructor fails fast with ArgumentException.","triggerScenarios":"Constructing new OpenAIChatAgent(client, name, options) where options.Messages was populated (e.g. options.AddMessage(...) before passing it in, or options reused from a previous request).","commonSituations":"Reusing a ChatCompletionsOptions object from earlier direct SDK calls; copy-pasting SDK sample code that builds a fully-populated options object into an AutoGen agent constructor.","solutions":["Remove all messages from options before constructing the agent (set only Temperature, MaxTokens, tools, etc.)","Pass the system prompt via the systemMessage parameter, not as a message in options","Send conversation content through the messages argument of GenerateReplyAsync, never through options"],"exampleFix":"// before\nvar options = new ChatCompletionsOptions();\noptions.Messages.Add(new ChatRequestSystemMessage(\"You are terse\"));\nvar agent = new OpenAIChatAgent(client, \"gpt\", options);\n\n// after\nvar options = new ChatCompletionsOptions();\nvar agent = new OpenAIChatAgent(client, \"gpt\", options, systemMessage: \"You are terse\");","handlingStrategy":"validation","validationCode":"if (options.Messages is { Count: > 0 })\n    throw new InvalidOperationException(\"Clear options.Messages before constructing the agent\");","typeGuard":null,"tryCatchPattern":"try { agent = new OpenAIChatAgent(client, name, options); }\ncatch (ArgumentException ex) when (ex.Message == \"Messages should not be provided in options\")\n{\n    options.Messages.Clear();\n    agent = new OpenAIChatAgent(client, name, options);\n}","preventionTips":["Never reuse a populated ChatCompletionsOptions with the agent","Pass the system prompt via the systemMessage parameter","Send chat content per-call, not via options"],"tags":["openai","constructor","options","validation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}