microsoft/autogen · error · ArgumentOutOfRangeException
Specified argument was out of the range of valid values. (Pa
Error message
Specified argument was out of the range of valid values. (Parameter 'arrayIndex')
What it means
Bounds check in ICollection<AIContent>.CopyTo: arrayIndex must satisfy 0 <= arrayIndex < array.Length, otherwise ArgumentOutOfRangeException(nameof(arrayIndex)). Note it rejects arrayIndex == array.Length even for an empty collection, which is slightly stricter than some BCL implementations.
Source
Thrown at dotnet/src/Microsoft.AutoGen/AgentChat/Abstractions/Messages.cs:326
}
/// <inheritdoc cref="ICollection{AIContent}.Contains" />
public bool Contains(AIContent item)
{
return this.Content.Any(x => x.AIContent == item);
}
/// <inheritdoc cref="ICollection{AIContent}.CopyTo" />
public void CopyTo(AIContent[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException(nameof(array));
}
if (arrayIndex < 0 || arrayIndex >= array.Length)
{
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
}
if (array.Length - arrayIndex < this.Content.Count)
{
throw new ArgumentException("The number of elements in the source is greater than the available space from arrayIndex to the end of the destination array.");
}
for (var i = 0; i < this.Content.Count; i++)
{
array[arrayIndex + i] = this.Content[i].AIContent;
}
}
/// <inheritdoc cref="IEnumerable{AIContent}.GetEnumerator" />
public IEnumerator<AIContent> GetEnumerator()
{
return this.Content.Select(x => x.AIContent).GetEnumerator();
}View on GitHub (pinned to 027ecf0a37)
Solutions
- Pass an index strictly inside the array: 0 <= arrayIndex < array.Length (use the count of already-filled elements, not array.Length)
- Ensure the destination array is non-empty; skip CopyTo entirely when there is nothing to copy
- Double-check offset arithmetic when copying multiple collections into one buffer
Example fix
// before msg.Content.CopyTo(dest, dest.Length); // always out of range // after msg.Content.CopyTo(dest, filled); // filled = number of elements already written
Defensive patterns
Strategy: validation
Validate before calling
if (arrayIndex < 0 || arrayIndex >= array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex), "must be 0 <= index < length"); msg.Content.CopyTo(array, arrayIndex);
Prevention
- Use the count of already-written elements as the index, never array.Length
- Skip CopyTo when there is nothing to copy (empty source or empty destination)
- Wrap buffer offset math in one helper to avoid scattered index arithmetic
When it happens
Trigger: Calling CopyTo(array, negativeIndex) or CopyTo(array, array.Length) (e.g. appending after existing items but passing length instead of a valid last index), or on an empty/zero-length array where any index fails.
Common situations: Index arithmetic bugs when copying into a larger buffer at an offset; passing array.Length by mistake when the intent was 'end of data so far'; zero-length arrays from empty allocations.
Related errors
- Value cannot be null. (Parameter 'array')
- The number of elements in the source is greater than the ava
- The recent update is not a TextMessage
- The content has more than one choice. Please try another inp
- Invalid ChatResponseMessage
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/32a7bf2a85a2ff47.
Report an issue: GitHub.