Skip to main content

Generate Features

The Generate operator uses AI to transform and analyze web content according to your instructions. Unlike Extract which pulls data as-is, Generate creates new insights, summaries, and transformations of the original content.

Overview

The generate operator is accessed through your TABStack client instance:

import { TABStack } from '@tabstack/sdk';

const tabs = new TABStack({
apiKey: process.env.TABSTACK_API_KEY!
});

// Access generate methods
tabs.generate.json(url, schema, instructions, options);

When to Use Generate vs Extract

Extract pulls data as it appears on the page:

// Extract: Get the exact price shown on the page
const result = await tabs.extract.json(url, priceSchema);
// Returns: { price: 99.99 }

Generate transforms and analyzes content with AI:

// Generate: Analyze pricing and create insights
const result = await tabs.generate.json(
url,
analysisSchema,
'Analyze the pricing strategy and categorize it as budget, mid-range, or premium'
);
// Returns: { price: 99.99, category: "mid-range", analysis: "..." }

Generate JSON

Transform web content into structured data using AI with custom instructions.

Basic Usage

const schema = {
type: 'object',
properties: {
summaries: {
type: 'array',
items: {
type: 'object',
properties: {
title: { type: 'string' },
category: { type: 'string' },
summary: { type: 'string' }
},
required: ['title', 'category', 'summary']
}
}
},
required: ['summaries']
};

const result = await tabs.generate.json(
'https://news.ycombinator.com',
schema,
'For each story, categorize it (tech, business, science, etc.) and write a one-sentence summary'
);

console.log(result.data.summaries);

Example Output:

{
"summaries": [
{
"title": "New AI Model Released",
"category": "tech",
"summary": "A breakthrough language model with improved reasoning capabilities was announced today."
},
{
"title": "Database Performance Tips",
"category": "tech",
"summary": "Key strategies for optimizing database queries and indexing in production systems."
}
]
}

Real-World Examples

Example 1: Sentiment Analysis

Analyze the sentiment and tone of web content:

interface SentimentAnalysis {
overallSentiment: 'positive' | 'negative' | 'neutral';
confidence: number;
keyPhrases: string[];
emotionalTone: string;
mainTopics: string[];
}

const schema = {
type: 'object',
properties: {
overallSentiment: {
type: 'string',
enum: ['positive', 'negative', 'neutral']
},
confidence: {
type: 'number',
description: 'Confidence score from 0 to 1'
},
keyPhrases: {
type: 'array',
items: { type: 'string' },
description: 'Important phrases that indicate sentiment'
},
emotionalTone: {
type: 'string',
description: 'Description of the emotional tone'
},
mainTopics: {
type: 'array',
items: { type: 'string' }
}
},
required: ['overallSentiment', 'confidence']
};

const result = await tabs.generate.json<SentimentAnalysis>(
'https://reviews.example.com/product/123',
schema,
'Analyze the overall sentiment of the product reviews, extract key phrases, and identify main topics discussed'
);

console.log(`Sentiment: ${result.data.overallSentiment} (${result.data.confidence * 100}% confident)`);
console.log(`Tone: ${result.data.emotionalTone}`);
console.log(`Key phrases: ${result.data.keyPhrases.join(', ')}`);
console.log(`Topics: ${result.data.mainTopics.join(', ')}`);

Example 2: Content Categorization and Tagging

Automatically categorize and tag articles:

interface CategorizedArticle {
title: string;
primaryCategory: string;
secondaryCategories: string[];
tags: string[];
targetAudience: string;
readingLevel: 'beginner' | 'intermediate' | 'advanced';
estimatedReadingTime: number;
}

const schema = {
type: 'object',
properties: {
title: { type: 'string' },
primaryCategory: {
type: 'string',
description: 'Main category (e.g., Technology, Business, Health)'
},
secondaryCategories: {
type: 'array',
items: { type: 'string' }
},
tags: {
type: 'array',
items: { type: 'string' },
description: 'Relevant keywords and topics'
},
targetAudience: {
type: 'string',
description: 'Description of intended audience'
},
readingLevel: {
type: 'string',
enum: ['beginner', 'intermediate', 'advanced']
},
estimatedReadingTime: {
type: 'number',
description: 'Reading time in minutes'
}
},
required: ['title', 'primaryCategory', 'tags', 'readingLevel']
};

const result = await tabs.generate.json<CategorizedArticle>(
'https://blog.example.com/article',
schema,
'Categorize this article, identify relevant tags, determine the target audience, assess reading difficulty, and estimate reading time'
);

console.log(`Title: ${result.data.title}`);
console.log(`Category: ${result.data.primaryCategory}`);
console.log(`Level: ${result.data.readingLevel}`);
console.log(`Reading Time: ${result.data.estimatedReadingTime} minutes`);
console.log(`Tags: ${result.data.tags.join(', ')}`);

Example 3: Extract Key Insights

Pull out the most important information from long-form content:

interface ArticleInsights {
mainThesis: string;
keyPoints: string[];
conclusions: string[];
actionItems: string[];
relevantQuotes: Array<{
quote: string;
context: string;
}>;
}

const schema = {
type: 'object',
properties: {
mainThesis: {
type: 'string',
description: 'The primary argument or purpose of the article'
},
keyPoints: {
type: 'array',
items: { type: 'string' },
description: 'Main points supporting the thesis'
},
conclusions: {
type: 'array',
items: { type: 'string' },
description: 'Key conclusions or takeaways'
},
actionItems: {
type: 'array',
items: { type: 'string' },
description: 'Actionable recommendations'
},
relevantQuotes: {
type: 'array',
items: {
type: 'object',
properties: {
quote: { type: 'string' },
context: { type: 'string' }
}
}
}
},
required: ['mainThesis', 'keyPoints', 'conclusions']
};

