Type System

How the type system meets the LLM

Kaya's type system isn't just a compile-time check — it's the contract the model reads and writes against. Beyond the built-in types, the as operator turns a type into an instruction the LLM can satisfy.

The as operator is a first-class construct in Kaya designed for LLM-powered type coercion and classification. It sits above standard type conversion by combining deterministic parsing with semantic intelligence from LLMs.

Primitive conversions run locally. Model-backed examples require a matching admitted model binding and session grant; omitted models need a configured default. Finite classifications can use configured decision routes, while structured transformations use generation. See native model setup. Reference fragments also require their surrounding type and input bindings.

Syntactic Formats

The as operation is highly versatile and supports three distinct syntaxes:

  1. Operator Style:
    let mood = message as Sentiment
  2. Function Style:
    let mood = as(value: message, type: Sentiment)
  3. Method Style:
    let mood = message.as(Sentiment)

The Three Tiers of Coercion

Kaya intelligently routes as calls through three execution tiers depending on the complexity of the target type:

Tier 1: Deterministic Coercion (Local, No LLM)

For built-in primitive types, Kaya performs direct local conversion without invoking an LLM. This provides high-speed, zero-cost type casting.

let numberStr = "42"
let value = numberStr as number       // → 42
let integer = numberStr as int        // → 42 (floored)
let text = 123 as string              // → "123"
let booleanVal = "true" as boolean    // → true
Run in Browser No model needed

Tier 2: LLM-based Classification (Refined Literal Unions)

When the target type is a union of string literals with semantic constraints, Kaya delegates the selection to an LLM. This acts as a highly accurate, zero-boilerplate classification engine.

type UserIntent = "question" + { is: "asking for information or help" }
                | "feedback" + { is: "sharing suggestions or opinions" }
                | "greeting" + { is: "saying hello, hi, or casual greetings" }

let message = "How do I set up custom domain routing?"
let intent = message as UserIntent    // → "question" (LLM semantic classification)
Run in Browser Text model required

Tier 3: LLM-based Transformation (Complex Structured Types)

For object or array types, the LLM translates the source text/structure into a fully conforming, structured instance. This is ideal for robust, structured data synthesis.

type SpanishProfile = {
  name: string + { is: "A Spanish name translation if needed" },
  age: number + { is: "A realistic age" }
}

let bio = "I am John and I was born in 1995."
let profile = bio as SpanishProfile   // → { name: "Juan", age: 31 }
Run in Browser Text model required