0

๐ŸŽฏ Tutorial 3: Structured Output Agent

awesome-llm-apps3_structured_output_agent

Welcome to structured output! This tutorial teaches you how to create agents that return type-safe, structured data instead of plain text. This is crucial for building reliable applications that need predictable data formats.

Sign in to save downloads to your library and vote.

Preview

๐ŸŽฏ Tutorial 3: Structured Output Agent

Welcome to structured output! This tutorial teaches you how to create agents that return type-safe, structured data instead of plain text. This is crucial for building reliable applications that need predictable data formats.

๐ŸŽฏ What You'll Learn

  • Pydantic Schemas: Define data structures with validation
  • Type Safety: Ensure your agents return expected data formats
  • Business Logic: Process structured data reliably
  • Error Handling: Graceful handling of validation errors
  • Real-world Applications: Customer support and email generation

๐Ÿง  Core Concept: Structured Output

Structured output means your agent returns validated data objects instead of raw text:

  • โœ… Type Safety: Know exactly what data format you'll receive
  • โœ… Validation: Automatic checking of required fields and data types
  • โœ… Reliability: No more parsing text responses manually
  • โœ… Integration: Easy to use in applications and databases

Why Structured Output?

  • Predictable: Always get the same data structure
  • Validated: Pydantic ensures data correctness
  • Typed: Full IDE support and type checking
  • Scalable: Easy to modify and extend schemas

๐Ÿš€ Tutorial Structure

This tutorial contains two comprehensive examples:

๐Ÿ“ Example 1: Customer Support Ticket Agent

Location: ./3_1_customer_support_ticket_agent/

  • Extract structured ticket information from customer complaints
  • Priority classification and urgency assessment
  • Contact information extraction
  • Department routing logic

๐Ÿ“ Example 2: Email Generation Agent

Location: ./3_2_email_agent/

  • Generate structured email content with metadata
  • Subject line optimization
  • Recipient classification
  • Email template formatting

๐Ÿ“ Project Structure

3_structured_output_agent/
โ”œโ”€โ”€ README.md                           # This tutorial overview
โ”œโ”€โ”€ 3_1_customer_support_ticket_agent/  # Customer support example
โ””โ”€โ”€ 3_2_email_agent/                   # Email generation example

Each example directory follows the standard structure:

  • Python file: Contains the agent implementation and Streamlit app
  • README.md: Setup and usage documentation
  • requirements.txt: Dependencies list

๐ŸŽฏ Learning Objectives

By the end of this tutorial, you'll understand:

  • โœ… How to define Pydantic schemas for structured output
  • โœ… How to configure agents to return structured data
  • โœ… How to handle validation errors gracefully
  • โœ… When to use structured output vs plain text
  • โœ… Best practices for schema design

๐Ÿ’ก Key Patterns

Basic Structured Output Pattern

from pydantic import BaseModel
from google.adk.agents import Agent

class TicketInfo(BaseModel):
    title: str
    priority: str
    category: str
    urgency_level: int

agent = Agent(
    name="support_agent",
    model="gemini-3-flash-preview",
    instruction="Extract ticket information...",
    response_format=TicketInfo,  # This ensures structured output!
)

Advanced Schema with Validation

from pydantic import BaseModel, Field, validator
from typing import List, Optional

class EmailData(BaseModel):
    subject: str = Field(..., min_length=5, max_length=100)
    recipients: List[str] = Field(..., min_items=1)
    priority: str = Field(..., regex="^(low|medium|high)$")
    
    @validator('recipients')
    def validate_emails(cls, v):
        # Custom email validation logic
        return v

๐ŸŽฏ Real-World Applications

Structured output agents are perfect for:

  • Customer Support: Extracting ticket information from complaints
  • Data Processing: Converting unstructured text to database records
  • Content Generation: Creating structured content with metadata
  • Form Processing: Extracting information from documents
  • API Integration: Providing consistent data formats for other systems

๐Ÿ’ก Pro Tips

  • Clear Schemas: Use descriptive field names and add docstrings
  • Validation: Add appropriate validators for your use case
  • Optional Fields: Use Optional for fields that might be missing
  • Examples: Provide example data in your schema documentation
  • Error Handling: Always handle validation errors gracefully

๐Ÿšจ Important Notes

  • Pydantic Required: You need Pydantic for schema definitions
  • Model Support: Not all models support structured output equally well
  • Validation Overhead: Complex schemas may slow down responses
  • Schema Evolution: Plan for schema changes in production systems

Ingestion metadata

Source catalog
awesome-llm-apps
Repository
Shubhamsaboo/awesome-llm-apps ยท main
File path
ai_agent_framework_crash_course/google_adk_crash_course/3_structured_output_agent/README.md
Last refreshed
7/24/2026, 3:00:13 AM (52m ago)
Refresh schedule
Daily ยท 03:00 UTC
Dedupe status
Unique ยท deduped by (source, url)