Skip to main content

Jun 2025 (v0.3.1)

Mochi v0.3.1 expands on the generative AI foundation introduced in v0.3.0 by adding first-class support for user-defined types and structured AI output generation. Programs can now declare their own rich types, instantiate them manually, or populate them dynamically using AI-generated responses.

This release further emphasizes Mochi’s commitment to clarity, composability, and deep integration with modern AI workflows.

New Language Feature: User-Defined Types

You can now declare custom struct-like types using the type keyword. These types group related fields under a named structure, enabling safer and more expressive programs.

Example

type Person {
name: string
age: int
email: string
}

type Book {
title: string
author: Person
pages: int
tags: list<string>
metadata: map<string, string>
published: bool
}

Usage

Declared types can be used to declare variables, construct values, and access fields:

let bob: Person = Person {
name: "Bob",
age: 42,
}

let book: Book = Book {
title: "Example",
author: bob,
pages: 100,
tags: ["guide"],
metadata: { "isbn": "123" },
published: true
}

print("custom types defined")
print(book.author.name)

Features

  • ✅ Full support for nested structs, lists, and maps
  • ✅ Inline value construction using { field: value, ... } syntax
  • ✅ Type-annotated variables (let x: Person)
  • ✅ Field access with dot notation (x.field)

This forms the foundation for composable data modeling, JSON-like transformations, and generative type synthesis.

New Language Feature: generate <Type>

Building on user-defined types, you can now use LLMs to synthesize structured data directly:

let p = generate Person {
prompt: "Generate a fictional software engineer with name, age, and email."
}

Mochi parses the returned JSON and validates it against the declared Person type. All fields must be present and match their expected types.

Example

type Person {
name: string
age: int
email: string
}

let p = generate Person {
prompt: "Generate a fictional software engineer"
}

print("Name:", p.name)
print("Age:", p.age)
print("Email:", p.email)

Error Handling

Mochi validates the LLM’s response against your type. If a required field is missing or the structure doesn't match, you’ll receive a detailed runtime error with a diff.

Prompt Composition and Functions

All prompt text is fully dynamic:

fun describe(topic: string): Book {
generate Book {
prompt: "Generate a book about " + topic
}
}

Runtime Configuration

As in v0.3.0, the LLM backend is defined via environment variables:

VariableDescription
LLM_PROVIDERProvider name (e.g., openai, echo, local)
LLM_DSNConnection string or API base URL
LLM_MODELDefault model name

Structured and text generation both use this shared configuration.

Testing and Snapshot Support

generate blocks integrate with Mochi’s test system. Use expect assertions to snapshot structured output:

test "generate book" {
let book = generate Book {
prompt: "Create a guidebook named 'Mochi Language'"
}

expect book.title == "Mochi Language"
}

Run mochi test --update to re-record the generated output.

Mochi v0.3.1 makes typed programming and generative AI first-class citizens — setting the stage for upcoming agent, stream, and task modeling features in the next release.