Logo
    Login
    Hackerspace
    • Learn
    • Colleges
    • Hackers
    Career
    • Jobs
    • Applications
    Profile
    • Login as Hacker
    Vercel Fanboys College

    Builders Guide to the AI SDK

    0 / 16 chapters0%
    Course Introduction
    Fundamentals
    Introduction to LLMs
    Prompting Fundamentals
    AI SDK Dev Setup
    Data Extraction
    Model Types and Performance
    Invisible AI
    Introduction to Invisible AI
    Text Classification
    Automatic Summarization
    Structured Data Extraction
    UI with v0
    Conversational AI
    Basic Chatbot
    AI Elements
    System Prompts
    Tool Use
    Multi-Step & Generative UI
    Conclusion
    1. Builders Guide to the AI SDK
    2. Structured Data Extraction

    Structured Data Extraction

    You've used AI to classify and summarize text. Now, get even more precise with Structured Extraction. This pulls specific pieces of information from unstructured text and places them exactly where needed in your app.

    Use generateObject with a detailed Zod schema to extract appointment details from natural language input and display them using a v0-prototyped UI.

    Project Setup

    Continuing with the same codebase from AI SDK Dev Setup. For this section, you'll find the classification example files in the app/(4-extraction)/ directory.

    The Problem: Turning Natural Language into Data

    Imagine typing "Lunch with Sarah next Tuesday at noon at Cafe Central" and having it automatically create a perfect calendar event with all the details filled in. Apps like Fantastical pioneered this kind of natural language processing - it seems like magic, but that's structured extraction in action!

    Natural into data diagram

    Manually parsing that input is a nightmare. Regex breaks easily, and complex parsing logic is brittle. But for an LLM? It's a natural fit. Models continue to improve giving better results at a lower cost.

    Setup: The Appointment Extractor App

    Let's get your environment ready.

    1. Run the Dev Server: Make sure it's running (pnpm run dev).
    2. Open the Page: Navigate to http://localhost:3000/extraction.

    You'll see a simple UI: an input field to type appointment details and an empty calendar card below it (this is our CalendarAppointment component, built with Vercel v0. We'll explore this AI-powered UI generator in the next lesson).

    Screenshot of the '/extraction' page showing the input field and the empty 'CalendarAppointment' card.

    Step 1: The Extraction Action (actions.ts)

    Like before, you will use a Server Action to handle the AI call.

    1. Create schemas.ts: In app/(4-extraction)/extraction/, create the file schemas.ts.
    2. Start with this basic structure:
    typescript
    1. Now implement the schema:
    typescript

    Why nullable() instead of optional()?

    In our experience, explicitly requiring a field but allowing null (z.string().nullable()) often yields more reliable results from LLMs than making the field entirely optional (z.string().optional()). It forces the model to consider the field and consciously decide if the information is present or not.

    1. Create actions.ts: In app/(4-extraction)/extraction/, create the file actions.ts.
    2. Start with the basic setup:
    typescript
    1. Now implement the extraction:
    typescript

    Step 2: Connecting the Frontend (page.tsx)

    No you'll make the form work.

    1. Open app/(4-extraction)/extraction/page.tsx. The basic UI is already set up.

    2. Add the necessary imports and state at the top of the file (after the existing imports):

    typescript
    1. Replace the handleSubmit function with the actual implementation:
    typescript
    1. Pass the appointment data to the CalendarAppointment component:

    Find the line with <CalendarAppointment appointment={null} /> and replace it with:

    typescript

    The complete page.tsx file should look like this:

    typescript

    Step 3: Run and Observe (Initial Extraction)

    Let's test it! Go to http://localhost:3000/extraction.

    Enter: Meeting with Guillermo Rauch about Next Conf Keynote Practice tomorrow at 2pm at Vercel HQ

    Click "Extract Appointment".

    Screenshot of the '/extraction' page showing the initial extraction results

    The initial results might be okay, but not perfect (e.g., title includes names, date is wrong, time format is basic).

    Step 4: Refining with .describe() - The Key!

    The initial results might work, but they could be imperfect (e.g., title includes names, date might be wrong, time format is basic).

    Let's improve our extraction using .describe() in our Zod schema. Update your schemas.ts:

    typescript

    Key refinements:

    • Title: Clear instructions to exclude names and be concise
    • Time: Specific format requirements (24-hour HH:MM)
    • Date: Provides today's date for correct relative date calculation
    • Attendees: Instructions on extracting full names

    Save schemas.ts, refresh the browser, and test again with the same input. The extraction should now be much more accurate!

    Screenshot of the '/extraction' page showing the refined extraction results

    💡 Handling Relative Dates and Time Formats

    Struggling with date parsing or time format inconsistencies? Try asking an AI assistant:

    text

    Key Things to Consider

    • Structured Extraction pulls specific data points from unstructured text into a defined format.
    • generateObject + Zod Schema is the ideal tool combination.
    • Use nullable() for potentially missing fields.
    • .describe() is essential for specifying formats, providing context (like today's date), and defining default logic.
    • Sharing Zod schemas between backend (actions) and frontend provides end-to-end type safety.

    Next Step: Supercharge UI with Vercel v0

    You've seen how structured data unlocks practical features like calendar extraction and form filling. Now, take a quick (optional) detour to explore Vercel v0, the tool that was used to prototype the CalendarAppointment UI in this example. You'll get hands-on experience generating UI components directly from prompts, accelerating your frontend development for AI features.

    Ready to move on?

    Mark this chapter as finished to continue

    Ready to move on?

    Mark this chapter as finished to continue

    LoginLogin to mark
    Chapter completed!
    NextGo to Next Chapter

    © 2025 Hacklab

    • Privacy
    • Terms