clockworklabs/SpacetimeDB · error · InvalidOperationException

Unknown UUID version

Error message

Unknown UUID version

What it means

Uuid.GetVersion classifies a value as V4 or V7 from the version nibble (byte 6 high bits), with special cases for the all-zero NIL and all-ones MAX UUIDs. Any other version nibble (v1, v2, v3, v5, v6, v8) or a random 128-bit value throws InvalidOperationException, because the runtime only understands versions 4 and 7.

Solutions

  1. Generate UUIDs via the library's own constructors (FromRandomBytesV4, FromCounterV7) so the version is always 4 or 7
  2. When ingesting foreign UUIDs, inspect the raw version nibble yourself and branch, instead of calling GetVersion
  3. Validate inbound identifiers at the boundary and re-key non-v4/v7 values to library-generated ones

Example fix

// before
var u = Uuid.FromBigEndian(legacySystemBytes); // holds a v1 UUID
var v = u.GetVersion(); // InvalidOperationException: Unknown UUID version

// after
var version = (legacySystemBytes[6] >> 4) & 0x0F;
if (version is 4 or 7)
{
    var v = Uuid.FromBigEndian(legacySystemBytes).GetVersion();
}
else
{
    // foreign UUID: re-key or handle explicitly
}
Defensive patterns

Strategy: try-catch

Type guard

static bool TryGetUuidVersion(Uuid u, out object? version)
{
    try { version = u.GetVersion(); return true; }
    catch (InvalidOperationException) { version = null; return false; }
}

Try / catch

try
{
    var v = u.GetVersion();
}
catch (InvalidOperationException)
{
    // foreign UUID (v1/v3/v5/v6/...): re-key to a v4/v7 id or handle explicitly
}

Prevention

When it happens

Trigger: Calling GetVersion on a Uuid constructed from a foreign UUID — a v1 time-based UUID from a legacy database, a namespaced v3/v5, or v6 from another library — or on an arbitrary 128-bit value that is not a UUID at all.

Common situations: Migrating identifiers from systems that emit non-v4/v7 UUIDs; debugging by parsing random 128-bit values as Uuid; third-party integrations with their own UUID generators.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@6dee26c6ef (2026-08-20). Data as JSON: /api/errors/5ac017e12aec02ac. Report an issue: GitHub.

Appendix: source

Thrown at crates/bindings-csharp/BSATN.Runtime/BSATN/Uuid.cs:218

    }

    /// <summary>
    /// Returns the <see cref="UuidVersion"/> of this <see cref="Uuid"/>.
    ///
    /// Throws <see cref="InvalidOperationException"/> if the <see cref="Uuid"/> version is unknown.
    /// </summary>
    public UuidVersion GetVersion()
    {
        var bytes = value.ToBytesBigEndian();
        var version = (bytes[6] >> 4) & 0x0F;

        return version switch
        {
            4 => UuidVersion.V4,
            7 => UuidVersion.V7,
            _ => this == Uuid.NIL ? UuidVersion.Nil
            : this == Uuid.MAX ? UuidVersion.Max
            : throw new InvalidOperationException("Unknown UUID version"),
        };
    }

    private static void GuidToBigEndianBytes(ReadOnlySpan<byte> guidBytes, Span<byte> be)
    {
        // Guid’s weird internal layout (mixed-endian)

        // time_low (4 bytes) — little-endian → reverse
        be[0] = guidBytes[3];
        be[1] = guidBytes[2];
        be[2] = guidBytes[1];
        be[3] = guidBytes[0];

        // time_mid (2 bytes) — little-endian
        be[4] = guidBytes[5];
        be[5] = guidBytes[4];

        // time_hi_and_version (2 bytes) — little-endian

View on GitHub (pinned to 6dee26c6ef)