invoke-ai/InvokeAI · warning · HTTPException

Cannot relate a model to itself.

Error message

Cannot relate a model to itself.

What it means

HTTP 400 raised by POST /models/relationships when model_key_1 and model_key_2 in the request body are identical. Relationships are strictly between two distinct models; relating a model to itself is meaningless and rejected before any service call.

Source

Thrown at invokeai/app/api/routers/model_relationships.py:124

        500: {"description": "Internal server error"},
    },
    summary="Add Model Relationship",
    description="Creates a **bidirectional** relationship between two models, allowing each to reference the other as related.",
)
def add_model_relationship(
    current_user: AdminUserOrDefault,
    req: ModelRelationshipCreateRequest = Body(..., description="The model keys to relate"),
) -> None:
    """
    Add a relationship between two models.

    Relationships are bidirectional and will be accessible from both models.

    - Raises 400 if keys are invalid or identical.
    - Raises 409 if the relationship already exists.
    """
    if req.model_key_1 == req.model_key_2:
        raise HTTPException(status_code=400, detail="Cannot relate a model to itself.")

    try:
        ApiDependencies.invoker.services.model_relationships.add_model_relationship(
            req.model_key_1,
            req.model_key_2,
        )
    except ValueError as e:
        raise HTTPException(status_code=409, detail=str(e))


@model_relationships_router.delete(
    "/",
    status_code=status.HTTP_204_NO_CONTENT,
    responses={
        204: {"description": "The relationship was successfully removed"},
        400: {"description": "Invalid model keys or self-referential relationship"},
        404: {"description": "The relationship does not exist"},
        422: {"description": "Validation error"},

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Check that model_key_1 !== model_key_2 in the request payload before sending
  2. Skip or filter self-pairs when iterating model lists to create relationships
  3. Correct the client code that is populating both keys with the same value

Example fix

// before
await api.addRelationship({ model_key_1: k, model_key_2: k });
// after
if (k1 !== k2) await api.addRelationship({ model_key_1: k1, model_key_2: k2 });
Defensive patterns

Strategy: validation

Validate before calling

if (req.model_key_1 === req.model_key_2) {
  throw new Error('Cannot relate a model to itself');
}

Try / catch

try {
  await api.addRelationship(req);
} catch (e) {
  if (e.status === 400 && e.body?.detail === 'Cannot relate a model to itself.') throw new Error('self-pair in payload');
  throw e;
}

Prevention

When it happens

Trigger: Sending the same key in both fields of the AddModelRelationshipsRequest body to POST /api/v1/models/relationships, typically from a client that builds the pair from a single selected model.

Common situations: UI drag-and-drop onto itself; scripting relationship creation from a list where duplicates weren't deduplicated; copy-paste of the same key into both form fields.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/badb015aa17717e8. Report an issue: GitHub.