Code Generation
The kumoai.codegen module can generate Python SDK code for supported connectors, tables, graphs, and predictive queries. Use it to turn a supported saved entity into a reproducible script that can be reviewed, versioned, 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 class is ambiguous, include 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 objects in a shared workspace.
Generate code from an in-memory object
For advanced workflows, pass an SDK object directly with 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
The SDK includes a CLI module. Invoke it directly with python -m:
If your environment exposes the optional kumo-codegen entry point, the equivalent command is:
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 UI or notebook changes, then review the diff before replacing a production script.