Lab 4 Challenge: Structured Haiku & Sentiment Analysis
Goal
Your task is to evolve your "Haiku Poet" agent into a "Structured Analyzer". Instead of returning plain text, the agent must now analyze the user's message and return a JSON object containing both a haiku and a sentiment analysis of the input.
Requirements
- Navigate into your
haiku_poet_agentdirectory (created by duplicatingecho_agent). - Define a Pydantic Schema: Create a class named
HaikuAnalysisthat defines two fields:haiku: A string containing the 5-7-5 poem.sentiment: A string (e.g., "positive", "negative", or "neutral").
- Update the Agent Configuration:
- Set the
output_schemato yourHaikuAnalysisclass. - Set the
output_keyto"last_analysis".
- Set the
- Refine Instructions: Update the agent's
instructionto ensure it understands it must now categorize the sentiment in addition to writing the haiku. - Run and Verify: Run the agent using
adk web haiku_poet_agent. In the Dev UI, verify that the response is a valid JSON object. - Inspect State: After a few interactions, check the "Session State" in the Dev UI to confirm that
"last_analysis"is being populated correctly.
Python Approach (Primary)
Modify agent.py to include the Pydantic model and updated LlmAgent parameters.
from pydantic import BaseModel
from google.adk.agents import LlmAgent
# TODO: Define your Pydantic schema
class HaikuAnalysis(BaseModel):
# TODO: Add fields
pass
root_agent = LlmAgent(
name="haiku_analyzer_agent",
model="gemini-2.5-flash",
instruction="...", # TODO: Update instructions for sentiment + haiku
output_schema=..., # TODO: Link the schema
output_key=... # TODO: Set the output key
)
Self-Reflection Questions
- Why is it better to use
output_schemainstead of just asking the LLM to "respond in JSON" in the text instructions? - What happened to the agent's ability to use tools once you enabled
output_schema? Why does the ADK enforce this restriction? - Look at the Dev UI's "Session State". How could another agent in a future multi-agent system use the data stored in the
"last_analysis"key?
🕵️ Hidden Solution 🕵️
Looking for the solution? Here's a hint (Base64 decode me):
L2RvYy1hZGstdHJhaW5pbmcvbW9kdWxlMDQtbGxtYWdlbnQtZGVlcC1kaXZlL2xhYi1zb2x1dGlvbg==
The direct link is: Lab Solution