mastra-ai/mastra · error · Error

Target not found: ${targetType}/${targetId}

Error message

Target not found: ${targetType}/${targetId}

What it means

When an experiment specifies a registry-based task via targetType+targetId, runExperiment resolves the target through resolveTarget against the Mastra instance's registry (agents, tools, workflows, scorers). If resolution returns null/undefined, this Error is thrown: no registered object matches that type/ID (or the requested agentVersion).

Source

Thrown at packages/core/src/datasets/experiment/index.ts:335

            signal: itemSignal,
          });
          return { output: result, error: null, traceId: null };
        } catch (err: unknown) {
          return {
            output: null,
            error: {
              message: err instanceof Error ? err.message : String(err),
              stack: err instanceof Error ? err.stack : undefined,
            },
            traceId: null,
          };
        }
      };
    } else if (targetType && targetId) {
      // Registry-based target path (existing)
      const resolved = await resolveTarget(mastra, targetType, targetId, agentVersion);
      if (!resolved) {
        throw new Error(`Target not found: ${targetType}/${targetId}`);
      }
      const { target } = resolved;
      execFn = (item, itemSignal) => {
        // Merge global request context with per-item request context (item takes precedence)
        const mergedRequestContext =
          globalRequestContext || item.requestContext ? { ...globalRequestContext, ...item.requestContext } : undefined;
        return executeTarget(target, targetType, item, {
          signal: itemSignal,
          requestContext: mergedRequestContext,
          experimentId,
          versions,
          toolMocks: targetType === 'agent' ? item.toolMocks : undefined,
          unmockedToolPolicy:
            targetType === 'agent' ? (item.unmockedToolPolicy ?? config.unmockedToolPolicy ?? 'allow') : undefined,
        });
      };
    } else {
      throw new Error('No task: provide targetType+targetId or task');

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Verify the target is registered: check mastra.getAgent(targetId)/equivalent before running the experiment
  2. Correct the targetId (or rename it back) to match the registered key
  3. Omit or fix agentVersion so it matches a version that exists for the target
  4. Ensure runExperiment is called on the Mastra instance that actually registers the target

Example fix

// before
await runExperiment({ targetType: 'agent', targetId: 'support-agent-v2', ... });
// after
if (!mastra.getAgent('support-agent-v2')) throw new Error('agent not registered');
await runExperiment({ targetType: 'agent', targetId: 'support-agent-v2', ... });
Defensive patterns

Strategy: validation

Validate before calling

if (!mastra.getAgent(targetId) && !mastra.getTool(targetId) && !mastra.getWorkflow(targetId)) {
  throw new Error(`Target ${targetType}/${targetId} not registered on this Mastra instance`);
}

Type guard

function isRegisteredTarget(mastra: Mastra, t: { targetType: string; targetId: string }): boolean {
  switch (t.targetType) {
    case 'agent': return !!mastra.getAgent(t.targetId);
    case 'tool': return !!mastra.getTool(t.targetId);
    case 'workflow': return !!mastra.getWorkflow(t.targetId);
    default: return false;
  }
}

Try / catch

try {
  await runExperiment(config);
} catch (err) {
  if (err instanceof Error && err.message.startsWith('Target not found')) {
    console.error(`Register target or fix ID: ${err.message}`);
  } else throw err;
}

Prevention

When it happens

Trigger: Calling runExperiment with targetType 'agent' (or 'tool'/'workflow'/'scorer') and a targetId that is not registered on the Mastra instance, or with an agentVersion that does not exist for the named agent.

Common situations: Renaming an agent without updating experiment configs; registering the target on a different Mastra instance than the one running the experiment; referencing an old agentVersion after redeploying; typo in the target ID.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/0a8dd6e7d3d50e5f. Report an issue: GitHub.