infiniflow/ragflow · error · LookupError

Instance {model_obj.instance_id} not found for model {model_

Error message

Instance {model_obj.instance_id} not found for model {model_name}.

What it means

LookupError raised by get_api_key (direct-ID path) when TenantModelInstanceService.get_by_id(model_obj.instance_id) fails: the tenant_model row points at a missing tenant_model_instance row. The API key actually lives on the instance row, so an orphaned instance reference makes key retrieval impossible. A warning is logged with tenant_id, model_id, instance_id before the raise.

Source

Thrown at api/db/joint_services/tenant_model_service.py:459

        # Verify tenant ownership through the provider chain
        ok, provider_obj = TenantModelProviderService.get_by_id(model_obj.provider_id)
        if not ok:
            raise LookupError(f"Provider id={model_obj.provider_id} not found for model {model_name}.")
        if tenant_id != provider_obj.tenant_id:
            joined_tenants = TenantService.get_joined_tenants_by_user_id(tenant_id)
            joined_tenant_ids = [t["tenant_id"] for t in joined_tenants]
            if provider_obj.tenant_id not in joined_tenant_ids:
                raise LookupError(f"Tenant {tenant_id} has no access to provider owned by tenant {provider_obj.tenant_id}.")

        exist_inst, instance_obj = TenantModelInstanceService.get_by_id(model_obj.instance_id)
        if not exist_inst:
            logger.warning(
                "Direct-ID resolution: instance not found | tenant_id=%s model_id=%s instance_id=%s",
                tenant_id,
                model_name,
                model_obj.instance_id,
            )
            raise LookupError(f"Instance {model_obj.instance_id} not found for model {model_name}.")
        logger.debug(
            "Direct-ID resolution: resolved | tenant_id=%s model_id=%s instance_id=%s",
            tenant_id,
            model_name,
            model_obj.instance_id,
        )
        return instance_obj.api_key

    # Fall back to name-based resolution: model[@instance]@provider
    _, instance_name, provider_name = split_model_name(model_name)

    if not provider_name:
        raise LookupError("Provider name is required.")
    provider_obj = TenantModelProviderService.get_by_tenant_id_and_provider_name(tenant_id, provider_name)
    if not provider_obj:
        raise LookupError(f"Provider {provider_name} not found.")
    instance_obj = _resolve_instance_for_model(provider_obj, instance_name, model_name)
    return instance_obj.api_key

View on GitHub (pinned to 554fb1133a)

Solutions

  1. Re-add or re-save the provider's instance (Model Providers -> edit) so instances and models are regenerated consistently, then re-select the model.
  2. Delete the stale tenant_model row and re-register the model under a live instance.
  3. Audit tenant_model rows whose instance_id has no tenant_model_instance counterpart and clean them.
  4. Ensure provider-instance deletion cascades to or repoints dependent models.

Example fix

-- find models pointing at missing instances
SELECT m.id, m.instance_id FROM tenant_model m
LEFT JOIN tenant_model_instance i ON i.id = m.instance_id
WHERE i.id IS NULL;
Defensive patterns

Strategy: try-catch

Validate before calling

exist, m = TenantModelService.get_by_id(model_id)
if exist:
    ok_inst, _ = TenantModelInstanceService.get_by_id(m.instance_id)
    if not ok_inst:
        raise ValueError("Model references a deleted instance; re-save the provider")

Try / catch

try:
    key = get_api_key(tenant_id, model_id)
except LookupError as e:
    if "Instance" in str(e) and "not found" in str(e):
        # stale FK: re-save provider config to regenerate instances
        ...

Prevention

When it happens

Trigger: Calling get_api_key with a model id whose tenant_model.instance_id references a deleted instance row — e.g. an instance was removed during provider reconfiguration but the model row kept the stale FK, or partial deletion of a provider's instances.

Common situations: Editing a provider's API endpoints/instances and deleting the old instance while models still pointed at it; interrupted provider-edit transactions; DB restores with table inconsistency.

Related errors


AI-assisted analysis of infiniflow/ragflow@554fb1133a (2026-08-15). Data as JSON: /api/errors/761ae4f011bed09b. Report an issue: GitHub.