const result = await tabs.generate.json<ArticleInsights>(
'https://research.example.com/paper',
schema,
'Extract the main thesis, key supporting points, conclusions, and actionable recommendations. Include 2-3 relevant quotes with context.'
);

console.log('Main Thesis:');
console.log(result.data.mainThesis);
console.log('\nKey Points:');
result.data.keyPoints.forEach((point, i) => {
console.log(`${i + 1}. ${point}`);
});
console.log('\nAction Items:');
result.data.actionItems.forEach((item, i) => {
console.log(`${i + 1}. ${item}`);
});

Example 4: Comparative Analysis

Compare multiple items and generate insights:

interface ProductComparison {
products: Array<{
name: string;
price: number;
pros: string[];
cons: string[];
bestFor: string;
}>;
recommendation: string;
valueLeader: string;
premiumChoice: string;
}

const schema = {
type: 'object',
properties: {
products: {
type: 'array',
items: {
type: 'object',
properties: {
name: { type: 'string' },
price: { type: 'number' },
pros: { type: 'array', items: { type: 'string' } },
cons: { type: 'array', items: { type: 'string' } },
bestFor: {
type: 'string',
description: 'Type of user or use case'
}
}
}
},
recommendation: {
type: 'string',
description: 'Overall recommendation based on comparison'
},
valueLeader: { type: 'string' },
premiumChoice: { type: 'string' }
},
required: ['products', 'recommendation']
};

const result = await tabs.generate.json<ProductComparison>(
'https://reviews.example.com/laptop-comparison',
schema,
'Compare the laptops, list pros and cons for each, identify who each is best for, and provide an overall recommendation. Identify the value leader and premium choice.'
);

console.log('Comparison Summary:');
console.log(result.data.recommendation);
console.log(`\nBest Value: ${result.data.valueLeader}`);
console.log(`Premium Choice: ${result.data.premiumChoice}`);

result.data.products.forEach(product => {
console.log(`\n${product.name} - $${product.price}`);
console.log(`Best for: ${product.bestFor}`);
console.log(`Pros: ${product.pros.join(', ')}`);
console.log(`Cons: ${product.cons.join(', ')}`);
});

Writing Effective Instructions

The quality of your results depends heavily on your instructions. Here are best practices:

Be Specific and Clear

// ❌ Vague
'Analyze this content'

// ✅ Specific
'Analyze the sentiment of customer reviews, categorize each as positive/negative/neutral, and extract common themes'

Define Expected Output

// ❌ Unclear output
'Summarize the articles'

// ✅ Clear expectations
'For each article, write a 2-sentence summary focusing on the main finding and its practical implications'

Include Context

// ❌ No context
'Extract information'

// ✅ With context
'You are analyzing product reviews for a purchasing decision. Extract the most mentioned pros and cons, and identify any dealbreaker issues.'

Use Examples When Helpful

const instructions = `
Categorize each product review as positive, negative, or mixed.
Examples:
- "Great product, works perfectly!" → positive
- "Terrible quality, broke after one week" → negative
- "Good features but poor battery life" → mixed
`;

Options Reference

GenerateJsonOptions

OptionTypeDefaultDescription
nocachebooleanfalseBypass cache and force fresh generation

Combining Generate with Extract

You can combine Extract and Generate for powerful workflows:

// Step 1: Extract raw data
const extractSchema = {
type: 'object',
properties: {
products: {
type: 'array',
items: {
type: 'object',
properties: {
name: { type: 'string' },
price: { type: 'number' },
description: { type: 'string' }
}
}
}
}
};

const extracted = await tabs.extract.json(url, extractSchema);

// Step 2: Generate insights from extracted data
const generateSchema = {
type: 'object',
properties: {
analysis: {
type: 'object',
properties: {
priceRange: { type: 'string' },
averagePrice: { type: 'number' },
productCategories: { type: 'array', items: { type: 'string' } },
recommendations: { type: 'array', items: { type: 'string' } }
}
}
}
};

const generated = await tabs.generate.json(
url,
generateSchema,
'Analyze the product lineup, determine price ranges, categorize products, and provide purchasing recommendations'
);

console.log('Product Analysis:', generated.data.analysis);

Best Practices

1. Use Descriptive Schema Properties

Include description fields to guide the AI:

const schema = {
type: 'object',
properties: {
sentiment: {
type: 'string',
enum: ['positive', 'negative', 'neutral'],
description: 'Overall sentiment based on tone and language used'
},
confidence: {
type: 'number',
description: 'Confidence level from 0 to 1, where 1 is very confident'
}
}
};

2. Be Mindful of Costs

Generate operations use AI and are more expensive than Extract. Use them when you need analysis, not just data extraction.

// ✅ Good: Use Extract for straightforward data
const prices = await tabs.extract.json(url, priceSchema);

// ❌ Wasteful: Using Generate when Extract would work
const prices = await tabs.generate.json(url, priceSchema, 'Get the prices');

3. Test Instructions Iteratively

Start simple and refine based on results:

// Iteration 1: Basic
'Summarize the article'

// Iteration 2: More specific
'Summarize the article in 3 sentences, focusing on key findings'

// Iteration 3: Optimized
'Summarize the article in 3 sentences. Focus on: 1) main finding, 2) methodology, 3) practical implications for developers'

4. Handle Variable Content

Account for pages that might not have all expected content:

const schema = {
type: 'object',
properties: {
sentiment: { type: 'string' },
keyPoints: {
type: 'array',
items: { type: 'string' },
description: 'Main points if present; empty array if none found'
}
}
};

Next Steps