Lesson 1 of 6
Get a structured answer from a short text
In the Gemini chat window, write a prompt that gives Gemini a role, a short text, and a required output format, then check the answer.
4 minClaude, Gemini and ChatGPT
After this lesson, you will be able to write a prompt that makes Gemini answer in a fixed format, and you will know how to check that the answer follows it.
Start with a clear job
Gemini answers better when you tell it what to do. A prompt with a role, a task, and a required output format gives Gemini less room to guess.
Open the Gemini chat window in your browser. You need a Google account, but you do not need to pay for this lesson.
Copy this prompt into the chat box:
You are a careful research assistant. Read the text below and list every date it mentions, with the event that happened on that date. Return the answer as a JSON object with one key "events". The value must be a list of objects, each with keys "date" and "event".
Text:
The company was founded on 3 April 2011. Its first product shipped on 15 November 2012. The second product shipped on 9 May 2014.Run the prompt. Gemini should return a JSON object like this:
{
"events": [
{"date": "3 April 2011", "event": "company founded"},
{"date": "15 November 2012", "event": "first product shipped"},
{"date": "9 May 2014", "event": "second product shipped"}
]
}The answer may be worded slightly differently, but the structure must match: one key events, whose value is a list of objects with keys date and event.
Why a role helps
The first sentence sets a role: "You are a careful research assistant." This tells Gemini what kind of answer you want. A role is a short phrase that describes the helper's job.
Without a role, Gemini might answer in a different style, such as a paragraph instead of a list. The role does not force a format by itself, but it guides the tone and level of detail.
Why a required format helps
The prompt says: "Return the answer as a JSON object with one key events." This is the required output format. It tells Gemini exactly what shape the answer must have.
A required format is useful when you want to use the answer in a program. A program can read JSON easily. If the answer is a plain paragraph, the program would have to search for the dates.
The prompt also says what each object in the list must contain: keys date and event. This makes the answer predictable.
Check the answer
After Gemini answers, check three things:
- The answer is valid JSON. You can paste it into a JSON checker, or just look at the brackets and commas.
- The top-level object has only the key
events. - Each item in the list has exactly the keys
dateandevent.
If any of these is wrong, change the prompt to be more specific. For example, if Gemini adds an extra key, say: "Do not add any other keys."
Try a different format
Now ask for the same information as a table. Copy this prompt:
You are a careful research assistant. Read the text below and list every date it mentions, with the event that happened on that date. Return the answer as a Markdown table with two columns: Date and Event.
Text:
The company was founded on 3 April 2011. Its first product shipped on 15 November 2012. The second product shipped on 9 May 2014.Gemini should return a table with two columns and three rows of data. The exact wording in the cells may differ, but the table structure must be there.
This shows that the same text can be turned into different formats by changing one sentence in the prompt.
Keep the prompt reusable
In the next lessons, you will use the same idea in Python. A good prompt has three parts:
- A role that sets the style.
- The text to work on, clearly separated.
- A required output format, with the exact keys or columns.
Write your prompts so that only the text changes from one run to the next. That will make it easy to move to the API later.
Key takeaways
- A role tells Gemini what kind of helper to be.
- A required output format makes the answer predictable and easy to use in a program.
- Check that the answer follows the format exactly, and adjust the prompt if it does not.
- Keep the prompt reusable by separating the role, the text, and the format.
Try it
Write a prompt for Gemini that takes this short text: "The meeting is on Monday 12 June at 10:00. The deadline is Friday 16 June at 17:00. The review is on Wednesday 21 June at 14:00." Ask Gemini to list every event with its day, date, and time. Require the answer as a JSON object with one key "schedule", whose value is a list of objects with keys "day", "date", and "time". Run the prompt in the Gemini chat window. A good result is valid JSON with exactly those keys, and no extra keys, for all three events.
Official documentation
Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.