Sanity Library Reference Docs
    Preparing search index...
    • Alpha

      Generates content for a document (or specific fields) via Sanity Agent Actions.

      Parameters

      Returns (options: GenerateInstruction) => Subscribable<unknown>

      A stable callback that triggers the action and yields a Subscribable stream.

      This hook provides a stable callback to trigger AI-powered content generation for documents.

      Features:

      • Uses instruction templates with $variables and supports instructionParams (constants, fields, documents, GROQ queries).
      • Can target specific paths/fields; supports image generation when targeting image fields.
      • Supports optional temperature, async, noWrite, and conditionalPaths.
      • Returns a Subscribable stream for tracking generation progress.
      import {useAgentGenerate} from '@sanity/sdk-react'

      function GenerateDescription({documentId}: {documentId: string}) {
      const generate = useAgentGenerate()

      const handleGenerate = () => {
      generate({
      documentId,
      instruction: 'Write a compelling product description based on the title: $title',
      instructionParams: {
      title: {type: 'field', path: 'title'},
      },
      targetPaths: ['description'],
      }).subscribe({
      next: (result) => console.log('Generation progress:', result),
      complete: () => console.log('Generation complete'),
      error: (err) => console.error('Generation failed:', err),
      })
      }

      return <button onClick={handleGenerate}>Generate Description</button>
      }
      import {useAgentGenerate} from '@sanity/sdk-react'

      function GenerateProductImage({documentId}: {documentId: string}) {
      const generate = useAgentGenerate()

      const handleGenerateImage = () => {
      generate({
      documentId,
      instruction: 'Generate a product photo for: $productName',
      instructionParams: {
      productName: {type: 'field', path: 'name'},
      },
      targetPaths: ['mainImage'],
      }).subscribe({
      complete: () => console.log('Image generated'),
      })
      }

      return <button onClick={handleGenerateImage}>Generate Image</button>
      }