Generate
Run AI instructions to create and modify your Sanity documents.
Experimental feature
This article describes an experimental Sanity feature. The APIs described are subject to change and the documentation may not be completely accurate.
Generate lets you programmatically run schema-aware AI instructions to create and modify Sanity documents. You can run instructions from anywhere you can execute code, such as cloud functions, webhook listeners, CI/CD pipelines, migration scripts, and more.
- Generate draft documents with new content.
- Generate images based on fields within your document.
- Use your existing content to inform new documents.
Requirements
See the Agent Actions introduction for a full list of requirements and limitations.
Core Concepts
The instruction workflow
Generate uses the concept of an instruction to tell large language models (LLMs) about your desired action and the content you want to interact with. This is similar to the prompting you see in other chat or interactive AI tools.
Much like with the Studio AI Assist plugin, you want to:
- Identify the task you want AI to perform.
- Identify the document or types of documents you want to target with the instruction.
- Identify what kind of background information, or context, you should supply to the instruction.
From there, you have options for how to initiate the instructions. It will sometimes make sense to run them when documents change. In others, you may want to combine document changes into a new Content Release for human review.
Multi-stage instructions
A single instruction is often fine for smaller tasks like updating an individual field. However, splitting instructions into multiple steps or stages for more significant tasks like writing complete documents with complex schemas returns better results.
One approach to improve the LLM success rate is to approach instructions more like a human would. For example:
- Make a skeleton or outline by populating simple, foundational fields like title, description, and categories or topics.
- Run instructions for more complex areas, like an article's main content field, individually by passing in the results of step 1 as field parameters.
- Run any summarization tasks at the end for content like SEO fields, social copy, or connecting related content.
You can see an example of this strategy in the Generate cheat sheet.
Limitations
The following field types are not supported:
The following field types are supported, but with limitations:
- Slug: Generate does not perform uniqueness validation to check if the slug conflicts with others.
- URL: Generate only writes to this field type if the instruction includes links.
The following types require additional setup:
- Image (See the configuration guide)
- Reference (See the configuration guide. Requires the Embeddings API)
- Date and Datetime: To enable these fields, use the
localeSettings
in thegenerate
call. Learn more in the configuration guide.
The API can only overwrite non-array fields and can only append to array fields, including Block/Portable Text fields.
Was this page helpful?