v139 · AI · Prompt API
Structured Output Studio
Force Gemini Nano to respond as valid JSON by passing a responseConstraint JSON schema. Enter text, pick an analysis type, and receive a guaranteed-valid structured object.
Chrome 148 required: Structured output via
responseConstraint needs Chrome 148+ with Gemini Nano enabled.
Without it, this demo uses a simulated response to show the concept.
See the Prompt Playground for setup instructions.
Input text
Schema
Raw JSON output
// Output appears here
Rendered
JSON Schema used
/* Select a schema above */
API code
const session = await LanguageModel.create({
systemPrompt: 'You are a text analysis assistant.',
// Constrain response to valid JSON matching this schema:
responseConstraint: {
type: 'json_schema',
json_schema: {
name: 'text_analysis',
schema: {
type: 'object',
properties: {
sentiment: { type: 'string', enum: ['positive', 'neutral', 'negative'] },
score: { type: 'number', minimum: -1, maximum: 1 },
keywords: { type: 'array', items: { type: 'string' }, maxItems: 5 },
summary: { type: 'string', maxLength: 200 },
},
required: ['sentiment', 'score', 'keywords', 'summary'],
additionalProperties: false,
}
}
}
});
const result = await session.prompt(userText);
const data = JSON.parse(result); // guaranteed valid JSON