Intro to Generating and Executing Python Code with Gemini 3
Source notebook
Repo path:
gemini/code-execution/intro_code_execution.ipynb· Open on GitHub · intro
Uses Gemini 3.5 Flash code execution to generate, run, inspect, and stream Python results via Vertex AI.
Summary
This notebook teaches how to use the Google Gen AI SDK with Vertex AI to enable Gemini code execution. It creates a client, defines a code execution tool, sends prompts that generate and run Python, inspects executable code and execution results, then repeats the pattern in chat and streaming sessions.
Key code patterns
Create Vertex AI client
client = genai.Client(
enterprise=True,
project=PROJECT_ID,
location=LOCATION,
)Connects the Google Gen AI SDK to Gemini through Vertex AI using project and location settings.
Enable code execution tool
code_execution_tool = Tool(
code_execution=ToolCodeExecution()
)Registers Gemini’s ability to generate and execute Python code inside API calls.
Generate and execute code
response = client.models.generate_content(
model=MODEL_ID,
contents=PROMPT,
config=GenerateContentConfig(
tools=[code_execution_tool],
),
)Passes the code execution tool with a prompt so the model can produce code and return verified results.
Read code and results
for part in response.candidates[0].content.parts:
if part.executable_code:
display(Markdown(part.executable_code.code))
if part.code_execution_result:
display(Markdown(part.code_execution_result.output))
print(part.code_execution_result.outcome)Shows how generated Python and execution output are exposed as separate response parts.
Use code execution in chat
chat = client.chats.create(
model=MODEL_ID,
config=GenerateContentConfig(
tools=[code_execution_tool],
),
)
response = chat.send_message(PROMPT)Keeps conversational history while iteratively rewriting code for exploratory data analysis.
Stream code execution output
for chunk in client.models.generate_content_stream(
model=MODEL_ID,
contents=PROMPT,
config=GenerateContentConfig(tools=[code_execution_tool]),
):
for part in chunk.candidates[0].content.parts:
if part.text or part.executable_code or part.code_execution_result:
display(part)Demonstrates streaming natural language, generated code, and execution results as they arrive.
Models & APIs used
- Models: gemini-3.5-flash
- APIs / services: Vertex AI, Gemini API, Vertex AI API
- SDKs / libraries:
google-genai
When to use this
Use this pattern when Gemini should generate Python, execute it, inspect results, and iterate for math, text processing, or exploratory data analysis.
Gotchas & caveats
- Requires an existing Google Cloud project.
- Requires the Vertex AI API to be enabled.
- Colab users must authenticate with google.colab.auth.authenticate_user().
- PROJECT_ID must be set directly or through GOOGLE_CLOUD_PROJECT.
- LOCATION defaults to global unless GOOGLE_CLOUD_REGION is set.
- The code execution tool must be included in GenerateContentConfig for each model, chat, or streaming call.
Best practices
- Use the Google Gen AI SDK unified interface with Vertex AI for enterprise-ready projects.
- Pass code_execution as a Tool instead of expecting code execution by default.
- Inspect executable_code and code_execution_result response parts separately.
- Save or display the output code, result, or outcome downstream in the application.
- Use chat sessions when code needs to be rewritten iteratively with history.
- Use streaming when natural language, generated code, and execution results should be surfaced incrementally.
Related
- Concepts: Gemini Capabilities · Function Calling & Tools · Getting Started
- Entities: Vertex AI · Google GenAI SDK · Gemini · Function Calling · Model Garden
- Area: Gemini Notebooks
- Best practices: Gemini Capabilities - Best Practices · Function Calling & Tools - Best Practices · Getting Started - Best Practices