Tutorial 1: The Deterministic Assembly Line

Tutorial 1: The Deterministic Assembly Line

In a factory, a stamping machine is deterministic. You put in a sheet of metal, you get the exact same fender out every single time. If the machine malfunctions, it’s a mechanical error, not an "interpretation" error.

In software, deterministic generation is your bread and butter: In python code generators, that would be Jinja2 templates, Pydantic schemas, and rigid boilerplate generation. It is the "Assembly Line." It is safe, repeatable, and boring. That is why it works.

Step 1: The Environment Setup

Do not skip this. If your environment is messy, your brain is messy.

mkdir enterprise_gen
cd enterprise_gen

git init

python -m venv .venv
source .venv/bin/activate  # On Windows, use: .venv\Scripts\activate

pip install jinja2 pydantic pytest

touch generator.py test_generator.py template.j2
        

Step 2: The Template (The Mold)

We are going to define a template that creates a Pydantic model based on a configuration.

Open template.j2 and type this:

from pydantic import BaseModel

class {{ class_name }}(BaseModel):
    {% for field, type in fields.items() -%}
    {{ field }}: {{ type }}
    {% endfor %}
        

Step 3: The Generator (The Machine)

Open generator.py. This is our deterministic engine. It takes input and outputs exact code.

from jinja2 import Template

def generate_model(class_name: str, fields: dict) -> str:
    with open("template.j2", "r") as f:
        template = Template(f.read())
    return template.render(class_name=class_name, fields=fields)

if __name__ == "__main__":
    fields = {"id": "int", "name": "str", "balance": "float"}
    print(generate_model("Account", fields))
        

Step 4: Test-Driven Verification

We don't trust our eyes; we trust the test. If it isn't tested, it doesn't exist.

Open test_generator.py:

import pytest
from generator import generate_model

def test_generate_model_output():
    fields = {"id": "int"}
    result = generate_model("TestModel", fields)
    
    assert "class TestModel(BaseModel):" in result
    assert "id: int" in result

def test_generate_empty_fields():
    result = generate_model("Empty", {})
    assert "class Empty(BaseModel):" in result
        

Step 5: Execute and Validate

Run your tests. If these fail, you typed something wrong. Go back and check the indentation in template.j2.

pytest test_generator.py
        

The Lesson:

This code is deterministic. Given the same fields dictionary, it will produce the exact same string, character for character, every single time. It cannot "hallucinate" a new field. It cannot decide that balance should be a Decimal instead of a float unless you change the logic.

That is your baseline. Everything we build from here—even the AI parts—must respect this level of structural integrity.

When you have the tests passing and are ready for the next Tutorial, wait till tomorrow. We introduce the non-deterministic element and see why determinism starts to break.

The hybrid approach combining deterministic safety nets with non-deterministic AI intelligence is the right architectural mindset for 2026. Constraining LLMs with structured workflows isn't limiting — it's what makes them production-ready. Great series ahead.

Like
Reply

To view or add a comment, sign in

Explore content categories