Skip to main content
Codegen enables you to create reusable code transformations using Python functions decorated with @codegen.function. These codemods can be shared, versioned, and run by your team.

Creating Codemods

The easiest way to create a new codemod is using the CLI create command:
This creates a new codemod in your .codegen/codemods directory:
Codemods are stored in .codegen/codemods/name/name.py and are tracked in Git for easy sharing.

AI-Powered Generation with -d

You can use AI to generate an initial implementation by providing a description:
This will:
  1. Generate an implementation based on your description
  2. Create a custom system prompt that you can provide to an IDE chat assistant (learn more about working with AI)
  3. Place both files in the codemod directory

Running Codemods

Once created, run your codemod using:
The execution flow:
  1. Codegen parses your codebase into a graph representation
  2. Your codemod function is executed against this graph
  3. Changes are tracked and applied to your filesystem
  4. A diff preview shows what changed

Codemod Structure

A codemod consists of three main parts:
  1. The @codegen.function decorator that names your codemod
  2. A run function that takes a Codebase parameter
  3. Your transformation logic using the Codebase API

Arguments

Codemods can accept arguments using Pydantic models:
Run it with:

Directory Structure

Your codemods live in a dedicated directory structure: