examples/functions/auto-summary/README.md
Content teams often need concise summaries of long-form content (like blog posts or articles) for previews, SEO, or editorial workflows. Manually writing these summaries is time-consuming and inconsistent, especially as content volume grows.
This example demonstrates how to use a Sanity Function to automatically generate a summary for a document's content field using Sanity's Agent Actions. When a post's content is changed the function generates a summary (up to 250 words) and writes it to the autoSummary field.
This function is built to be compatible with any of the official "clean" templates. We recommend testing the function out in one of those after you have installed them locally.
If you're using the nextjs-clean template, you'll need to add a tags field to your post schema:
studio/src/schemaTypes/documents/post.tsfields array:defineField({
name: 'autoSummary',
title: 'Auto Summary',
type: 'text',
description: 'Summary will be automatically generated when you publish a post',
}),
# /studio
npx sanity schema deploy
Important: Run these commands from the root of your project (not inside the studio/ folder).
Initialize the example
Run this if you haven't initialized blueprints:
npx sanity blueprints init
You'll be prompted to select your organization and Sanity studio.
Then run:
npx sanity blueprints add function --example auto-summary
Add configuration to your blueprint
Add the following resource to your sanity.blueprint.ts:
defineDocumentFunction({
src: './functions/auto-summary',
memory: 2,
timeout: 30,
name: 'auto-summary',
event: {
on: ['create', 'update'],
filter:
"_type == 'post' && (delta::changedAny(content) || (delta::operation() == 'create' && defined(content)))",
projection: '{_id}',
},
})
Install dependencies
Install dependencies in the project root:
npm install
Make sure you have a schema deployed
From the studio folder, run:
# In the studio/ folder
npx sanity schema deploy
You can test the auto-summary function locally using the Sanity CLI before deploying it to production.
Test the function with an existing document ID from your dataset:
npx sanity functions test auto-summary --document-id <insert-document-id> --dataset production --with-user-token
Replace <insert-document-id> with an actual document ID from your dataset and production with your dataset name.
Start the development server for interactive testing:
npx sanity functions dev
noWrite: true in the functionpost document type containing:
content field (for content analysis)autoSummary field (string or text) (for storing generated summary)When a content editor creates a blog post or modifies the 'content' field, the function automatically:
Result: Content creators get consistent, relevant summaries without manual effort.
instruction string in index.ts to adjust the summary style, length, or language.filter in the blueprint resource to target different document types or conditions.noWrite: false in the function to automatically write the summary to the document in production.Error: "Error occurred during summary generation"
content field or if there are API issues.content field and check your Sanity project configuration.