Notebooks
G
Google Gemini
Basic Code Generation

Basic Code Generation

promptinggemini-cookbookgemini-apiexamplesgemini
Copyright 2025 Google LLC.
[7]

Gemini API: Basic code generation

This notebook demonstrates how to use prompting to perform basic code generation using the Gemini API's Python SDK. Two use cases are explored: error handling and code generation.

The Gemini API can be a great tool to save you time during the development process. Tasks such as code generation, debugging, or optimization can be done with the assistance of the Gemini model.

[8]

Configure your API key

To run the following cell, your API key must be stored it in a Colab Secret named GOOGLE_API_KEY. If you don't already have an API key, or you're not sure how to create a Colab Secret, see Authentication for an example.

[9]

Additionally, select the model you want to use from the available options below:

[10]
MODEL_ID

Examples

Error handling

For code generation, you should prioritize accuracy over creativity. A temperature of 0 ensures that the generated content is deterministic, producing the most sensible output every time.

[11]
[12]

Code generation

[13]
[14]

Let's check if generated code works.

[15]
20 seconds remaining...
19 seconds remaining...
18 seconds remaining...
17 seconds remaining...
16 seconds remaining...
15 seconds remaining...
14 seconds remaining...
13 seconds remaining...
12 seconds remaining...
11 seconds remaining...
10 seconds remaining...
9 seconds remaining...
8 seconds remaining...
7 seconds remaining...
6 seconds remaining...
5 seconds remaining...
4 seconds remaining...
3 seconds remaining...
2 seconds remaining...
1 seconds remaining...
Time is up!

Next steps

Be sure to explore other examples of prompting in the repository. Try writing prompts around your own code as well using the examples in this notebook.