AI Structured Data
Learn how to use Vercel AI SDK for structured data generation in Tuturuuu.
Prerequisite: You should have followed the Development and Local Supabase Development setup guides.
Overview
Tuturuuu leverages the Vercel AI SDK to generate structured data from large language models (LLMs). This approach enables type-safe AI responses, improved reliability, and consistent data structures for features like flashcards, quizzes, and learning plans.
This guide covers how to use AI structured data generation in the Tuturuuu development workflow.
Key Concepts
What is Structured Data Generation?
While text generation can be useful, many applications require generating structured data. For example, you might want to:
- Extract specific information from text
- Generate quizzes or flashcards from learning material
- Create complex objects like learning plans or task lists
- Ensure AI responses follow a consistent format
The AI SDK standardizes structured object generation across model providers with the generateObject
and streamObject
functions. You can use Zod schemas to specify the shape of the data that you want, and the AI model will generate data that conforms to that structure.
Architecture in Tuturuuu
Tuturuuu’s AI features follow this high-level architecture:
- Frontend UI - React components that display and interact with AI-generated content
- API Routes - Next.js routes that handle AI requests and responses
- AI SDK - Vercel AI SDK that manages model providers and generates structured data
- Supabase - Backend database for authentication, authorization, and storing AI-generated content
Schema Definitions
Schemas define the structure of the data that will be generated by the AI models. In Tuturuuu, these are defined in packages/ai/src/object/types.ts
using Zod.
Here are some examples of schemas used in Tuturuuu:
Flashcard Schema
Quiz Schema
Year Plan Schema
Creating an API Endpoint
To create an API endpoint that generates structured data, follow these steps:
1. Create a new route file
Create a new route file in the appropriate Next.js app, for example:
2. Implement authentication and validation
Use Supabase to authenticate the user and validate their permissions:
3. Generate structured data
Use the AI SDK to generate structured data based on the schema:
Supported Models
Tuturuuu supports multiple AI models through the Vercel AI SDK. The available models are defined in packages/ai/src/models.ts
:
To use a different model in your endpoint, simply change the model reference:
Calling from the Frontend
To call your AI endpoint from the frontend, you can use the appropriate hooks or fetch API:
Integration with Supabase
Tuturuuu’s AI features are tightly integrated with Supabase for several purposes:
TypeScript Types
Supabase-generated TypeScript types are available at packages/types/src/supabase.ts
. These types are automatically generated when you run pnpm sb:typegen
or pnpm sb:reset
and are accessible to all apps that have the @tuturuuu/types
package installed.
You can use these types to ensure type safety when working with Supabase data in your AI features:
Short-hand Type Access
For more convenient access to common table types in your AI features, Tuturuuu also provides short-hand type definitions in packages/types/src/db.ts
. These are easier to use and remember than the full database type paths:
The short-hand types can also include extended client-side properties that aren’t in the database schema, making them perfect for your AI feature implementations.
This ensures that your AI features correctly interact with the database schema, reducing runtime errors and improving development experience.
Authentication and Authorization
Before making AI requests, ensure the user is authenticated and authorized to use the feature:
Feature Flags
Use the workspace_secrets
table to enable or disable AI features for specific workspaces:
Storing Results
You can store AI-generated content in Supabase for future use:
Best Practices
Schema Design
When designing schemas for AI-generated content:
- Be specific - Use the
.describe()
method to provide clear instructions to the AI model - Keep it simple - Break complex schemas into smaller, nested objects
- Add validations - Use Zod’s validation methods (
.min()
,.max()
,.regex()
, etc.) - Use enums - For fields with a fixed set of values, use
.enum()
Example of a well-designed schema:
Error Handling
Implement robust error handling for AI-generated content:
Response Processing
For complex AI-generated content, you may need to post-process the response:
Local Development and Testing
Setting Up API Keys
To test AI features locally, you need to set up the appropriate API keys in your environment:
- Create a
.env.local
file in the root of your Next.js app - Add the necessary API keys:
- Restart your development server
Testing AI Endpoints
You can test your AI endpoints using tools like Postman or simple cURL commands:
Troubleshooting
Common Issues
- API Key Issues: Ensure your API keys are correctly set in your environment
- Model Unavailability: Some models may be unavailable in certain regions
- Token Limits: Large prompts may exceed token limits
- Schema Validation Errors: The AI might generate content that doesn’t match your schema
Debugging Tips
- Log the prompt: Print the full prompt being sent to the AI model
- Start with simple schemas: Begin with simple schemas and gradually increase complexity
- Check response format: Verify the raw response from the AI model before schema validation
Further Resources
Was this page helpful?