examples/functions/sentiment-analysis/README.md
Content teams manually review thousands of user-generated content pieces (reviews, comments, feedback) to identify sentiment, leading to delayed responses to negative feedback, missed opportunities to amplify positive content, and inconsistent content prioritization across teams.
This Sanity Function automatically analyzes sentiment in user-generated content using AI, categorizing it into 5 levels (very_positive, positive, neutral, negative, very_negative) to enable automated workflows and content prioritization.
This function is not directly compatible with our starter templates, but starter templates or any project can be easily modified to work. Common use cases include:
review|feedback|comment field with portable text (for content analysis)sentiment field (for storing analyzed sentiment)schemaId. Captured by deploying your schema or studio and then running sanity schema list. (See following step for details)First you'll need a content type in which to add sentiment to. Common types are 'review', 'comment', and 'feedback'. You'll need to add a sentiment field to your chosen content type schema:
studio/src/schemaTypes/documents/review|comment|feedback.ts)fields array:defineField({
name: 'sentiment',
title: 'Sentiment',
type: 'string',
options: {
list: [
{ title: 'Very Positive', value: 'very_positive' },
{ title: 'Positive', value: 'positive' },
{ title: 'Neutral', value: 'neutral' },
{ title: 'Negative', value: 'negative' },
{ title: 'Very Negative', value: 'very_negative' }
]
},
description: 'Sentiment will be automatically analyzed when you publish content',
}),
# /studio
npx sanity schema deploy
# View your schemaId with
npx sanity schema list
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 sentiment-analysis
Add configuration to your blueprint
// sanity.blueprint.ts
import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
export default defineBlueprint({
resources: [
defineDocumentFunction({
name: 'sentiment-analysis',
src: './functions/sentiment-analysis',
memory: 2,
timeout: 30,
event: {
on: ['publish'],
filter: "_type in ['review', 'comment', 'feedback'] && !defined(sentiment)",
projection: '{_id}',
},
}),
],
})
Install dependencies
Install dependencies in the project root:
npm install
And install function dependencies:
npm install @sanity/functions
cd functions/sentiment-analysis
npm install
cd ../..
Make sure you have a schema deployed
From the studio folder, run:
# In the studio/ folder
npx sanity schema deploy
You can test the sentiment-analysis 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 sentiment-analysis --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
npx sanity documents query to find suitable test documentsnpx sanity functions logs sentiment-analysis --watchWhen a user submits new feedback, review, or comment without sentiment analysis, the function automatically:
Result: Content teams get instant sentiment analysis without manual effort.
instruction string in index.ts to adjust the sentiment categories or analysis criteria.filter in the blueprint resource to target different document types or conditions.Once you've tested your function locally and are satisfied with its behavior, you can deploy it to production.
Important: Make sure you have the Deploy Studio permission for your Sanity project before attempting to deploy.
Make sure your sanity.blueprint.ts file is properly configured with your function:
// sanity.blueprint.ts
import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
export default defineBlueprint({
resources: [
defineDocumentFunction({
name: 'sentiment-analysis',
src: './functions/sentiment-analysis',
memory: 2,
timeout: 30,
event: {
on: ['publish'],
filter: "_type in ['review', 'comment', 'feedback'] && !defined(sentiment)",
projection: '{_id}',
},
}),
],
})
From your project root, run:
npx sanity blueprints deploy
This command will:
Important: The function automatically handles local vs production context - no manual configuration needed.
After deployment, you can verify your function is active by:
sentiment field is populatedsentiment field exists in your schemaError: "Deploy Studio permission required"
Error: "Blueprint validation failed"
sanity.blueprint.ts configurationFunction not analyzing sentiment after deployment
Error: "AI Assistant feature not enabled"
For more details, see the official function deployment documentation.
Error: "AI Assistant feature not enabled"
Error: "Field 'sentiment' not found"
Error: "Document not found"
Error: "Error occurred during sentiment analysis"
review|feedback|comment field or if there are API issues.review|feedback|comment field and check your Sanity project configuration.