Skip to content

feat: add genai-migrate codemod CLI for legacy google-generativeai#2748

Open
jeshiomurmu wants to merge 2 commits into
googleapis:mainfrom
jeshiomurmu:feat/genai-migrate-codemod
Open

feat: add genai-migrate codemod CLI for legacy google-generativeai#2748
jeshiomurmu wants to merge 2 commits into
googleapis:mainfrom
jeshiomurmu:feat/genai-migrate-codemod

Conversation

@jeshiomurmu

Copy link
Copy Markdown

Description

This PR introduces a source-to-source migration tool (genai-migrate) powered by LibCST to automate the transition from the deprecated google-generativeai package to the new google-genai SDK.

Why this is needed

Developers are currently facing a 'DX crisis' (referenced in issue #1606) where AI code generators continue to hallucinate legacy patterns. Manually migrating large codebases is error-prone and time-consuming. This tool automates the most common pattern shifts while preserving original code formatting.

Supported Transformations

  • Imports: import google.generativeai as genai -> from google import genai
  • Configuration: genai.configure() -> genai.Client()
  • Model Instantiation: Captures GenerativeModel variables to map them to the new Client architecture.
  • Content Generation: model.generate_content() -> client.models.generate_content()
  • Streaming: Automatically converts stream=True calls to generate_content_stream().
  • Chat/Tokens: Migrates start_chat() and count_tokens() to the new client.chats and client.models namespaces.

Quality Assurance

  • Automated Tests: 12/12 tests passing (including migration and idempotency checks).
  • Fixtures: 6 legacy/expected fixture pairs created covering all major use cases.
  • Real-world Validation: Verified against a public legacy repository to ensure AST parseability.
  • Linting: Formatted with Black and isort to match repository style.

Fixes #1606

Adds a formatting-preserving source-to-source rewriter (powered by libcst)
to help developers migrate from the deprecated google-generativeai patterns
to the new google-genai Client/Models/Chats architecture.

Addresses the DX crisis outlined in upstream issue googleapis#1606 where AI code
generators hallucinate legacy patterns (GenerativeModel, start_chat,
genai.configure), costing developers weeks of debugging.

Supported transforms:
- import google.generativeai as genai  ->  from google import genai
- genai.configure(api_key=...)         ->  client = genai.Client(api_key=...)
- genai.GenerativeModel('gemini-X')    ->  model name captured as string
- model.generate_content(...)          ->  client.models.generate_content(model=..., contents=...)
- model.generate_content(..., stream=True) -> client.models.generate_content_stream(...)
- model.start_chat(history=...)        ->  client.chats.create(model=..., ...)
- model.count_tokens(...)              ->  client.models.count_tokens(model=..., contents=...)

Install: pip install google-genai[migrate]
Usage:    genai-migrate ./app/ --diff   # preview
          genai-migrate ./app/ --in-place
@google-cla

google-cla Bot commented Jul 20, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Strategic Crisis] AI Code Generation is Irrecoverably Breaking the google-genai Ecosystem Due to Deprecated API Patterns

1 participant