কাঠামোগত আউটপুট
আপনি প্রদত্ত JSON স্কিমা মেনে প্রতিক্রিয়া তৈরি করার জন্য জেমিনি মডেলগুলি কনফিগার করতে পারেন। এটি অনুমানযোগ্য, টাইপ-সেফ ফলাফল নিশ্চিত করে এবং অসংগঠিত টেক্সট থেকে সংগঠিত ডেটা নিষ্কাশনকে সহজ করে তোলে।
কাঠামোগত আউটপুট ব্যবহার করা নিম্নলিখিত ক্ষেত্রে আদর্শ:
- ডেটা নিষ্কাশন: টেক্সট থেকে নাম এবং তারিখের মতো নির্দিষ্ট তথ্য বের করা।
- কাঠামোগত শ্রেণিবিন্যাস: পাঠ্যকে পূর্বনির্ধারিত বিভাগগুলিতে শ্রেণিবদ্ধ করুন।
- এজেন্টিক ওয়ার্কফ্লো: টুল বা এপিআই-এর জন্য কাঠামোগত ইনপুট তৈরি করুন।
REST API-তে JSON স্কিমা সমর্থন করার পাশাপাশি, Google GenAI SDK-গুলো Pydantic (Python) এবং Zod (JavaScript) ব্যবহার করে স্কিমা নির্ধারণ করার সুযোগ দেয়।
এই উদাহরণটি দেখায় কিভাবে object , array , string এবং integer মতো মৌলিক JSON স্কিমা টাইপ ব্যবহার করে টেক্সট থেকে কাঠামোগত ডেটা বের করা যায়।
পাইথন
from google import genai
from pydantic import BaseModel, Field
from typing import List, Optional
class Ingredient(BaseModel):
name: str = Field(description="Name of the ingredient.")
quantity: str = Field(description="Quantity of the ingredient, including units.")
class Recipe(BaseModel):
recipe_name: str = Field(description="The name of the recipe.")
prep_time_minutes: Optional[int] = Field(description="Optional time in minutes to prepare the recipe.")
ingredients: List[Ingredient]
instructions: List[str]
client = genai.Client()
prompt = """
Please extract the recipe from the following text.
The user wants to make delicious chocolate chip cookies.
They need 2 and 1/4 cups of all-purpose flour, 1 teaspoon of baking soda,
1 teaspoon of salt, 1 cup of unsalted butter (softened), 3/4 cup of granulated sugar,
3/4 cup of packed brown sugar, 1 teaspoon of vanilla extract, and 2 large eggs.
For the best part, they'll need 2 cups of semisweet chocolate chips.
First, preheat the oven to 375°F (190°C). Then, in a small bowl, whisk together the flour,
baking soda, and salt. In a large bowl, cream together the butter, granulated sugar, and brown sugar
until light and fluffy. Beat in the vanilla and eggs, one at a time. Gradually beat in the dry
ingredients until just combined. Finally, stir in the chocolate chips. Drop by rounded tablespoons
onto ungreased baking sheets and bake for 9 to 11 minutes.
"""
interaction = client.interactions.create(
model="gemini-3-flash-preview",
input=prompt,
response_format={
"type": "text",
"mime_type": "application/json",
"schema": Recipe.model_json_schema()
},
)
recipe = Recipe.model_validate_json(interaction.steps[-1].content[0].text)
print(recipe)
জাভাস্ক্রিপ্ট
import { GoogleGenAI } from "@google/genai";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
const ingredientSchema = z.object({
name: z.string().describe("Name of the ingredient."),
quantity: z.string().describe("Quantity of the ingredient, including units."),
});
const recipeSchema = z.object({
recipe_name: z.string().describe("The name of the recipe."),
prep_time_minutes: z.number().optional().describe("Optional time in minutes to prepare the recipe."),
ingredients: z.array(ingredientSchema),
instructions: z.array(z.string()),
});
const client = new GoogleGenAI({});
const prompt = `
Please extract the recipe from the following text.
The user wants to make delicious chocolate chip cookies.
They need 2 and 1/4 cups of all-purpose flour, 1 teaspoon of baking soda,
1 teaspoon of salt, 1 cup of unsalted butter (softened), 3/4 cup of granulated sugar,
3/4 cup of packed brown sugar, 1 teaspoon of vanilla extract, and 2 large eggs.
For the best part, they'll need 2 cups of semisweet chocolate chips.
First, preheat the oven to 375°F (190°C). Then, in a small bowl, whisk together the flour,
baking soda, and salt. In a large bowl, cream together the butter, granulated sugar, and brown sugar
until light and fluffy. Beat in the vanilla and eggs, one at a time. Gradually beat in the dry
ingredients until just combined. Finally, stir in the chocolate chips. Drop by rounded tablespoons
onto ungreased baking sheets and bake for 9 to 11 minutes.
`;
const interaction = await client.interactions.create({
model: "gemini-3-flash-preview",
input: prompt,
response_format: {
type: 'text',
mime_type: 'application/json',
schema: zodToJsonSchema(recipeSchema)
},
});
const recipe = recipeSchema.parse(JSON.parse(interaction.steps.at(-1).content[0].text));
console.log(recipe);
বিশ্রাম
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3-flash-preview",
"input": "Please extract the recipe from the following text.\nThe user wants to make delicious chocolate chip cookies.\nThey need 2 and 1/4 cups of all-purpose flour, 1 teaspoon of baking soda,\n1 teaspoon of salt, 1 cup of unsalted butter (softened), 3/4 cup of granulated sugar,\n3/4 cup of packed brown sugar, 1 teaspoon of vanilla extract, and 2 large eggs.\nFor the best part, they will need 2 cups of semisweet chocolate chips.\nFirst, preheat the oven to 375°F (190°C). Then, in a small bowl, whisk together the flour,\nbaking soda, and salt. In a large bowl, cream together the butter, granulated sugar, and brown sugar\nuntil light and fluffy. Beat in the vanilla and eggs, one at a time. Gradually beat in the dry\ningredients until just combined. Finally, stir in the chocolate chips. Drop by rounded tablespoons\nonto ungreased baking sheets and bake for 9 to 11 minutes.",
"response_format": {
"type": "text",
"mime_type": "application/json",
"schema": {
"type": "object",
"properties": {
"recipe_name": {
"type": "string",
"description": "The name of the recipe."
},
"prep_time_minutes": {
"type": "integer",
"description": "Optional time in minutes to prepare the recipe."
},
"ingredients": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": { "type": "string", "description": "Name of the ingredient."},
"quantity": { "type": "string", "description": "Quantity of the ingredient, including units."}
},
"required": ["name", "quantity"]
}
},
"instructions": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["recipe_name", "ingredients", "instructions"]
}
}
}
}'
উদাহরণ উত্তর:
{
"recipe_name": "Delicious Chocolate Chip Cookies",
"ingredients": [
{ "name": "all-purpose flour", "quantity": "2 and 1/4 cups" },
{ "name": "baking soda", "quantity": "1 teaspoon" },
{ "name": "salt", "quantity": "1 teaspoon" },
{ "name": "unsalted butter (softened)", "quantity": "1 cup" },
{ "name": "granulated sugar", "quantity": "3/4 cup" },
{ "name": "packed brown sugar", "quantity": "3/4 cup" },
{ "name": "vanilla extract", "quantity": "1 teaspoon" },
{ "name": "large eggs", "quantity": "2" },
{ "name": "semisweet chocolate chips", "quantity": "2 cups" }
],
"instructions": [
"Preheat the oven to 375°F (190°C).",
"In a small bowl, whisk together the flour, baking soda, and salt.",
"In a large bowl, cream together the butter, granulated sugar, and brown sugar until light and fluffy.",
"Beat in the vanilla and eggs, one at a time.",
"Gradually beat in the dry ingredients until just combined.",
"Stir in the chocolate chips.",
"Drop by rounded tablespoons onto ungreased baking sheets and bake for 9 to 11 minutes."
]
}
স্ট্রিমিং ফলাফল
আপনি স্ট্রাকচার্ড আউটপুট স্ট্রিম করতে পারেন, যার ফলে রেসপন্সটি তৈরি হওয়ার সাথে সাথেই আপনি সেটির প্রসেসিং শুরু করতে পারবেন। স্ট্রিম করা অংশগুলো হলো বৈধ আংশিক JSON স্ট্রিং, যেগুলোকে একত্রিত করে চূড়ান্ত JSON অবজেক্টটি তৈরি করা যায়।
পাইথন
from google import genai
from pydantic import BaseModel
from typing import Literal
class Feedback(BaseModel):
sentiment: Literal["positive", "neutral", "negative"]
summary: str
client = genai.Client()
prompt = "The new UI is incredibly intuitive. Add a very long summary to test streaming!"
stream = client.interactions.create(
model="gemini-3-flash-preview",
input=prompt,
response_format={
"type": "text",
"mime_type": "application/json",
"schema": Feedback.model_json_schema()
},
stream=True
)
for event in stream:
if event.event_type == "step.delta" and event.delta.text:
print(event.delta.text, end="")
জাভাস্ক্রিপ্ট
import { GoogleGenAI } from "@google/genai";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
const client = new GoogleGenAI({});
const feedbackSchema = z.object({
sentiment: z.enum(["positive", "neutral", "negative"]),
summary: z.string(),
});
const stream = await client.interactions.create({
model: "gemini-3-flash-preview",
input: "The new UI is incredibly intuitive. Add a very long summary!",
response_format: {
type: 'text',
mime_type: 'application/json',
schema: zodToJsonSchema(feedbackSchema)
},
stream: true,
});
for await (const event of stream) {
if (event.type === "step.delta" && event.delta?.text) {
process.stdout.write(event.delta.text);
}
}
সরঞ্জাম সহ কাঠামোগত আউটপুট
জেমিনি ৩ আপনাকে স্ট্রাকচার্ড আউটপুট-এর সাথে বিল্ট-ইন টুলগুলো একত্রিত করার সুযোগ দেয়, যার মধ্যে রয়েছে গুগল সার্চের মাধ্যমে গ্রাউন্ডিং , ইউআরএল কনটেক্সট , কোড এক্সিকিউশন , ফাইল সার্চ এবং ফাংশন কলিং ।
পাইথন
from google import genai
from pydantic import BaseModel, Field
from typing import List
class MatchResult(BaseModel):
winner: str = Field(description="The name of the winner.")
final_match_score: str = Field(description="The final match score.")
scorers: List[str] = Field(description="The name of the scorer.")
client = genai.Client()
interaction = client.interactions.create(
model="gemini-3.1-pro-preview",
input="Search for all details for the latest Euro.",
tools=[{"type": "google_search"}, {"type": "url_context"}],
response_format={
"type": "text",
"mime_type": "application/json",
"schema": MatchResult.model_json_schema()
},
)
result = MatchResult.model_validate_json(interaction.steps[-1].content[0].text)
print(result)
জাভাস্ক্রিপ্ট
import { GoogleGenAI } from "@google/genai";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
const client = new GoogleGenAI({});
const matchSchema = z.object({
winner: z.string().describe("The name of the winner."),
final_match_score: z.string().describe("The final score."),
scorers: z.array(z.string()).describe("The name of the scorer.")
});
const interaction = await client.interactions.create({
model: "gemini-3.1-pro-preview",
input: "Search for all details for the latest Euro.",
tools: [{type: "google_search"}, {type: "url_context"}],
response_format: {
type: 'text',
mime_type: 'application/json',
schema: zodToJsonSchema(matchSchema)
},
});
const match = matchSchema.parse(JSON.parse(interaction.steps.at(-1).content[0].text));
console.log(match);
বিশ্রাম
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{
"model": "gemini-3.1-pro-preview",
"input": "Search for all details for the latest Euro.",
"tools": [{"type": "google_search"}, {"type": "url_context"}],
"response_format": {
"type": "text",
"mime_type": "application/json",
"schema": {
"type": "object",
"properties": {
"winner": {"type": "string"},
"final_match_score": {"type": "string"},
"scorers": {"type": "array", "items": {"type": "string"}}
},
"required": ["winner", "final_match_score", "scorers"]
}
}
}'
JSON স্কিমা সমর্থন
একটি JSON অবজেক্ট তৈরি করতে, response_format ফিল্ডে text টাইপের একটি অবজেক্ট (অথবা অবজেক্ট ধারণকারী একটি অ্যারে) কনফিগার করুন এবং এর mime_type application/json এ সেট করুন। schema ফিল্ডে স্কিমাটি প্রদান করতে হবে।
জেমিনির স্ট্রাকচার্ড আউটপুট মোড JSON স্কিমা স্পেসিফিকেশনের একটি উপসেট সমর্থন করে।
নিম্নলিখিত type মানগুলি সমর্থিত:
-
string: টেক্সটের জন্য। -
number: ফ্লোটিং-পয়েন্ট সংখ্যার জন্য। -
integer: অখণ্ড সংখ্যার জন্য। -
boolean: সত্য বা মিথ্যা মানের জন্য। -
object: কী-ভ্যালু জোড়যুক্ত কাঠামোগত ডেটার জন্য। -
array: আইটেমের তালিকার জন্য। -
null: কোনো প্রপার্টিকে null হওয়ার অনুমতি দিতে, type অ্যারেতে"null"অন্তর্ভুক্ত করুন (যেমন,{"type": ["string", "null"]})।
এই বর্ণনামূলক বৈশিষ্ট্যগুলো মডেলটিকে পথনির্দেশনা দিতে সাহায্য করে:
-
title: কোনো সম্পত্তির সংক্ষিপ্ত বিবরণ। -
description: কোনো সম্পত্তির একটি দীর্ঘতর ও অধিকতর বিশদ বিবরণ।
প্রকার-নির্দিষ্ট বৈশিষ্ট্য
object মানগুলির জন্য:
-
properties: এমন একটি অবজেক্ট যেখানে প্রতিটি কী হলো একটি প্রোপার্টির নাম এবং প্রতিটি ভ্যালু হলো সেই প্রোপার্টির স্কিমা। -
required: স্ট্রিংগুলির একটি অ্যারে, যেখানে কোন প্রপার্টিগুলি বাধ্যতামূলক তার তালিকা থাকবে। -
additionalProperties:propertiesএ তালিকাভুক্ত নয় এমন প্রপার্টি অনুমোদিত হবে কিনা তা নিয়ন্ত্রণ করে। এটি একটি বুলিয়ান বা স্কিমা হতে পারে।
string মানের জন্য:
-
enum: শ্রেণিবিন্যাস কাজের জন্য সম্ভাব্য স্ট্রিংগুলির একটি নির্দিষ্ট সেট তালিকাভুক্ত করে। -
format: স্ট্রিংটির জন্য একটি সিনট্যাক্স নির্দিষ্ট করে, যেমনdate-time,date,time।
number এবং integer মানের জন্য:
-
enum: সম্ভাব্য সাংখ্যিক মানগুলির একটি নির্দিষ্ট সেট তালিকাভুক্ত করে। -
minimum: সর্বনিম্ন অন্তর্ভুক্ত মান। -
maximum: সর্বোচ্চ অন্তর্ভুক্ত মান।
array মানগুলির জন্য:
-
items: অ্যারের অন্তর্ভুক্ত সমস্ত আইটেমের স্কিমা নির্ধারণ করে। -
prefixItems: প্রথম N সংখ্যক আইটেমের জন্য স্কিমার একটি তালিকা নির্ধারণ করে, যা টাপল-সদৃশ কাঠামো অনুমোদন করে। -
minItems: অ্যারেতে থাকা আইটেমের সর্বনিম্ন সংখ্যা। -
maxItems: অ্যারেতে থাকা আইটেমের সর্বোচ্চ সংখ্যা।
মডেল সমর্থন
| মডেল | কাঠামোগত আউটপুট |
|---|---|
| জেমিনি ৩.১ প্রো প্রিভিউ | ✔️ |
| জেমিনি ৩ ফ্ল্যাশ প্রিভিউ | ✔️ |
| জেমিনি ২.৫ প্রো | ✔️ |
| জেমিনি ২.৫ ফ্ল্যাশ | ✔️ |
| জেমিনি ২.৫ ফ্ল্যাশ-লাইট | ✔️ |
| জেমিনি ২.০ ফ্ল্যাশ | ✔️* |
| জেমিনি ২.০ ফ্ল্যাশ-লাইট | ✔️* |
জেমিনি ২.০-এর জন্য একটি সুস্পষ্ট propertyOrdering তালিকা প্রয়োজন।
কাঠামোগত আউটপুট বনাম ফাংশন কলিং
| বৈশিষ্ট্য | প্রাথমিক ব্যবহারের ক্ষেত্র |
|---|---|
| কাঠামোগত আউটপুট | চূড়ান্ত উত্তরের বিন্যাস। যখন আপনি মডেলের উত্তর একটি নির্দিষ্ট বিন্যাসে চান, তখন এটি ব্যবহার করুন। |
| ফাংশন কলিং | কথোপকথন চলাকালীন পদক্ষেপ গ্রহণ করা। যখন মডেলকে চূড়ান্ত উত্তর দেওয়ার আগে আপনাকে কোনো কাজ করতে বলতে হয়, তখন এটি ব্যবহার করুন। |
সর্বোত্তম অনুশীলন
- সুস্পষ্ট বিবরণ: মডেলকে নির্দেশনা দিতে
descriptionক্ষেত্রটি ব্যবহার করুন। - স্ট্রং টাইপিং: নির্দিষ্ট টাইপ (
integer,string,enum) ব্যবহার করুন। - দ্রুত প্রকৌশল: মডেলটি দিয়ে আপনি কী করাতে চান, তা স্পষ্টভাবে বলুন।
- যাচাইকরণ: আউটপুট সিনট্যাক্সগতভাবে সঠিক JSON হলেও, আপনার অ্যাপ্লিকেশনে মানগুলো সর্বদা যাচাই করুন।
- ত্রুটি ব্যবস্থাপনা: স্কিমা-সম্মত কিন্তু অর্থগতভাবে ভুল আউটপুটের জন্য শক্তিশালী ত্রুটি ব্যবস্থাপনা বাস্তবায়ন করুন।
সীমাবদ্ধতা
- স্কিমা সাবসেট: JSON স্কিমার সকল বৈশিষ্ট্য সমর্থিত নয়।
- স্কিমার জটিলতা: খুব বড় বা গভীরভাবে স্তরীভূত স্কিমা প্রত্যাখ্যাত হতে পারে।