dotnet/efcore · error · InvalidOperationException

The type ' ' is being used as a vector, but the vector data…

Error message

The type '{clrType}' is being used as a vector, but the vector data type cannot be inferred. Only 'ReadOnlyMemory<byte>, ReadOnlyMemory<sbyte>, ReadOnlyMemory<float>, byte[], sbyte[], and float[] are supported.

What it means

Thrown by CosmosVectorType.CreateDefaultVectorDataType when the CLR type's element type is not sbyte, byte, or float. The factory unwraps ReadOnlyMemory<T> or IEnumerable<T> and maps only those three element types (Int8, Uint8, Float32); anything else cannot be expressed as a Cosmos vector data type.

Solutions

  1. Change the embedding property CLR type to one of: ReadOnlyMemory<byte>, byte[], ReadOnlyMemory<sbyte>, sbyte[], ReadOnlyMemory<float>, float[].
  2. If your upstream model produces doubles/longs, cast to float before storing and back after reading.
  3. Re-validate the model to confirm the data type is now inferred (VectorDataType.Int8/Uint8/Float32).

Example fix

// before
public class Doc { public ReadOnlyMemory<double> Embedding { get; set; } }

// after
public class Doc { public ReadOnlyMemory<float> Embedding { get; set; } }
Defensive patterns

Strategy: type-guard

Validate before calling

// Reject unsupported vector CLR types at startup.
static bool IsSupportedVectorType(Type t)
    => t == typeof(ReadOnlyMemory<byte>) || t == typeof(byte[])
    || t == typeof(ReadOnlyMemory<sbyte>) || t == typeof(sbyte[])
    || t == typeof(ReadOnlyMemory<float>) || t == typeof(float[]);
foreach (var et in modelBuilder.Model.GetEntityTypes())
foreach (var p in et.GetProperties())
    if (p.GetVectorDistanceFunction() is not null && !IsSupportedVectorType(p.ClrType))
        throw new InvalidOperationException($"{p.Name} uses an unsupported vector CLR type {p.ClrType}.");

Type guard

static bool IsSupportedVectorType(Type t) =>
    t == typeof(ReadOnlyMemory<byte>) || t == typeof(byte[]) ||
    t == typeof(ReadOnlyMemory<sbyte>) || t == typeof(sbyte[]) ||
    t == typeof(ReadOnlyMemory<float>) || t == typeof(float[]);

Prevention

When it happens

Trigger: Configuring IsVectorProperty on a property whose CLR type is e.g. double[], long[], ReadOnlyMemory<double>, Vector128, a custom struct, or List<decimal>. ValidateVectorProperty calls this factory when both distance function and dimensions are set.

Common situations: Using a higher-precision numeric type for embeddings; copying embedding DTOs from another SDK; using System.Numerics vector types directly.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/0aef4881e7d8fa55. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Cosmos/Metadata/Internal/CosmosVectorType.cs:33

{
    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public static VectorDataType CreateDefaultVectorDataType(Type clrType)
    {
        var elementType = clrType.TryGetElementType(typeof(ReadOnlyMemory<>))?.UnwrapNullableType()
            ?? clrType.TryGetElementType(typeof(IEnumerable<>))?.UnwrapNullableType();

        return elementType == typeof(sbyte)
            ? VectorDataType.Int8
            : elementType == typeof(byte)
                ? VectorDataType.Uint8
                : elementType == typeof(float)
                    ? VectorDataType.Float32
                    : throw new InvalidOperationException(CosmosStrings.BadVectorDataType(clrType.ShortDisplayName()));
    }
}

View on GitHub (pinned to 3a2006ef56)