mastra-ai/mastra · error · Error
No task: provide targetType+targetId or task
Error message
No task: provide targetType+targetId or task
What it means
runExperiment needs a task to run per item: either a registered target (targetType+targetId) or an inline `task` function. If neither is present the config cannot produce an execFn, so this plain Error is thrown during setup and the run is marked failed before execution.
Source
Thrown at packages/core/src/datasets/experiment/index.ts:353
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');
}
} catch (err) {
await markFailedOnSetupError(err);
throw err; // unreachable, but satisfies TS control flow
}
// Tool mocks only apply to agent targets. If a dataset carrying toolMocks is reused
// against a task/workflow/scorer target, the mocks are silently ignored — warn once
// (not per item) so the misconfiguration is visible without log spam.
const itemsWithToolMocks = items.filter(item => item.toolMocks?.length).length;
if (targetType !== 'agent' && itemsWithToolMocks > 0) {
mastra
.getLogger()
?.warn(
`Experiment target is "${config.task ? 'task' : targetType}" but ${itemsWithToolMocks} of ${items.length} dataset items declare toolMocks. ` +
`Tool mocks only apply to agent targets and will be ignored.`,
);
}View on GitHub (pinned to 75dd419e61)
Solutions
- Provide a `task` function (item, signal) => result for inline execution
- Or provide both targetType and targetId referencing a registered agent/tool/workflow/scorer
- Validate the config before calling: ensure (targetType && targetId) || typeof task === 'function'
- Trace why neither branch was populated (e.g. a flag or lookup returning undefined)
Example fix
// before
await runExperiment({ datasetId: 'ds_123' }); // no task, no target
// after
await runExperiment({
datasetId: 'ds_123',
targetType: 'agent',
targetId: 'my-agent',
}); Defensive patterns
Strategy: validation
Validate before calling
if (!((config.targetType && config.targetId) || typeof config.task === 'function')) {
throw new Error('runExperiment requires targetType+targetId or a task function');
} Type guard
function hasTask(c: { targetType?: string; targetId?: string; task?: unknown }): boolean {
return (typeof c.targetType === 'string' && typeof c.targetId === 'string') || typeof c.task === 'function';
} Try / catch
try {
await runExperiment(config);
} catch (err) {
if (err instanceof Error && err.message.includes('No task')) {
console.error('Experiment config must define targetType+targetId or task');
} else throw err;
} Prevention
- Validate the full experiment config with a schema requiring task XOR target
- Never leave both task and target fields conditionally unset
- Type the config so task/target are a discriminated union
- Add a CI check that renders and validates experiment configs before runs
When it happens
Trigger: Calling runExperiment (or startExperiment/startExperimentAsync/executeRun) with a config containing neither targetType+targetId nor a task function — e.g. both fields undefined after config assembly.
Common situations: Programmatic config building where task was expected to be injected; copying an example that used datasetId+task but dropping the task; conditional logic that skips both branches (e.g. only setting targetType when some flag is true).
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- Unknown target type: ${targetType}
- No data source: provide datasetId or data
- AGENT_DURABLE_METHOD_NOT_AVAILABLE
- Unsupported output format: ${format}
- Experiment not found: ${experimentIdA}
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/27056009d1f6d815.
Report an issue: GitHub.