Skip to main content
The schemaTask function allows you to define a task with a runtime payload schema. This schema is used to validate the payload before running the task or when triggering a task directly. If the payload does not match the schema, the task will not execute.

Usage

schemaTask takes all the same options as task, with the addition of a schema field. The schema field is a schema parser function from a schema library or or a custom parser function.
We will probably eventually combine task and schemaTask into a single function, but because that would be a breaking change, we are keeping them separate for now.
When you trigger the task directly, the payload will be validated against the schema before the run is created:
The error thrown when the payload does not match the schema will be the same as the error thrown by the schema parser function. For example, if you are using Zod, the error will be a ZodError. We will also validate the payload every time before the task is run, so you can be sure that the payload is always valid. In the example above, the task would fail with a TaskPayloadParsedError error and skip retrying if the payload does not match the schema.

Input/output schemas

Certain schema libraries, like Zod, split their type inference into “schema in” and “schema out”. This means that you can define a single schema that will produce different types when triggering the task and when running the task. For example, you can define a schema that has a default value for a field, or a string coerced into a date:
In this case, the trigger payload type is { name?: string, age: number; dob: string }, but the run payload type is { name: string, age: number; dob: Date }. So you can trigger the task with a payload like this:

Task-backed AI tools

Use a schemaTask as the implementation of a Vercel AI SDK tool: the model calls the tool, and Trigger runs your task as a subtask with tool-call metadata, optional chat context, and the same payload validation as a normal trigger. Prefer building the tool with the AI SDK’s tool() and passing execute: ai.toolExecute(yourTask). You keep full control of description, inputSchema, and AI-SDK-only options (for example experimental_toToolResultContent), and your types follow the ai version installed in your app.
experimental_toToolResultContent and other tool-level options belong on tool({ ... }), not on ai.toolExecute:
Inside the task run, you can read tool execution context with ai.currentToolOptions() (and helpers like ai.toolCallId(), ai.chatContext() when running inside a chat.agent):
See the AI SDK tool execution options for fields passed through the runtime.
ai.toolExecute works with schemaTask definitions that use Zod, ArkType, or any schema that provides a JSON schema via .toJsonSchema() (same coverage as the legacy ai.tool wrapper).

Deprecated: ai.tool

The ai.tool(task, options?) helper is deprecated. It constructs an AI SDK Tool for you (using tool() for Zod-like schemas and dynamicTool() otherwise) and may be removed in a future major version. New code should use tool({ ..., execute: ai.toolExecute(task) }) as shown above.

Legacy ai.tool example (deprecated)

Supported schema types

Zod

You can use the Zod schema library to define your schema. The schema will be validated using Zod’s parse function.

Yup

Superstruct

ArkType

@effect/schema

runtypes

valibot

typebox

Custom parser function

You can also define a custom parser function that will be called with the payload before the task is run. The parser function should return the parsed payload or throw an error if the payload is invalid.