microsoft/autogen · error · ArgumentException
Messages should not be provided in options
Error message
Messages should not be provided in options
What it means
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.
Source
Thrown at dotnet/src/AutoGen.OpenAI.V1/Agent/OpenAIChatAgent.cs:84
{
}
/// <summary>
/// Create a new instance of <see cref="OpenAIChatAgent"/>.
/// </summary>
/// <param name="openAIClient">openai client</param>
/// <param name="name">agent name</param>
/// <param name="systemMessage">system message</param>
/// <param name="options">chat completion option. The option can't contain messages</param>
public OpenAIChatAgent(
OpenAIClient openAIClient,
string name,
ChatCompletionsOptions options,
string systemMessage = "You are a helpful AI assistant")
{
if (options.Messages is { Count: > 0 })
{
throw new ArgumentException("Messages should not be provided in options");
}
this.openAIClient = openAIClient;
this.Name = name;
this.options = options;
this.systemMessage = systemMessage;
}
public string Name { get; }
public async Task<IMessage> GenerateReplyAsync(
IEnumerable<IMessage> messages,
GenerateReplyOptions? options = null,
CancellationToken cancellationToken = default)
{
var settings = this.CreateChatCompletionsOptions(options, messages);
var reply = await this.openAIClient.GetChatCompletionsAsync(settings, cancellationToken);
View on GitHub (pinned to 027ecf0a37)
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
Example fix
// before
var options = new ChatCompletionsOptions();
options.Messages.Add(new ChatRequestSystemMessage("You are terse"));
var agent = new OpenAIChatAgent(client, "gpt", options);
// after
var options = new ChatCompletionsOptions();
var agent = new OpenAIChatAgent(client, "gpt", options, systemMessage: "You are terse"); Defensive patterns
Strategy: validation
Validate before calling
if (options.Messages is { Count: > 0 })
throw new InvalidOperationException("Clear options.Messages before constructing the agent"); Try / catch
try { agent = new OpenAIChatAgent(client, name, options); }
catch (ArgumentException ex) when (ex.Message == "Messages should not be provided in options")
{
options.Messages.Clear();
agent = new OpenAIChatAgent(client, name, options);
} Prevention
- Never reuse a populated ChatCompletionsOptions with the agent
- Pass the system prompt via the systemMessage parameter
- Send chat content per-call, not via options
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Unsupported config type {config.GetType()}
- Required create args are missing: {required_create_args - cr
- Parameter name cannot be null
- Unknown message type: {m.GetType()}
- Content is null
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/00f9f33b8c016a98.
Report an issue: GitHub.