Create your own Sanity template
Learn how to create a reusable Sanity project template
Go to Create your own Sanity templateSanity AI Assist puts the power of Large Language Models (LLM) right at your fingertips in the studio, where your content lives. Write reusable instructions in natural human language to a document-aware AI assistant that can handle chores and repetitive tasks while you focus on the creative stuff.
In this guide, we will get familiar with how AI Assist integrates into Sanity Studio and explore some example instructions to give you all you need to put these new capabilities to work in your own projects.
This article is about a feature that is currently available on all paid plans from Teams and up.
We all spend an astonishing amount of time on stuff that isn’t, in fact, about content creation or refinement but rather the type of repetitive chores and menial drudgery that seem to pop into existence in the orbit of any creative task.
Write a one-paragraph summary of your article for the front page, then cut it down further to fit in a tweet. Come up with three alternate titles for A/B testing. Pick the right category and tags for the algorithms. Extract all the quotes for fact-checking. All the little stuff that can’t easily be automated but feels like it should be.
Sanity AI Assist is here to take some of that dreary busywork off your hands, leaving you to spend your time and energy tackling more interesting and stimulating problems. If you want to get right into the inner workings and see some sample instructions, jump to the section titled The rules of AI Assist, or continue reading for some examples of usage and a step-by-step tour of the feature with a practical example.
Before we get into the grand tour of the AI Assist feature, you might want to see some examples of it in action. Let’s start with a simple but powerful example of giving the AI some input and asking it to iterate on it to create variant titles for an article. We’ll be re-creating this example in the section next section – Touring the AI Inspector with a practical example.
The simplest tricks are often the best. Give the assistant some input and tell it to riff on it. Massage your input and rinse and repeat for a fresh take. Sanity AI Assist takes it one step further by integrating intimate knowledge of your document structure and ability to format its conjurations accordingly.
Tasks that no human gets excited about – like taking an unstructured blob of input and fastidiously sorting, categorizing, and entering each element into the appropriate custom block component – are what AI Assist thrives on.
The dirty secret of Large Language Models is that their so-called ‘intelligence’ is really just being very good at recognizing and imitating structure in language. We can put that to our advantage by feeding it raw input and asking it to find and extract certain types of content. For example, check out how Sanity co-founder Simen Svale created a new content type, “facts,” and used Sanity AI Assist to extract the facts from an article and drop them into the “facts” field, ready for verification of truthiness.
In this section, we’ll get familiar with Sanity AI Assist and how it fits in your studio workflow by re-creating and then expanding upon a basic instruction to generate a list of alternative titles for an article.
Our example document has a very minimalist schema with a Title string
field and an array
of string
s for Alternate titles. You can reproduce this example with any document that has a title and a field that has some way to output a list, such as Portable Text or even plain text.
If you prefer action to reading, look for the pointing finger emoji 👉 to quickly locate the next actionable item!
👉 From any open document in the editor pane, open the AI Inspector by clicking the sparkle icon ✨ in the document’s top right context menu. A new panel on the right side of the document pane should open.
If you are using AI Assist for the very first time, you may be prompted to enable the feature by creating a token. This step must be completed once by a user that has token creation privileges, typically an admin or developer type of role.
The AI Inspector will open with the entire document as its “target” because it was opened from the root-level sparkle button in the top right corner of the editor. To target a specific field, hover your cursor over the field to reveal its very own dedicated sparkle button. Then click it to switch the context to that field.
👉 Click the sparkle ✨ button to focus the AI Inspector on the Alternate Titles field.
Once you have the AI Inspector panel focused on the Alternate Titles field, you are ready to create your first instruction. Let’s start by clicking the button labeled Add item, and an empty instruction will open in the editor. You can also decide whether to share your work with other users of this studio or keep it private for now.
👉 Create a new instruction by clicking Add item. Give your instruction an appropriate and informative name, e.g., Create variants, and then move on to the Instruction body.
Instructions for AI Assist are written in a normal, human-readable style. These instructions can be enhanced by adding references to key values from the current document, which the assistant can access in real-time. You can also prompt the user for input or refer to reusable contextual documents – such as a style guide or a description of your target audience – to further inform the assistant about the expected output. We will look closer at each of these options in due course, but for now, let’s go with the first option and select Document field.
👉 In plain English, instruct the assistant to generate some alternate titles, then tell it to base its suggestion on the current value of the Title field by clicking the + in the upper left corner of the Instructions textarea and selecting Document field. In the popup that appears, search for Title to find and select the appropriate field from your schema
The instruction is saved automatically whenever you make changes and is now ready to run. You can close the AI Inspector and invoke the instruction from the sparkle ✨ button on the Alternate titles field. Alternatively, you can keep the sidebar open and run the instruction from there by clicking the Run instruction button at the bottom of the AI Inspector interface. Just make sure you’ve actually entered a Title for it to base its mad libs on!
👉 Run the instruction and let the assistant do its thing. You’ll see the results unfold in real-time.
The assistant will notice that you are targeting an array and format its output accordingly. You could also have it output its result as a bulleted list in a Portable Text field or in a plain text format like markdown. You can experiment with how you phrase your instruction to generate a different flavor of clickbaity titles – something we’ll explore further in the next section!
Let’s explore the other possibilities presented by that unassuming + in the top left corner of the Instructions input area. You could keep editing your current instruction but let’s instead preserve our work and create a copy to keep working on by navigating back to the list of available instructions for the Alternate Titles field and choosing the Duplicate option in the contextual menu hidden under the three vertical dots on the Create variants instruction listing.
The AI Inspector should open your new copy in the editor automatically. Make sure to rename it to something appropriate, like Create targeted variants.
👉 Edit your instruction so it looks like the leftmost figure. Then click the + button and select User input. Fill in the details to your liking, as in the middle figure. Then, finally, close the popover dialog so your instruction looks like the rightmost figure.
👉 Start up the assistant by clicking Run instruction. You should see a dialog prompting you for some target audience groups.
👉 Enter a comma-separated list of target audiences, such as Boomers, GenX, Millennials, Zoomers
. Then submit the dialog and wait for the assistant to complete the instruction. Your result should resemble the screenshot below.
It’s worth mentioning at this point that even though we are sticking to outputting strings into an array for the sake of brevity and accessibility in this section, the assistant would happily populate a more complex field, like an array of object
s with fields for title
, target
, and emoji
.
Let’s take a final detour to get familiar with Contexts. The final input type in the + menu triumvirate requires you to do some prep work first, but it is quite the power feature once you get going! Contexts are reusable bodies of text you can use to prime your assistant with additional … well, context. A company style guide would be a good candidate for a context, as would any large-ish amount of textual background information you would like your assistant to keep in mind.
👉 Navigate to the leftmost pane in your studio titled Content. You should see a new entry in your list of document types titled AI Context. Select this new option, and click the Create new button in the top area of the new pane. (It’s the one that looks like a little pencil writing in a square box.)
👉 Give your new context a name, and start entering some background info you’d like to include in your instructions. Or, just copy and paste the example below to fast-track the tutorial. Your context is saved automatically, so there’s no need to click a publish button. Once you’re happy, move on to the next step.
You are a copywriting assistant whose specialty is coming up with copy to entice the readers to click your story. You work for the most popular news outlet in the Galactic Empire around the time of the events of the movie Star Wars: The force awakens take place. All your historic references will be checked and approved by Empire fact-checkers to make sure the Empire always looks good and heroic, and any rebel activity is portrayed as heinous terrorism.
👉 Navigate back to your article
document, or create a new one. Then open the AI Inspector and focus the pane on the Alternate titles field once again. Make another copy of the original Create variants-instruction as earlier, and name your new instruction Create contextual variants.
👉 Edit your instruction to look like the screenshot above. Then click + to open the insert menu, and this time choose AI Context. Start typing the name you gave your context in the popover dialog that should appear, and select your context. Refer to the figures below if needed.
👉 Once more, make sure your document has a Title, then run your instruction either by clicking Run instruction in the AI Inspector or by finding it in the AI contextual menu (the sparkle ✨ button) for the Alternate titles field.
It’s no protocol droid, but it’s still pretty neat! Let’s wrap up this tour of the AI Assist interface with a list of some main takeaways:
AI Assist can be disabled for any field through configuration. By default, AI Assist is enabled for most object
, array
, and string
field types. Your assistant can write to all compatible fields that it detects. Additionally, it can create array items, including Portable Text blocks.
Number fields are not supported and will not be changed by the assistant.
Large Language Models (LLM) are still new tech! The constraints and limitations are still being explored, but some common caveats to the field in general that you might run into using AI Assist are:
Document field
– You’ll be using this a lot. Use this to refer to fields in the same document. Note: you can refer to the current field you are writing the prompt for - you need to do this to have the assistant complete arrays and portable text using the content that’s currently there.Context
– This is a globally available text fragment that you can use across prompts. Use it to add reusable information to instruct your assistant, like style guides or prompts to write for a certain demographic or language.User input
– Ask the user for input every time the prompt runs. For example, if you frequently write articles based on interview transcripts, you could use the transcript as User Input and the style guide of your publication as Instruction Context.You can add instructions to different levels of the document to have the machine work there.
Where: body
– Portable Text or plain text field
Prompt:
Given the title
body
keep writing copy.
Where: document instruction
Prompt:
Given the following inspiration User input (What should the article be about?)
Create an article.
Where: summary
– Portable Text or plain text field
Prompt:
Summarize body
Variation (to suggest how the summary should be formatted when saving to a formatted text field):
Summarize body
. Do not use lists, headlines, or quotes.
Where: On an array field with strings
Prompt:
List the categories relevant to body
Where: On the field to shorten
Prompt:
Shorten <the field itself>
Where: On the field where the translation should be stored
Prompt:
Translate title
to norwegian.
Where: On a text field where the sentiment should be stored
Prompt:
Classify the sentiment of the following text body
Determine if it is elated, happy, neutral, sad or angry.
Where: On the title field
Prompt:
Create a catchy and engaging headline about User input: (What should the title be about)
Where: On any field or at the document level
Prompt:
Given the following User input:
User input (Paste your stuff here)
Format the User input so it aligns with the schema. Do not omit sections or paragraphs, but use formatting, headings and lists as appropriate.
Infer item types based on context.
title
, then add a relevant callout
to body
. Do not add anything to any other fields.”This version of the feature uses OpenAI.com as a third-party sub-processor. Their security posture has been vetted by Sanity's security team, and approved for use.
Sanity Composable Content Cloud is the headless CMS that gives you (and your team) a content backend to drive websites and applications with modern tooling. It offers a real-time editing environment for content creators that’s easy to configure but designed to be customized with JavaScript and React when needed. With the hosted document store, you query content freely and easily integrate with any framework or data source to distribute and enrich content.
Sanity scales from weekend projects to enterprise needs and is used by companies like Puma, AT&T, Burger King, Tata, and Figma.
Learn how to create a reusable Sanity project template
Go to Create your own Sanity templateA thorough intro to using GROQ-projections in a webhook contest
Go to GROQ-Powered Webhooks – Intro to ProjectionsA thorough intro to using GROQ-filters in a webhook-context
Go to GROQ-Powered Webhooks – Intro to Filters