Code Generation
The kumoai.codegen module generates Python SDK code for supported connectors, tables, graphs, and predictive queries. Use it to export any supported Kumo entity as a reproducible Python script you can review, version, and rerun.
Code generation is intended for SDK objects that the codegen module supports in your installed SDK version. If a loaded object has no registered codegen handler, generate_code() raises UnsupportedEntityError. Invalid or unsupported ID/entity-class inputs can raise ValueError.
Generate code by ID
Initialize the SDK, then call generate_code() with an entity ID. When the entity ID alone does not identify the type, also pass entity_class.
The generated script includes Kumo initialization boilerplate and the imports required to recreate the entity and its dependencies.
Write generated code to a file
Pass output_path to write the generated script directly.
Review the generated file before running it in production, especially if it will create or update Kumo entities in a shared workspace.
Generate code from an in-memory object
Pass an in-memory SDK object directly using the object input mode.
This is useful in notebooks when you have modified an object interactively and want to export the equivalent Python code.
Command-line usage
Invoke the CLI module directly with python -m:
If the optional kumo-codegen entry point is available in your environment:
Use --verbose to print additional diagnostics.
Supported input modes
How dependency generation works
When you generate code for an entity with dependencies, codegen emits parent objects first. For example, generating a predictive query can emit the connector, source tables, Kumo tables, and graph needed by that query.
The generator deduplicates equivalent supported connector configurations; other entities are reused only when the same object instance is encountered.
Error handling
Catch codegen-specific exceptions when building automation around generated scripts.
Best practices
- Commit generated scripts alongside the analysis or pipeline that depends on them.
- Treat generated code as a starting point; rename variables and add comments before production use.
- Prefer environment variables for
KUMO_API_ENDPOINTandKUMO_API_KEYso generated scripts do not contain secrets. - Regenerate code after substantial changes in the Kumo UI or notebooks, then review the diff before replacing a production script.