From dcfad54db2599b720156bb89dbc36b96267e7819 Mon Sep 17 00:00:00 2001 From: sedanah-m Date: Fri, 24 Jul 2026 13:07:10 -0700 Subject: [PATCH 1/2] adds: structured output demo with JSON and enum schemas. adds: UI to switch between the modes, input prompts, and display the responses. --- .../src/features/structured-output/index.tsx | 122 +++++++++++++++++- .../src/features/structured-output/service.ts | 79 +++++++++++- 2 files changed, 195 insertions(+), 6 deletions(-) diff --git a/ai/ai-samples/src/features/structured-output/index.tsx b/ai/ai-samples/src/features/structured-output/index.tsx index 7542981dc..453e8ba95 100644 --- a/ai/ai-samples/src/features/structured-output/index.tsx +++ b/ai/ai-samples/src/features/structured-output/index.tsx @@ -1,9 +1,121 @@ -import React from 'react'; +import { useState, useEffect } from 'react'; +import { generateWithSDKSchema, generateWithEnumValues } from './service'; + +export default function StructuredOutputView() { + const [activeTab, setActiveTab] = useState<'json' | 'enum'>('json'); + const [prompt, setPrompt] = useState(''); + const [result, setResult] = useState(''); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + useEffect(() => { + if (activeTab === 'json') { + setPrompt('Generate 3 animal-based characters for a card game.'); + } else { + setPrompt('The film aims to educate and inform viewers about real-life subjects, events, or people. It offers a factual record of a particular topic by combining interviews, historical footage, and narration.'); + } + setResult(''); + setError(null); + }, [activeTab]); + + const handleGenerate = async () => { + const cleanedPrompt = prompt.trim(); + if (!cleanedPrompt) return; + + setLoading(true); + setError(null); + setResult(''); + + try { + let output = ''; + if (activeTab === 'json') { + output = await generateWithSDKSchema(cleanedPrompt); + } else { + output = await generateWithEnumValues(cleanedPrompt); + } + setResult(output); + } catch (err: unknown) { + if (err instanceof Error) { + setError(err.message); + } else { + setError('An error occurred while generating structured output.'); + } + } finally { + setLoading(false); + } + }; -export default function Feature() { return ( -
-

structured-output

+
+

Structured Output Generation

+

+ Force Gemini to return strictly formatted data by passing a schema into the generation configuration. +

+ +
+ + +
+ +