LykosAI/StabilityMatrix · error · InvalidOperationException

BatchSize card is null

Error message

BatchSize card is null

What it means

WithBatchSize() looks up a 'BatchSize' node/card inside the document State JSON and throws InvalidOperationException('BatchSize card is null') when that key is missing or its value is null. The method assumes a standard inference state layout containing a BatchSize card.

Solutions

  1. Ensure the project state contains a BatchSize card (recreate via the default state template).
  2. Add the 'BatchSize' card to State before calling WithBatchSize, or set batch values directly on State.
  3. Verify state schema on load and repair/upgrade missing standard cards.
  4. Catch InvalidOperationException and fall back to direct State manipulation.

Example fix

// before
doc.State["BatchSize"] // null -> throws
// after
doc.State["BatchSize"] ??= new JObject
{
    ["BatchSize"] = 1,
    ["BatchCount"] = 1
};
doc = doc.WithBatchSize(4, 2);
Defensive patterns

Strategy: validation

Validate before calling

if (doc.State?["BatchSize"] is null)
    throw new InvalidOperationException("Document state lacks a BatchSize card");

Type guard

bool HasBatchSizeCard(InferenceProjectDocument d) =>
    d.State?["BatchSize"] is not null;

Try / catch

try { doc = doc.WithBatchSize(4, 2); }
catch (InvalidOperationException ex) when (ex.Message.Contains("BatchSize card"))
{ doc = doc.WithDefaultCards(); doc = doc.WithBatchSize(4, 2); }

Prevention

When it happens

Trigger: Calling WithBatchSize() on a document whose State exists but lacks a 'BatchSize' entry — e.g. a custom/minimal state, a state where the BatchSize node was deleted, or a state created by a different app version.

Common situations: Documents edited externally where the BatchSize card was removed; older project formats missing newer cards; programmatically built states that omit standard cards.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of LykosAI/StabilityMatrix@af93d6ef57 (2026-09-12). Data as JSON: /api/errors/ebe957feed55b055. Report an issue: GitHub.

Appendix: source

Thrown at StabilityMatrix.Avalonia/Models/InferenceProjectDocument.cs:122

        if (!state.TryGetPropertyValue(key, out var modelNode) || modelNode is null)
        {
            return false;
        }

        state[key] = modifier(modelNode);

        return true;
    }

    public InferenceProjectDocument WithBatchSize(int batchSize, int batchCount)
    {
        if (State is null)
            throw new InvalidOperationException("State is null");

        var document = (InferenceProjectDocument)Clone();

        var batchSizeCard =
            document.State!["BatchSize"] ?? throw new InvalidOperationException("BatchSize card is null");

        batchSizeCard["BatchSize"] = batchSize;
        batchSizeCard["BatchCount"] = batchCount;

        return document;
    }

    /// <inheritdoc />
    public object Clone()
    {
        var newObj = (InferenceProjectDocument)MemberwiseClone();
        // Clone State also since its mutable
        newObj.State = State == null ? null : JsonSerializer.SerializeToNode(State).Deserialize<JsonObject>();
        return newObj;
    }
}

View on GitHub (pinned to af93d6ef57)