# Pragmatic AI With TypeChat

Daniel Rosenwasser, Microsoft

AI Engineer Summit 2023

[Talk page](https://ai.engineer/talks/UNKDAo8YNmg-pragmatic-ai-typechat) · [Video](https://www.youtube.com/watch?v=UNKDAo8YNmg)

## Pragmatic AI With TypeChat

TypeChat connects natural-language requests to application code by using types to guide generation, validate results, and describe programs whose operations remain under application control.

Basic familiarity with TypeScript interfaces, JSON, and function calls will help you follow the examples.

### A rainy-day recommendation needs a dependable shape

[0:17](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=17s)

What should an app do with a request for places to visit on a rainy day in Seattle? Its interface might show a map and a list of recommendations, each with a venue name and description. A conversational answer is useful to a person, but the application needs those pieces as separate fields. This is the gap Daniel Rosenwasser, program manager on TypeScript, introduces: connecting the freedom of a chat interface to the precise data expected by traditional software.

![App mockup asking for three places to visit on a rainy day in Seattle, with a map and entries for Chihuly Garden and Glass, Museum of Flight, and MoPoP.](https://ai.engineer/api/talk-essay-media/a0bf897a6a7a2e06ef29ea88f3aaac1d90d34188669897b928f8cb3ecb9ed6e4)

A rainy-day Seattle query paired with a map and three venue suggestions.

A tempting first implementation asks the model to keep its answer short and put each recommendation on a separate line. After seeing a few responses, a developer notices a pattern: every line begins with a number, followed by a venue name, a colon, and a description. The parser practically writes itself: split on newlines, remove the leading numbers, then split on the colon.

But an observed formatting habit is not an interface contract. The model can change its formatting, and the content itself can contain a delimiter that breaks the parser. More instructions may make the pattern more common; they do not make parsing arbitrary prose dependable.

### JSON supplies syntax; types supply the contract

[3:01](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=181s)

Asking for JSON improves the situation immediately. Give the model an example object containing venue names and descriptions, and it can return something the application can parse without interpreting prose. Yet a parseable response can still contain unexpected properties or omit information the app needs. **Valid JSON is only the beginning of validation.**

![Getting structure slide showing a prompt with venue and description fields beside a JSON response containing Seattle venue suggestions.](https://ai.engineer/api/talk-essay-media/52f87e42b2d58d8b98ca8b57bcfdafae58fe70393e1cafab83f89c9128fc4cfd)

Requesting venue suggestions in JSON using an example shape.

An example also becomes an awkward way to describe a richer contract. Uniform objects are easy to illustrate; optional properties, required properties that sometimes contain `null`, and values that can be either strings or numbers introduce combinations that examples must somehow cover. Types express these distinctions directly:

| Requirement | TypeScript form |
|---|---|
| Property may be absent | `description?: string` |
| Property must exist but may be null | `description: string \| null` |
| Value accepts two alternatives | `value: string \| number` |

These declarations describe the allowed cases without enumerating every possible object.

For the Seattle app, the contract can remain small: a list of objects, each with two string fields. An interface expressing that shape looks like this:

```typescript
interface Venue {
  name: string;
  description: string;
}

interface Recommendations {
  venues: Venue[];
}
```

The model receives the user's intent together with the type definitions the application expects to consume. Those types become guidance for producing the answer. Rosenwasser's qualification is that the model needs sufficient training on both natural language and code; naming a provider or a local model does not establish that every model will perform equally well.

### Use the same types to validate and repair

[5:18](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=318s)

Guiding generation solves only half the problem. The returned data must also satisfy the contract. TypeScript provides a useful route because JSON values can be represented inside a TypeScript program. Under the hood, the library constructs a miniature program that lets the compiler check the candidate against the requested type.

That produces a straightforward validation and repair procedure:

1. Check the generated data against the expected type using the TypeScript compiler.
2. If the check succeeds, return the well-typed data.
3. If it fails, use the compiler diagnostic to ask the model for a repaired answer.

The same contract therefore guides generation and supplies concrete feedback when generation goes wrong.

[TypeChat](https://github.com/microsoft/TypeChat) packages this process into a TypeScript library available through npm. Its usefulness grows with schemas richer than the recommendation list: a coffee order, for example, can describe several kinds of items and the choices available for each. User intent and those types go into translation; structured, checked output comes back.

### Give unsupported requests somewhere to go

[7:12](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=432s)

The live demonstration starts in a cloned TypeChat repository with dependencies installed. Its README orders the examples by increasing complexity, beginning with positive, negative, or neutral sentiment classification. The coffee shop moves beyond that introductory task: its schema describes orders, and its command-line prompt accepts a request for one latte with foam. That request translates successfully.

The next request combines a latte with a medium purple gorilla named Bonsai. Translation still succeeds, but success does not mean the coffee shop has acquired a gorilla. The JSON carries the unsupported phrase through an **unknown-text alternative**, allowing the application to tell the user which part it did not understand. The schema can represent both the recognized order and the unresolved part of the request.

This shifts some of the work from prompt engineering to **schema engineering**. The schema does more than list acceptable products: it defines how the application can receive and handle an incomplete interpretation. An explicit unknown case gives the model a way to preserve unsupported intent instead of forcing every phrase into a menu item.

Rosenwasser describes the shown coffee application code as under 40 lines. Its core wiring creates a model from environment settings, constructs a JSON translator with the type-definition contents and expected type name, then calls `translate` for each request. The recording uses the original `createJsonTranslator(model, schema, typeName)` construction; [TypeChat 0.1.0](https://microsoft.github.io/TypeChat/blog/announcing-typechat-0-1-0/) later changed this to `createJsonTranslator(model, validator)` and moved program helpers to `typechat/ts`. The line-count claim concerns the shown application wiring, not its separate schema or the library underneath it.

Removing the unknown-input alternative has an immediate effect in the editor: the application code that handles that case now produces type errors. After adjusting the example, Rosenwasser requests a cappuccino and a purple gorilla named Bonsai. This time, the result includes a bagel with butter. The altered schema has left the model without an appropriate way to express that part of the request.

**Type correctness does not establish that an answer faithfully represents the user's intent.** A bagel can be a valid menu item while being the wrong interpretation. A schema that anticipates misunderstanding gives the application a recovery path: show what was recognized, expose what remains unclear, and let the user resolve it. The unknown case supports that interaction; it is not a guarantee against every mistaken interpretation.

### A list of commands eventually needs dataflow

[11:09](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=669s)

Structured data can also describe actions. A request to schedule an appointment can translate into a calendar command, and several requested actions can become a list of commands. This is a natural extension of the coffee example: the output now tells the application what to do.

A simple chain works when one operation's output becomes the next operation's input. The scripting slide illustrates a sequence of `readFile`, `trimText`, and `writeFile`. But what happens when an operation needs several arguments, or two later operations need the same earlier result? The representation needs a way to name and reuse values. A command list is becoming a program.

![Scripting slide showing a readFile command, a sequence containing readFile, trimText, and writeFile, and the question What’s wrong with this?](https://ai.engineer/api/talk-essay-media/a40d6f9a0eeecdfd8e20da87f42de412912fe60b69c1c8311520acce4ddedf52)

From a single command to a sequence of read, trim, and write operations.

One approach is to declare the methods the model may call and ask it to generate code using only that API. Execution constraints quickly complicate the idea. To protect availability, the application might forbid loops; it might also exclude lambdas and other constructs. A model trained on full JavaScript or Python can still generate code outside that permitted subset, leaving the application with a rejected program instead of a usable result. Sandboxing and the distinction between synchronous and asynchronous APIs add further concerns.

### Represent the program as JSON

[13:31](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=811s)

TypeChat's experimental approach is to generate a small artificial language encoded as JSON. The API types still guide generation, but the model produces a structured program rather than unrestricted source code. References identify results from prior steps, making those results available as arguments to later operations. Rosenwasser compares this representation to an abstract syntax tree and to static single assignment form: both analogies help explain explicit operations and dependencies.

The JSON program is then used to construct a synthetic TypeScript program in memory. Compiler validation checks both the calls and the connections between them: an operation must be available in the declared API, and a referenced result must have a type accepted by the consuming operation. This goes beyond checking that a command name exists. It checks whether the proposed program fits the API's type relationships. Those checks constrain the program; they do not by themselves establish a complete execution sandbox.

### Let application code perform the arithmetic

[14:28](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=868s)

The calculator demonstration makes the dependency concrete: add one to forty-one, then divide by seven. The addition produces the value needed by the division. The model's job is to describe those operations and their relationship; application code performs the arithmetic. The resulting calculation is `(1 + 41) / 7 = 6`, not evidence that the model itself has become a more accurate calculator.

Rosenwasser describes the shown calculator application code as under 50 lines. It uses a program translator, stops if translation and validation fail, and otherwise passes the validated program to an evaluator. The [math application's evaluator callback](https://github.com/microsoft/TypeChat/blob/main/typescript/examples/math/src/main.ts) acts as an instruction interpreter, dispatching each operation to application code. That dispatch can use an object, a function, or a `switch`.

For the two operations in this request, an application-owned dispatcher can be as small as:

```typescript
type MathOperation = "add" | "divide";

function dispatch(
  operation: MathOperation,
  left: number,
  right: number,
): number {
  switch (operation) {
    case "add":
      return left + right;
    case "divide":
      return left / right;
  }
}

const sum = dispatch("add", 1, 41);
const result = dispatch("divide", sum, 7);
```

In the translated workflow, the evaluator resolves the reference to the addition's result before invoking the division callback. This separates interpreting the request from executing its operations, while allowing tasks richer than a single command.

### Carry the typed approach into Python

[16:04](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=964s)

Other approaches overlap with parts of this design. The capability Rosenwasser emphasizes is validation of both structured JSON results and programs. The next question is whether the same idea can work beyond TypeScript. In the Python experiments, a coffee-order schema again guides interpretation, while a calculator API describes operations as class methods with typed arguments and explanatory comments. Rosenwasser reports encouraging results, without presenting a quantitative benchmark.

![VS Code displaying math_api.py with subtraction, multiplication, division, power, negation, and identity methods beside an idle terminal.](https://ai.engineer/api/talk-essay-media/f4a67861bd2f124a6a85feeff7f49ec71a180940bf20f8fc8ef5f85250494d18)

Python calculator methods with typed arguments and descriptive docstrings.

The Python code view shows calculator methods and their docstrings, illustrating how an API can communicate both permissible operations and their intended meaning. The next example extends that idea from arithmetic to a richer CSV-processing API.

### Compose a data-cleaning request from available operations

[17:10](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=1030s)

The CSV API exposes column access and operations that support filtering and joins, with Boolean selections identifying rows. The closing request is to read a CSV, find values equal to `N/A`, and drop the corresponding rows. The same program-generation idea now connects several data-processing operations: obtain the data, compute a selection, and use that selection to remove rows.

The Python work shown here is explicitly a prototype, not ready for prime time. Even so, it extends the central mechanism from menu items to arithmetic and tabular data: describe the application's vocabulary with types, translate intent into that vocabulary, and validate the result before the application uses it.

Rosenwasser closes by asking engineers to try TypeChat and share what they are building. The practical goal is to bring language models into the precise world of everyday applications, giving developers a familiar contract through which free-form requests can become usable data and executable plans.

## Resources

- [TypeChat](https://github.com/microsoft/TypeChat) — Library and examples for translating natural-language requests into validated structured data.
- [Introducing TypeChat](https://microsoft.github.io/TypeChat/blog/introducing-typechat/) — The original announcement explains type-guided generation, compiler validation and repair.
- [TypeChat example guide](https://microsoft.github.io/TypeChat/docs/examples/) — Setup instructions and an ordered path through sentiment, coffee, calendar and program-generation examples.
- [Schema engineering techniques](https://microsoft.github.io/TypeChat/docs/techniques/) — Practical schema guidance, including explicit ways to represent requests outside an application's domain.
- [Math example implementation](https://github.com/microsoft/TypeChat/blob/main/typescript/examples/math/src/main.ts) — Application code that translates requests into programs and dispatches arithmetic operations through an evaluator callback.
- [Pypechat](https://github.com/DanielRosenwasser/Pypechat) — Daniel Rosenwasser's Python prototype with coffee, math and CSV examples.
- [TypeChat 0.1.0 API changes](https://microsoft.github.io/TypeChat/blog/announcing-typechat-0-1-0/) — Explains validator-based translator construction, Zod support and changed import locations introduced after the talk.

## Slides

### Chat interface

A chat interface alternates messages in a continuous flow of information. The speaker contrasts this with traditional apps, which need more precise data.

[0:47](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=47s) [Diagram of alternating user and assistant messages in a chat interface.](https://assets.ai.engineer/sites/aiecode2025/sha256/14b6bed34f598e876c1518dadc0d5069d4ae75197e247a6b1c9bcfbb984970f9.webp)

AIE

DR
⌘
DR
⌘

AutoGPT
smol ai

Interface text:
SUMMIT

### Rainy-day places in Seattle

An example app pairs a rainy-day question with three venue names and descriptions beside a map, illustrating the precise data a traditional app needs.

[1:10](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=70s) [Example app lists Chihuly Garden and Glass, Museum of Flight, and MoPoP beside a map.](https://assets.ai.engineer/sites/aiecode2025/sha256/62d2478de117dca3d897654cc22a2b5468d065b497f8155c1836df336f7e27c1.webp)

What are 3 places I can check out on a rainy day in Seattle?

Chihuly Garden and Glass
Do you like glass? Do you like gardens? Boy do we have...

Museum of Flight
Have you ever wondered how we get planes up in the air?

MoPoP
Do you like weird swirly buildings made of metal? Check this...

### The problem

A request for three quick suggestions gets a long response. Its numbered entries and colon-separated headings look easy to parse, but the speaker warns that an LLM may not keep that format.

[1:58](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=118s) [Slide contrasts natural-language output with code needs and shows a lengthy chat response to a request for brief Seattle suggestions.](https://assets.ai.engineer/sites/aiecode2025/sha256/a97fca506fe05f0b0d4e93b97cb54f10115b04dd718a51062de0741e1ca2b91d.webp)

The problem

LLMs need to be pampered to give reasonable answers

LLMs generate natural language by default.
• Great for people
• Not for code

It's rainy in Seattle (surprise!). Got 3 quick suggestions for what I should do this weekend? Keep it short, I'm impatient.

1. Visit a Museum: Seattle has a wide range of museums to explore, such as the Museum of Pop Culture (MoPOP), Seattle Art Museum, or the Museum of Flight. These indoor attractions offer a fascinating way to spend a rainy day while immersing yourself in art, culture, or history.
2. Try Local Coffee Shops: Seattle is renowned for its coffee culture. Take advantage of the rainy weather by cozying up in one of the city's numerous coffee shops. Grab a delicious cup of java, relax with a book or your favorite device, and enjoy the comforting ambiance while watching the raindrops fall.
3. Indulge in Local Cuisine: Seattle is a foodie's paradise, and rainy days provide the perfect opportunity to savor some of the city's diverse culinary offerings. Explore Pike Place Market or one of Seattle's many neighborhoods to find cozy restaurants and cafes serving everything from fresh seafood to global cuisine. Treat your taste buds to a delightful culinary adventure while staying dry indoors.

Interface text:
DR

[2:05](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=125s) [A slide contrasts natural-language responses with code, highlighting the prompt, numbered items, and separators in a chat response.](https://assets.ai.engineer/sites/aiecode2025/sha256/904796f20a78bc2d222993858e90c3f8f6fab8d57f30bcc6030edecc3d8c0f8e.webp)

The problem

LLMs need to be pampered to give reasonable answers

LLMs generate natural language by default.
• Great for people
• Not for code

It's rainy in Seattle (surprise!). Got 3 quick suggestions for what I should do this weekend? Keep it short, I'm impatient.

1. Visit a Museum: Seattle has a wide range of museums to explore, such as the Museum of Pop Culture (MoPOP), Seattle Art Museum, or the Museum of Flight. These indoor attractions offer a fascinating way to spend a rainy day while immersing yourself in art, culture, or history.
2. Try Local Coffee Shops: Seattle is renowned for its coffee culture. Take advantage of the rainy weather by cozying up in one of the city's numerous coffee shops. Grab a delicious cup of java, relax with a book or your favorite device, and enjoy the comforting ambiance while watching the raindrops fall.
3. Indulge in Local Cuisine: Seattle is a foodie's paradise, and rainy days provide the perfect opportunity to savor some of the city's diverse culinary offerings. Explore Pike Place Market or one of Seattle's many neighborhoods to find cozy restaurants and cafes serving everything from fresh seafood to global cuisine. Treat your taste buds to a delightful culinary adventure while staying dry indoors.

Interface text:
DR

### Getting structure

The prompt supplies an example JSON shape, and the model returns venue and description fields for three Seattle attractions. The speaker cautions that JSON alone does not guarantee the data an app needs.

[3:04](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=184s) [A prompt requests Seattle suggestions in JSON, alongside an example JSON response.](https://assets.ai.engineer/sites/aiecode2025/sha256/ca6086c0f9c08917b7edc33eebda96d889d46b2e2830f03c6a9dab8f271ca380.webp)

Getting structure

LLMs can respond in JSON!

Provide 3 suggestions for specific places to go to in Seattle on a rainy day. Respond in the form of JSON. The JSON should have the following format

[
  { venue: "...", description: "..." },
  { venue: "...", description: "..." },
]

[
  {
    "venue": "Seattle Art Museum",
    "description": "The Seattle Art Museum (SAM) is a world-class museum[illegible]
  },
  {
    "venue": "Chihuly Garden and Glass",
    "description": "Chihuly Garden and Glass is an extraordinary exhibit[illegible]
  },
  {
    "venue": "Museum of Pop Culture",
    "description": "The Museum of Pop Culture (MoPOP) is a vibrant museu[illegible]
  }
]

Interface text:
DR
json
Copy code

### Types!

TypeScript interfaces describe a Result containing an array of Location objects, each with venue and description strings. The speaker proposes using these types to guide the model.

[4:17](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=257s) [TypeScript interfaces define a result containing locations with venue and description strings.](https://assets.ai.engineer/sites/aiecode2025/sha256/736651008cce048f8f18d00e309de4c0caa11807d338d3fdb4035af28df32c38.webp)

Types!

export interface Result {
    items: Location[];
}

export interface Location {
    venue: string;
    description: string;
}

### Types can guide the model

The prompt gives an AI service a TypeScript response type with three allowed values. The illustrated JSON response selects “happy.”

[4:37](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=277s) [A slide says that types can guide the model.](https://assets.ai.engineer/sites/aiecode2025/sha256/fe3c62fb8eb1184f9a529c1ffb7bd6b164b098ba21dc29c4d2e182b0a27bc629.webp)

Types can guide
the the model.

Interface text:
SUMMIT

[4:44](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=284s) [A TypeScript response type guides an AI service to produce a JSON sentiment value.](https://assets.ai.engineer/sites/aiecode2025/sha256/cadfe5912c32af997718221a8dbb1527e636b83ca80ae5adc29dd37cd9d5fdea.webp)

Types can guide
the the model.

Please summarize the following sentence:

"Hello world!"

Respond in JSON that satisfies the following type.

export interface Response {
    data: "happy" | "sad" | "neutral";
}

⌘ CoolAI Service

{
    "data": "happy"
}

### Types can guide the model

The prompt pairs “Hello world!” with a TypeScript response type that limits data to three sentiment values. The illustrated AI service returns JSON with “happy”.

[4:47](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=287s) [A TypeScript response type guides an AI service to produce a JSON sentiment value.](https://assets.ai.engineer/sites/aiecode2025/sha256/20e34c23c2aa39e77897f2e7015cbd635dcfd37e26736e4f4eaf6658c0aea995.webp)

Types can guide the the model.

Please summarize the following sentence:

"Hello world!"

Respond in JSON that satisfies the following type.

export interface Response {
    data: "happy" | "sad" | "neutral";
}

↓

⌘ CoolAI Service

↓

{
    "data": "happy"
}

### Types can validate the results

TypeScript checks the response against its type. Compiler errors can be sent back to the model to request a repair.

[6:07](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=367s) [A typed response passes a TypeScript check beside the statement that types are all you need.](https://assets.ai.engineer/sites/aiecode2025/sha256/7575d1d5bdf1ca0530a89ea2b95104a7b894725014c59a8328786a5786953400.webp)

Types can validate the results.

Types are all you need.

export interface Response {
    data: "happy" | "sad" | "neutral";
}

export const data: Response = {
    "data": "happy"
}

TypeScript

Interface text:
SU

### TypeChat

TypeChat is a TypeScript library that bundles guidance and validation to help produce well-typed data from language models. Install it with npm install typechat.

[6:29](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=389s) [TypeChat title and npm installation command.](https://assets.ai.engineer/sites/aiecode2025/sha256/bcabb7cffc399ce368f23244d31cb2d82ab4488121b8d25514bea4e819db9230.webp)

TypeChat

npm install typechat

### Coffee order to typed JSON

TypeChat combines a coffee shop’s TypeScript schema with a customer’s order to produce typed JSON: one line item for a tall latte and another for a grande.

[6:52](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=412s) [A coffee order and TypeScript interfaces alongside JSON for two differently sized lattes.](https://assets.ai.engineer/sites/aiecode2025/sha256/a927c432d90a0d03f83161d93df702598bcada2c726214abbda1ebd335e95a0f.webp)

Please summarize the following order:

“2 lattes, 1 tall and 1 grande”

Respond in JSON that satisfies the following type.

export interface Cart {
    items: LineItem[];
}

export interface LineItem {
    type: "lineitem",
    product: Product;
    quantity: number;
}

export type Product =
    | BakeryProducts
    | LatteDrinks
    | EspressoDrinks
    | CoffeeDrinks;

// ...

Quick Demo

{
  "items": [
    {
      "type": "lineitem",
      "product": {
        "type": "LatteDrinks",
        "name": "latte",
        "size": "tall"
      },
      "quantity": 1
    },
    {
      "type": "lineitem",
      "product": {
        "type": "LatteDrinks",
        "name": "latte",
        "size": "grande"
      },
      "quantity": 1
    }
  ]
}

### TypeChat examples

The TypeChat README lists examples from sentiment classification to a coffee shop and calendar. The speaker moves from the basic sentiment example to the coffee shop demo.

[7:09](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=429s) [VS Code displays a README listing TypeChat examples, including sentiment, coffee shop, and calendar.](https://assets.ai.engineer/sites/aiecode2025/sha256/42325f4460759cfb15a394cb1503446e065b0a184e8461b208c2405f406782e2.webp)

To see TypeChat in action, check out the examples found in this directory.

Each example shows how TypeChat handles natural language input, and maps to validated JSON as output. Most example inputs run on both GPT 3.5 and GPT 4. We are working to reproduce outputs with other models. Generally, models trained on both code and natural language text have high accuracy.

We recommend reading each example in the following order.

Name    Description
Sentiment    A sentiment classifier which categorizes user input as negative, neutral, or positive. This is TypeChat's "hello world!"
Coffee Shop    An intelligent agent for a coffee shop. This sample translates user intent is translated to a list of coffee order items.
Calendar    An intelligent scheduler. This sample translates user intent into a sequence of actions to modify a calendar.
An intelligent agent for taking orders at a restaurant. Similar to the coffee shop example, but uses a more complex schema to model more complex linguistic input. The prose files illustrate

Interface text:
TypeChat - Preview README.md
EXPLORER
OPEN EDITORS
Preview READM...
TYPECH...
examples
calendar
coffeeShop
.vscode
dist
src
coffeeShopSche...
input.txt
input2.txt
main.ts
tsconfig.json
package.json
README.md
math
music
OUTLINE
TIMELINE
main
Offset: 1266
Search
2:13 PM
10/10/2023

### Coffee shop schema

The coffee shop schema defines a cart as a list of line items or unknown text. Each line item has a product and quantity; unknown text preserves order input that matches nothing else.

[7:49](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=469s) [VS Code displays TypeScript definitions for a cart, unknown text, line items, and products.](https://assets.ai.engineer/sites/aiecode2025/sha256/31ae533d5215340b09fb6980faf411a11bf7182318811befe25e735c68cebd98.webp)

// The following is a schema definition for ordering lattes.

export interface Cart {
    items: (LineItem | UnknownText)[];
}

// Use this type for order items that match nothing else
export interface UnknownText {
    type: "unknown",
    text: string; // The text that wasn't understood
}

export interface LineItem {
    type: "lineitem",
    product: Product;
    quantity: number;
}

export type Product = BakeryProducts | LatteDrinks | EspressoDrinks | CoffeeDrinks;

export interface BakeryProducts {
    type: "BakeryProducts";

Interface text:
TypeChat - coffeeShopSchema.ts
EXPLORER
OPEN EDITORS
Preview README.md
coffeeShopSchema.ts
examples > coffeeShop > src > coffeeShopSchema.ts
examples
calendar
coffeeShop
.vscode
dist
src
coffeeShopSche...
input.txt
input2.txt
main.ts
tsconfig.json
package.json
README.md
math
OUTLINE
TIMELINE
main
Ln 5, Col 2
Spaces: 4
UTF-8
CRLF
TypeScript
5.3.0-dev.20231009
Offset: 130
Search
2:13 PM
10/10/2023

### Recovering unknown text

The demo parses the latte into a line item and preserves the rest as unknown text. The app then reports the unrecognized phrase, even though the prompt succeeded.

[8:53](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=533s) [The terminal shows a parsed latte and an unknown-text item, followed by a message identifying the unrecognized text.](https://assets.ai.engineer/sites/aiecode2025/sha256/4ce0df6b8f81c739d89e341c1b7d52e8b28c8971a5e8d1212c80cb9c4a221485.webp)

// The following is a schema definition for ordering latte[illegible]

export interface Cart {
    items: (LineItem | UnknownText)[];
}

// Use this type for order items that match nothing else
export interface UnknownText {
    type: "unknown",
    text: string; // The text that wasn't understood
}

export interface LineItem {
    type: "lineitem",
    product: Product;
    quantity: number;
}

export type Product = BakeryProducts | LatteDrinks | Espre[illegible]

export interface BakeryProducts {
    type: "BakeryProducts";

Terminal:
☕> 1 latte and a medium purple gorilla named bonsai
{
  "items": [
    {
      "type": "lineitem",
      "product": {
        "type": "LatteDrinks",
        "name": "latte",
        "size": "grande"
      },
      "quantity": 1
    },
    {
      "type": "unknown",
      "text": "and a medium purple gorilla named bonsai"
    }
  ]
}
I didn't understand the following:
and a medium purple gorilla named bonsai
☕>

Interface text:
TypeChat - coffeeShopSchema.ts
Preview README.md
coffeeShopSchema.ts
examples > coffeeShop > src > coffeeShopSchema.ts > Cart
volta-shim - coffeeShop
Ln 5, Col 2  Spaces: 4  UTF-8  CRLF  TypeScript  5.3.0-dev.20231009  Offset: 130
2:14 PM  10/10/2023
AIE
AUTOGPT
smol ai

### Unknown-text result close-up

The order parses successfully: the latte becomes a line item, while UnknownText preserves the unrecognized phrase so the app can report it back.

[8:58](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=538s) [Close-up of the coffee shop schema and the parsed result containing an unknown-text item.](https://assets.ai.engineer/sites/aiecode2025/sha256/39d88ae77c8095bc0b0ba437370cf208106442fcf7205c07f8f563c3eca3c83b.webp)

// The following is a schema definition for ordering latte[illegible]

export interface Cart {
    items: (LineItem | UnknownText)[];
}

// Use this type for order items that match nothing else
export interface UnknownText {
    type: "unknown",
    text: string; // The text that wasn't understood
}

export interface LineItem {
    type: "lineitem",
    product: Product;
    quantity: number;
}

export type Product = BakeryProducts | LatteDrinks | Espre[illegible]

export interface BakeryProducts {
    type: "BakeryProducts";

1 latte and a medium purple gorilla named bonsai
{
  "items": [
    {
      "type": "lineitem",
      "product": {
        "type": "LatteDrinks",
        "name": "latte",
        "size": "grande"
      },
      "quantity": 1
    },
    {
      "type": "unknown",
      "text": "and a medium purple gorilla named bonsai"
    }
  ]
}
I didn't understand the following:
and a medium purple gorilla named bonsai

Interface text:
TypeChat - coffeeShopSchema.ts
Preview README.md
coffeeShopSchema.ts
examples > coffeeShop > src > coffeeShopSchema.ts > Cart
volta-shim - coffeeShop
main
Ln 5, Col 2
Spaces: 4
UTF-8
CRLF
TypeScript
5.3.0-dev.20231009
Offset: 130
Search
2:14 PM
10/10/2023

### Handling unknown text

The UnknownText schema preserves the unrecognized part of the order, allowing the app to report it while retaining the latte as a line item.

[9:17](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=557s) [The UnknownText interface is highlighted beside the result that uses it.](https://assets.ai.engineer/sites/aiecode2025/sha256/af25cd525dc43195873bc283ac2e838f8986998134f7285426ad1e2ed2f0c448.webp)

// The following is a schema definition for ordering latte[illegible]

export interface Cart {
    items: (LineItem | UnknownText)[];
}

// Use this type for order items that match nothing else
export interface UnknownText {
    type: "unknown",
    text: string; // The text that wasn't understood
}

export interface LineItem {
    type: "lineitem",
    product: Product;
    quantity: number;
}

export type Product = BakeryProducts | LatteDrinks | Espre[illegible]

export interface BakeryProducts {
    type: "BakeryProducts";

1 latte and a medium purple gorilla named bonsai
{
  "items": [
    {
      "type": "lineitem",
      "product": {
        "type": "LatteDrinks",
        "name": "latte",
        "size": "grande"
      },
      "quantity": 1
    },
    {
      "type": "unknown",
      "text": "and a medium purple gorilla named bonsai"
    }
  ]
}
I didn't understand the following:
and a medium purple gorilla named bonsai

AIE
AI Engineer SUMMIT
FIXIE
supab[illegible]

Interface text:
TypeChat - coffeeShopSchema.ts
Preview README.md
coffeeShopSchema.ts
examples > coffeeShop > src > coffeeShopSchema.ts > ...
volta-shim - coffeeShop
Ln 6, Col 1 (171 selected)  Spaces: 4  UTF-8  CRLF  TypeScript  5.3.0-dev.20231009  Offset: 132
2:14 PM  10/10/2023

[9:22](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=562s) [A TypeScript schema includes an unknown-text type beside a terminal showing a partially understood order.](https://assets.ai.engineer/sites/aiecode2025/sha256/09a09478ec8324d17d93895731e499ccbb4f43c9724dd5495fb045c9a1b5c2f0.webp)

// The following is a schema definition for ordering latte[illegible]

export interface Cart {
    items: (LineItem | UnknownText)[];
}

// Use this type for order items that match nothing else
export interface UnknownText {
    type: "unknown",
    text: string; // The text that wasn't understood
}

export interface LineItem {
    type: "lineitem",
    product: Product;
    quantity: number;
}

export type Product = BakeryProducts | LatteDrinks | Espre[illegible]

export interface BakeryProducts {
    type: "BakeryProducts";

1 latte and a medium purple gorilla named bonsai
{
  "items": [
    {
      "type": "lineitem",
      "product": {
        "type": "LatteDrinks",
        "name": "latte",
        "size": "grande"
      },
      "quantity": 1
    },
    {
      "type": "unknown",
      "text": "and a medium purple gorilla named bonsai"
    }
  ]
}
I didn't understand the following:
and a medium purple gorilla named bonsai

AIE
AI Engineer SUMMIT
FIXIE
supabase

Interface text:
TypeChat - coffeeShopSchema.ts
Preview README.md
coffeeShopSchema.ts
examples > coffeeShop > src > coffeeShopSchema.ts > ...
volta-shim - coffeeShop
Ln 6, Col 1 (171 selected)  Spaces: 4  UTF-8  CRLF  TypeScript  5.3.0-dev.20231009  Offset: 132
2:14 PM  10/10/2023

### Removing the unknown-text type

The cart schema now permits only LineItem entries, while the terminal still shows the earlier result: a latte plus unknown text for the purple gorilla.

[9:31](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=571s) [The cart schema contains only line items while the terminal retains the previous unknown-text result.](https://assets.ai.engineer/sites/aiecode2025/sha256/f813e51e659a1849beda0b2e830a299d3a32907cb03b09cc45ead95f2c7ee266.webp)

// The following is a schema definition for ordering latte[illegible]

export interface Cart {
    items: (LineItem)[];
}

export interface LineItem {
    type: "lineitem",
    product: Product;
    quantity: number;
}

export type Product = BakeryProducts | LatteDrinks | Espre[illegible]

export interface BakeryProducts {
    type: "BakeryProducts";
    name: "apple bran muffin" | "blueberry muffin" | "lemo[illegible]
    options: (BakeryOptions | BakeryPreparations)[];
}

export interface BakeryOptions {
    type: "BakeryOptions";

Terminal:
☕> 1 latte and a medium purple gorilla named bonsai
{
  "items": [
    {
      "type": "lineitem",
      "product": {
        "type": "LatteDrinks",
        "name": "latte",
        "size": "grande"
      },
      "quantity": 1
    },
    {
      "type": "unknown",
      "text": "and a medium purple gorilla named bonsai"
    }
  ]
}
I didn't understand the following:
and a medium purple gorilla named bonsai
☕>

AIE
AI Engineer SUMMIT
AUTOGPT
smol ai

Interface text:
TypeChat - coffeeShopSchema.ts
Preview README.md
coffeeShopSchema.ts
examples > coffeeShop > src > coffeeShopSchema.ts > Cart > items
volta-shim - coffeeShop
Ln 4, Col 21  Spaces: 4  UTF-8  CRLF  TypeScript  5.3.0-dev.20231009  Offset: 109
2:15 PM  10/10/2023

### TypeChat coffee shop demo

The demo loads a cart schema into a JSON translator. When the speaker asks for a cappuccino and a purple gorilla named Bonsai, the result contains a cappuccino and a bagel with butter.

[9:43](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=583s) [TypeScript code creates a language model, loads a schema, and initializes a JSON translator for a cart.](https://assets.ai.engineer/sites/aiecode2025/sha256/ce52fbe272b18b72de12b083f855582fb16053484d3a0e42a64ffee0e1fbd709.webp)

import fs from "fs";
import path from "path";
import dotenv from "dotenv";
import { createLanguageModel, createJsonTranslator, processRequests } from "typechat";
import { Cart } from "./coffeeShopSchema";

// TODO: use local .env file.
dotenv.config({ path: path.join(__dirname, "../../../.env") });

const model = createLanguageModel(process.env);
const schema = fs.readFileSync(path.join(__dirname, "coffeeShopSchema.ts"), "utf8");
const translator = createJsonTranslator<Cart>(model, schema, "Cart");

function processOrder(cart: Cart) {
    // Process the items in the cart
    void cart;
}

// Process requests interactively or from the input file specified on the command line
processRequests("☕> ", process.argv[2], async (request) => {
    const response = await translator.translate(request);
    if (!response.success) {

Interface text:
TypeChat - main.ts
Preview README.md
coffeeShopSchema.ts M
main.ts 3
examples > coffeeShop > src > main.ts > translator
main*  3  0
Ln 12, Col 67 (4 selected)  Spaces: 4  UTF-8  CRLF  TypeScript  5.3.0-dev.20231009  Offset: 511
2:15 PM  10/10/2023

[10:27](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=627s) [TypeScript code beside a terminal showing a cappuccino and a bagel with butter, followed by Success.](https://assets.ai.engineer/sites/aiecode2025/sha256/5a7c4f75f4878c3d2682d48420e6b13c5f9c046ff59d72437c3de351880c80ed.webp)

    void cart;
}

// Process requests interactively or from the input file s[illegible]
processRequests("☕> ", process.argv[2], async (request) = [illegible]
    const response = await translator.translate(request);
    if (!response.success) {
        console.log(response.message);
        return;
    }
    const cart = response.data;
    console.log(JSON.stringify(cart, undefined, 2));
    // if (cart.items.some(item => item.type === "unknown"[illegible]
    //     console.log("I didn't understand the following:[illegible]
    //     for (const item of cart.items) {
    //         if (item.type === "unknown") console.log(it[illegible]
    //     }
    //     return;
    // }
    processOrder(cart);
    console.log("Success!");

Terminal output:
    {
      "type": "lineitem",
      "product": {
        "type": "LatteDrinks",
        "name": "cappuccino"
      },
      "quantity": 1
    },
    {
      "type": "lineitem",
      "product": {
        "type": "BakeryProducts",
        "name": "bagel",
        "options": [
          {
            "type": "BakeryOptions",
            "name": "butter"
          }
        ]
      },
      "quantity": 1
    }
  ]
}
Success!
☕>

Interface text:
TypeChat - main.ts
README.md
coffeeShopSchema.ts M
main.ts M
examples > coffeeShop > src > main.ts > processRequests("☕> ") callback
volta-shim - coffeeShop
main*  0  0
Ln 24, Col 16  Spaces: 4  UTF-8  CRLF  TypeScript  5.3.0-dev.20231009  Offset: 908
2:16 PM  10/10/2023

### Planning and Programs

The talk moves from translating a coffee order into structured data to asking how TypeChat might script an application with richer commands.

[11:11](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=671s) [A white slide titled “Planning” and Programs, with a faint “Scripting”? heading near the top.](https://assets.ai.engineer/sites/aiecode2025/sha256/c139166afacfb6d026b510f2ee741e64b66ed3a19f3db33e333b8154ac77341b.webp)

“Scripting”?

“Planning” and Programs

### “Scripting”?

TypeChat can generate a sequence of commands, but the list leaves data flow implicit. Passing multiple arguments or reusing a result suggests a need for variables.

[11:30](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=690s) [A slide says TypeChat can generate commands and gives a readFile command example.](https://assets.ai.engineer/sites/aiecode2025/sha256/1bf40a2630c2930e1bb2dc1ea65b7f7949cbc469b2118bb4d2e3a6ab230a7679.webp)

“Scripting”?

• Can easily have TypeChat generate “commands”
  • { "commandName": "readFile", "fileName": "..." }

[11:48](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=708s) [A TypeChat command and a sequence of read, trim, and write commands, followed by the question of what is wrong with the sequence.](https://assets.ai.engineer/sites/aiecode2025/sha256/1ba2cf651892879a216aca13ea7b6bf3efa033e7c6daa118652255ebcc82c195.webp)

“Scripting”?

• Can easily have TypeChat generate “commands”
  • { "commandName": "readFile", "fileName": "..." }

• Can easily sequence them...
  • [
      { "commandName": "readFile", "fileName": "..." },
      { "commandName": "trimText" },
      { "commandName": "writeFile", "fileName": "..." }
    ]

• What’s wrong with this?

### Just Generate Code?

The proposed code uses variables to pass a file’s contents through trimText and into writeFile, making the data flow between API calls explicit.

[12:20](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=740s) [TypeScript code reads a file, trims its contents, and writes the result through an API.](https://assets.ai.engineer/sites/aiecode2025/sha256/083949059cc7709c128f636567644a27bd86e2ebf080b0dbf70a0faf8f2f6605.webp)

Just Generate Code?

function foo(api: API) {
    let input1 = api.readFile("...");
    let input2 = api.trimText(input1);
    let input3 = api.writeFile("...", input2);
}

### …from an API?

The proposed interface gives generated code three callable methods: read a file, trim text, and write a file. The speaker asks whether types alone can define which calls the code may make.

[12:30](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=750s) [A TypeScript API interface defines readFile, trimText, and writeFile methods.](https://assets.ai.engineer/sites/aiecode2025/sha256/a99a76dc0a38dc6f56b3360f7e59897bca9bffd1d0904b7e714757d53e76bb47.webp)

...from an API?

export interface API {
    /**
     * Reads a file at the given path,
     * @returns a UTF-8-parsed string.
     */
    readFile(fileName: string): string;

    /** Trims leading and trailing whitespace from the input. */
    trimText(text: string): string;

    /** Writes input text to a file at the given path. */
    writeFile(fileName: string, text: string): void;
}

### Challenges

Restricting generated code to a language subset can fail when models stray outside it. Sandboxing and handling synchronous versus asynchronous APIs remain challenges.

[12:44](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=764s) [Challenges include language-specific subsets, sandboxing, and exposing asynchronous versus synchronous behavior.](https://assets.ai.engineer/sites/aiecode2025/sha256/6d7fa6a49c87e7ee484d394bba92127a36f79fdc74aec88a1461ab008bd1a3e1.webp)

Challenges

• Language-specific
  • And you have to carve out a language subset
  • And language models tend to draw outside the lines

• Hard to sandbox

• Forced to expose async vs. sync

Interface text:
SUM

## Full transcript

[0:00](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=0s) [upbeat music] Good afternoon, my AI engineering friends.

[0:17](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=17s) How are we all feeling today? Great. [cheering] There we go. We got some energy even post-lunch. All right, you heard. I'm Daniel Rosenwasser. I'm the program manager on TypeScript, as well as a new little experimental library I'm here to talk about today called TypeChat.

[0:32](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=32s) Now, this is an AI engineering conference. Everybody here has used something like ChatGPT, right? We use it for this continuous flow of information. We've been able to prototype things with it, just get useful answers just by having this adorable little chat interface, right?

[0:49](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=49s) Um, but that's just one end of the spectrum. And on the other end of the spectrum, we have our traditional apps. These apps that are looking for this more precise sort of data to work with.

[0:59](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=59s) So the question is, how do we make all of the new AI tools, all these language models that are so powerful, accessible to every engineer out there? And so just to start things off, um, what if we had this cute, you know, this little app right here.

[1:14](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=74s) You have some basic user input at the very top, followed by these items, and each of these items has a venue name and description. So this just helps me figure out what I need to do on a rainy day in Seattle, because this is every day in Seattle for me.

[1:27](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=87s) Um, a lot of weather apps at this conference. But

[1:31](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=91s) the, the problem that you may find with trying to bridge together these language models and these traditional apps is that you find that you need to sort of massage the data.

[1:42](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=102s) You need to sort of like really, really, really pamper the models to give you what you're looking for. And even after all that's said and done, by default, these apps will give you natural language, which is great for people, but it's not great for code.

[1:59](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=119s) So if you just prototype this in, you know, something like a chat view, maybe you'd actually use the playground to do this. You would find yourself saying certain things to pamper, like keep it short and do this, and put everything on its different line and do whatever.

[2:13](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=133s) Um, you might find that you're starting to glom onto the patterns of what the language model gives you because you've seen it in a certain way, right? And you've noticed, oh, well, it gives me this format.

[2:23](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=143s) Each of these things is on its own line. Um, each of the lines has a leading number. They are always separating the venue name by the des-- and the description by a colon.

[2:31](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=151s) So I'll just do some basic parsing, split by new line, remove the trailing, the leading numbers, and then, uh, split on the colon. Um, that is a disaster [chuckles] waiting to happen because you can't rely on the language model to always do this.

[2:46](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=166s) And you can't know whether or not you're going to have something in the middle of that input that is going to just sort of wreck your parsing strategy, right?

[2:54](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=174s) Parsing natural language is extremely hard, if not a fool's errand for most people.

[3:01](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=181s) The thing that many people at this conference and elsewhere have discovered is you can say, "Pretty, pretty please give me some JSON." And it works pretty well, right? You know, just, here's an example of what I'm expecting.

[3:15](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=195s) Please respond with the answer. And voila, it comes right back. But there's two issues with this. One is just doing that on its own is not enough to guarantee that your app is actually gonna get the data it's looking for, um, because maybe there's an extra property that doesn't seem to align.

[3:34](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=214s) Maybe there's not enough data in the actual response, so you need to do some level of validation. Um, but not just that. You can't comprehensively describe all of the things that you want practically.

[3:47](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=227s) In this case, I have really, really simple schema or really, really simple like example. All the objects are uniform. They all have the same properties. End of story, right?

[3:58](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=238s) But what if something is optional? What if something is required but needs to be null in some cases? What if this could be a string or a number, but never something else?

[4:07](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=247s) I don't know. So you will not be able to get that far for more complex examples because you end up with this combinatorial explosion. So what we found is that you can use types.

[4:20](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=260s) Types are this great description to actually guide the model. Here, I'm just using type definitions in TypeScript. These are just plain interfaces. All I want is a thing with a list, and the list has these objects, and the objects have these two properties that are both strings on them.

[4:36](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=276s) And the beauty of these type definitions is that the types can guide the model, right? So you can actually use these types to tell a model, "Hey, here's some user input.

[4:48](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=288s) Here's a user intent. Now use this with the types that I'm g- actually gonna use in my application. Throw it through your cool AI service," whatever that is. That may be OpenAI, Cohere, uh, Anthropic.

[5:01](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=301s) Maybe it's a local model. Maybe it's Llama code. I don't know. But the, the point is, what we found is that if you use an, a language model that is sufficiently trained on both human prose, natural language, and code, this actually bridges the two worlds together.

[5:18](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=318s) But like I said, the guidance is not-- it's only half of the problem, right? You need to be able to actually validate what you're getting. And that's the key insight is that the types can also validate the results.

[5:30](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=330s) And so what we found is, in our experience, you know, we're using TypeScript. TypeScript's great for JSON because it's a superset of JavaScript, which is a superset of JSON, which means that you can actually construct a miniature little program that underneath the hood, the TypeScript compiler is using to do that validation.

[5:48](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=348s) And if that all goes well, then great. You have well-typed data from your language model. And if it doesn't go well, well, underneath the covers, what we actually end up with is an error message, right?

[6:01](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=361s) Because it's actually using the TypeScript compiler under the hood.

[6:04](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=364s) That error message can be used to perform a repair when you are reaching out to a language model to say, "No, no, no, no, no, that's not what I wanted.

[6:13](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=373s) Try again." And so the key insight is types are all you need. Types can actually guide and validate, and it becomes a very powerful model because... Whoops.

[6:27](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=387s) Well, yes, actually. [laughing] That's a key insight that we have with TypeChat. It's an n- it's a library on npm right now. It's a TypeScript library at the moment. Um, and basically we've bundled this all together and make it easy to just guide a language model, perform these, uh, queries, and actually, like, make sure that you're actually getting

[6:46](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=406s) well-typed, um, data from the language models. And so you can actually use much more complex examples as well. You might say, like, "I have a coffee shop, and the coffee shop has this schema, these types."

[6:58](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=418s) You define them like this. And basically you can use that to combine that with a user intent and input, and you get well-typed output. And I'll actually demo that right now.

[7:12](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=432s) Um, what I have here is my, you know, the TypeChat repository cloned, npm installed, everything's set up, and we have an examples directory. And I think if you're just curious to get started with, um, TypeChat, the examples directory gets you started.

[7:29](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=449s) We have a table. If you look at the README, we have a table of all of our examples. They kind of increase in, in complexity and difficulty, and the first one is, like, a sentiment thing where we say if something is positive, negative, or neutral.

[7:42](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=462s) Um, but that's so basic, it's like our hello world. I actually wanna go back to that coffee shop example that I showed you just now. So

[7:51](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=471s) we have this coffee shop schema, and this is just a bunch of types, right? You probably have something similar in your preferred language as well.

[8:01](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=481s) Um, and what I can do here is I'm just gonna run

[8:06](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=486s) our entry point, and from the command prompt, I actually have a little prompt, and I can actually just make orders here. So I can say, "One latte with foam, please."

[8:17](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=497s) Ta-da. Right? I- [cheering] Yeah. [clapping] So, you know, it's, it's, it's... This is the key thing is that it's actually so simple, and it actually just works very well in a surprising way.

[8:35](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=515s) Um, now that's... I could, I could just tell you about that and I could walk off, and that's not really good enough, I know. Um, what happens if I say one latte and a medium purple gor- purple gorilla named Bonsai?

[8:54](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=534s) So what actually happened here is technically when we ran this prompt, this thing succeeded. But even though we got a successful result, we were able to do this sort of recovery here.

[9:08](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=548s) We actually in our app are able to say, "I didn't understand the following, a medium purple gorilla named Bonsai." And that actually showed up in the JSON. And the reason that it did is because we have this thing called unknown text.

[9:21](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=561s) So we've started to see these patterns in that instead of doing this sort of prompt engineering, you're doing schema engineering. You're able to sort of thread through these results, uh, into your app, because if you actually, you know, remove this stuff...

[9:35](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=575s) And let me show you what this actually looks like. If you look at the coffee shop example, this is under 40 lines of code, right? The magic here actually comes from, we create a model, we infer it based on your environment settings, um, and then the actual magic is that we have this JSON translator.

[9:52](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=592s) You give us the contents of your types, you select the type that you're expecting, and then every single time you need to translate the user intent, you just run this translate function.

[10:02](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=602s) Now I'm getting type errors because I removed the type and it's telling me, like, this will never happen. Whoops. Not that.

[10:08](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=608s) So if I rerun this thing and I say, "One cappuccino."

[10:17](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=617s) Cappuccino? I can't spell anything today. And a purple

[10:22](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=622s) gorilla named Bonsai. I wanna be precise here. [laughs]

[10:30](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=630s) So I got a bagel with butter because I asked for Bonsai, and the thing is that the lang- what's gonna happen is that the language model really doesn't wanna dis- disappoint you.

[10:41](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=641s) It really wants to make sure you're getting what you want. So

[10:45](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=645s) this is the, this is the thing is you can actually define a schema that is rich enough to encoun- you know, anticipate failure, gives you a chance to recover, show that to the user, say, "I got this and this and this and that.

[10:58](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=658s) It wasn't so clear on that." And that's kind of the beauty of this approach. It's very simple, and it's really just about defining types, which you're gonna use in your application anyway.

[11:09](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=669s) Now, there's this other thing that we started encountering when we s- showed this off to teams internally. Um, people said, "Well, that's all cool. You're turning coffee into code."

[11:21](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=681s) Um, I do too. How do I actually do something more rich like commands? What if I wanna actually script my application in some way?

[11:32](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=692s) Well, this approach that I just showed you actually works for very simple stuff as well, right? You can, you can imagine something where you say, "Schedule an appointment for me," and that turns into a specific command for a calendar app.

[11:44](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=704s) In fact, in our examples, we actually have that. Um, what if you wanna string together multiple things? Hey, that's just a list of commands, right? Um,

[11:55](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=715s) kinda. [laughs] What's the... The problem with this is if I want these to kinda thread through to each other, this is a simple example, so it's just going input, you know, run the command, get the output, go to the input, et cetera, et cetera, et cetera, et cetera.

[12:10](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=730s) Um, what if you have something that expects multiple arguments? What if you wanna reuse a result? Sure seems like you need variables and other things like that here. Um, so we asked ourselves, is there a thing here where you can imagine you can just generate code and

[12:29](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=749s) just take the same approach where types are all you need? So what if you could just define, here's all the methods that I want you to be able to call.

[12:36](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=756s) Come back with some code that only calls those methods, and then generates a program like this.

[12:43](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=763s) The problem is that you really wanna have some sort of sandboxing and safety constraints in place, right? And so you might start saying, "I need availability. I can't just endlessly loop here.

[12:55](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=775s) Um, so I'm not gonna allow loops. I'm not gonna allow lambdas and whatever." And the, the problem is that even if you decide, I'm gonna pick a subset of a language like JavaScript or Python or whatever you have, um, the language models have seen so much of that code that they're gonna draw outside the lines, and then

[13:12](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=792s) you'll hit this failure case, and then you just won't get a result. You won't get a bad result. You just won't get a result that conforms to what you're expecting.

[13:19](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=799s) And then you still have to worry about sandboxing, and then there's all these questions about synchronous versus asynchronous APIs and all this other stuff too, that language models don't tend to understand because I guess most people don't either.

[13:31](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=811s) Um [laughing]  So what we actually [laughs] have been trying is, uh, we generate a fake language. Uh, we have the language models generate a fake language still based on the types,

[13:43](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=823s) but it's in the form of JSON, actually. And so you have things like refs, and refs are just pri- you know, references to prior results. And [laughs] if you're familiar with like, you know, if you're a compiler, this will look, may look like SSA.

[13:54](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=834s) It might look like an AST, whatever. Um,

[13:58](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=838s) but we use that to construct a fake TypeScript program in memory as well, and use that to make sure that not just are you calling all the, only the methods that are available to you, that you can only do certain actions, but also that the inputs from prior steps, um, matches up with the types that you're defining

[14:18](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=858s) from your API. And so that kinda comes back to types are all you need. We have another really simple example for...

[14:28](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=868s) We have a math schema. This is basically a calculator in sheep's clothing. So if you go back and we run this here,

[14:38](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=878s) we have another prompt that's an abacus. That's the closest thing to a calculator I could get. Um, if we could say something like add one to forty-one and then divide by seven.

[14:51](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=891s) Now, basically what happened here is we made a language model good at math. So we've also solved a whole other set of problem, right? [clapping] Yeah. Um, more seriously, though, uh, so at each of these steps, we're actually performing, not having the language model call a method, perform an operation.

[15:14](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=914s) And if you actually look at the code here, um, math main.

[15:20](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=920s) This is all under fifty lines of code. We are able to do the same sort of translation. We have a separate thing called a program translator. And in that program translator, when you are successfully able to validate your results, you know, you say, "If this thing is not a success, just jump out, otherwise do

[15:38](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=938s) some stuff with it." We have this evaluate function, and this evaluate function takes a callback, and that callback is just sort of like this instruction interpreter. And so you can do this with objects, you can do this with a function, with a switch case or whatever.

[15:52](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=952s) Um, but the point is that this actually allows you to do some richer tasks. Um, now, there are other approaches for many of these things, and they overlap with what TypeChat does.

[16:04](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=964s) But the cool thing is that TypeChat is able to actually give you this level of validation for both JSON and programs. Um, and it's something that we're also experimenting with, with other languages too.

[16:15](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=975s) So for example, people at this conference have been saying, "Yeah, I, you know, TypeScript is very cool," and I agree with them because I work on TypeScript. Um, but how would I wor- make this work with Python?

[16:27](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=987s) And so we have been experimenting with this, and we've been getting fairly good results. I'm able to do something like the coffee shop with a very similar approach using types.

[16:37](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=997s) Um, I'm able to do something similar with the calculator app, just defining methods on a class with comments and all this other stuff that helps the model do a little bit better, and it works really well.

[16:50](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=1010s) Um, we can even do more complex examples too. Like we have this CSV example. Um, maybe I want to be able to... Well, I'm not gonna get into... Oh, pipenv.

[17:04](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=1024s) The demos, demo gods are gonna kill me here. [laughs]

[17:10](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=1030s) That. Brutal. Okay. I can just create a program that does this now. I have this entire API that grabs columns and is able to perform certain operations, and then do joins that do filtering and joining and all this other stuff as well because it just sort of does this selection based on Booleans.

[17:34](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=1054s) So read a CSV, find all the values that equal N/A, and then drop the rows. And so this becomes this sort of powerful approach, and this is just a prototype of the Python stuff that we've been working on as well.

[17:45](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=1065s) Um, it's not primetime, and if you wanna talk to me about it, I'm definitely game.

[17:52](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=1072s) So what I want from you all is to try TypeChat out. Reach out. What I'm here at this conference for is to learn about what you're all trying to build, trying to help bridge the gap as well between what we're all learning on the cutting edge and making that more accessible to everyday engineers who have been at

[18:10](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=1090s) this more precise end of the spectrum, bringing the power of these language models that are so rich to the traditional apps. Thank you very much. Come see me at the Microsoft booth.

[18:19](https://www.youtube.com/watch?v=UNKDAo8YNmg&t=1099s) I'll be hanging out for a little bit. And thank you. [clapping] [upbeat music]
