Reduce Tech Debt with Gemini 3 Pro
Source notebook
Repo path:
gemini/use-cases/code/refactor_legacy_code.ipynb· Open on GitHub · advanced
Uses Gemini 3.1 Pro on Vertex AI to refactor legacy Python code through tests, specs, design docs, and verification.
Summary
This notebook demonstrates a Gemini-assisted workflow for reducing technical debt in legacy Python modules. It downloads Twisted’s zipstream.py, generates baseline unittest tests, verifies them against the original code, then generates a requirements specification, technical design document, and replacement implementation. It verifies the generated implementation against the same tests and shows how adding new style requirements can drive regeneration.
Key code patterns
Vertex AI GenAI client
from google import genai
client = genai.Client(
vertexai=True,
project=PROJECT_ID,
location=LOCATION,
)Initializes google-genai for Vertex AI using a Google Cloud project and location.
Configured Gemini call
response = client.models.generate_content(
model=MODEL_ID,
contents=prompt,
config=types.GenerateContentConfig(
temperature=1.0,
thinking_config=types.ThinkingConfig(thinking_budget=16384),
),
)Uses Gemini with explicit generation settings and a thinking budget for code analysis tasks.
Prompt template pipeline
with open(template_path) as f:
prompt = f.read()
for key, value in replacements.items():
prompt = prompt.replace(f"{{{key}}}", value)
content = call_llm(prompt)
with open(output_path, "w") as f:
f.write(content)Turns reusable prompt templates into generated tests, specs, design docs, and code.
Baseline and final verification
env["PYTHONPATH"] = f"{code_dir}" + os.pathsep + env.get("PYTHONPATH", "")
cmd = [sys.executable, "-m", "unittest", "-v", os.path.join(test_dir, test_file)]
result = subprocess.run(cmd, check=False, env=env, capture_output=True, text=True)Runs generated tests in a subprocess against both legacy and generated code.
Models & APIs used
- Models: gemini-3.1-pro-preview
- APIs / services: Vertex AI
- SDKs / libraries:
google-genai,pyparseit,markdown,IPython
When to use this
Use this pattern when modernizing a legacy Python module while preserving behavior through generated tests and specification-driven regeneration.
Gotchas & caveats
- Requires an existing Google Cloud project with the Vertex AI API enabled.
- Colab users must authenticate with google.colab.auth.authenticate_user().
- PROJECT_ID and LOCATION must be set or available from GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_REGION.
- The notebook uses LOCATION = “global”.
- The notebook notes that a Jupyter kernel restart may be needed after installing packages.
- Generated tests must pass on the legacy code before they can serve as a baseline.
- The target legacy module is downloaded from GitHub with wget.
Best practices
- Generate passing baseline tests before refactoring legacy code.
- Verify generated tests against the original code before generating replacement code.
- Create a detailed specification from the legacy source before creating a design document.
- Generate new code from the specification and design document, not directly from the legacy code.
- Run the same tests against the generated code for final verification.
- Use prompt templates for separate stages: tests, specification, design document, and implementation.
- Add new requirements to the specification, then regenerate design and code.
Related
- Concepts: Applied Use Cases · Prompt Engineering
- Entities: Vertex AI · Google GenAI SDK · Gemini
- Area: Gemini Notebooks
- Best practices: Applied Use Cases - Best Practices · Prompt Engineering - Best Practices