Why Code Generation?
Without codegen, you’d write:Generating Models
Using MoxnClient
Generate models for all prompts in a task:task_id: The task containing your promptsbranch_nameorcommit_id: Which version to generate fromoutput_dir: Directory to write the generated file (optional)
DatamodelCodegenResponse with:
filename: The generated file namegenerated_code: The Python code
Output File
The generated file is named after your task:- generated/
- customer_support_bot_models.py
What Gets Generated
For each prompt with an input schema, you get:1. A Pydantic Model
2. A TypedDict for Rendered Output
3. Nested Types
Complex schemas generate nested models:The Two Representations
Code generation produces two related types for each schema:| Type | Purpose | Values |
|---|---|---|
| Pydantic Model | Input validation, IDE support | Typed (str, int, list[T]) |
| TypedDict | What gets injected into prompts | All strings |
- Your code works with typed data (lists, numbers, nested objects)
- Prompts receive string values (JSON, markdown, custom formats)
render() method bridges these two:
Customizing render()
The generatedrender() method provides a default implementation, but you can override it:
Default behavior
Custom markdown formatting
Custom XML formatting
Using kwargs
Pass extra parameters torender():
Schema Metadata
Generated models include metadata linking them to their source:- Creating sessions directly from session data
- Tracking which schema version was used in telemetry
- Validating that session data matches the expected prompt
When to Regenerate
Regenerate models when:- You add or modify variables in your prompts
- You change property types
- You add new prompts to your task
- You want to capture a new commit version
Workflow suggestion
Run codegen as part of your development workflow:Without Code Generation
You don’t have to use codegen. You can create your own models:RenderableModel protocol:
Type Mappings
JSON Schema types map to Python types:| JSON Schema | Python Type |
|---|---|
string | str |
integer | int |
number | float |
boolean | bool |
array | list[T] |
object | nested model or dict |
string + format: date | date |
string + format: date-time | datetime |
string + format: email | str (with validation) |
string + format: uri | str (with validation) |