TypeChat uses TypeScript types to guide language models toward the data structures an application expects.
2
The same types validate model output and provide error messages that can be sent back for repair.
3
For multi-step tasks, TypeChat generates a constrained JSON program instead of asking a model to write unrestricted JavaScript or Python.
Summary
Daniel Rosenwasser presents TypeChat as a way to connect language models with existing applications that need structured data. He starts with the limits of parsing natural-language responses, then shows how TypeScript interfaces can describe the expected result and guide a model toward it. TypeChat uses those types again to validate the response. Validation errors can be sent back to the model for repair. In a coffee-shop example, an unknown request such as a purple gorilla named Bonsai is preserved as unknown text instead of silently breaking the application. Rosenwasser then extends the approach to commands and multi-step programs. Rather than generating unrestricted code, TypeChat uses a small JSON-based language with references to earlier results, validates it against available methods and types, and interprets the calls in the application. He also describes early Python experiments, including calculator and CSV examples. The talk is a practical account of where types help and where sandboxing and model behavior remain problems.
Natural-language output cannot be parsed safely by application code
Rosenwasser contrasts chat interfaces with traditional applications that need precise data. In a Seattle rainy-day example, a developer might prompt the model to put each venue on its own line, add a leading number, and separate the venue name from its description with a colon. Parsing that format by splitting lines and colons can work in a demo, but a model may change the format or include a colon inside the input. Rosenwasser calls parsing natural language "extremely hard" and says it is close to a fool's errand for most people. JSON is a better starting point, but requesting JSON alone does not guarantee the required properties or enough data.
Types can describe model output more completely than a simple JSON example
A small JSON example can describe uniform objects, but real application data has optional fields, values that may be null, and properties that can accept one of several types. Rosenwasser says examples become difficult to describe as these cases combine. TypeChat instead uses ordinary TypeScript interfaces, such as a list of objects with venue names and descriptions that are both strings. A model trained on natural language and code can use those definitions alongside user input and intent. The types give the model a description of the structure the application will consume, while leaving the natural-language request as the input.
The same types guide generation and validate the result
TypeChat uses TypeScript's relationship with JSON to construct a small program that checks the generated data. If validation succeeds, the application receives well-typed output. If it fails, the compiler produces an error message. TypeChat can send that message back to the language model as a repair request, telling it what did not match the expected result. Rosenwasser describes this as the central idea behind the library: types guide the model before generation and check its response afterward. The npm library bundles model setup, type-based translation, validation, and repair into a short application flow.
A schema can preserve requests the application does not understand
In the coffee-shop demo, a request for a latte succeeds, while a request for a medium purple gorilla named Bonsai is not forced into a valid coffee order. TypeChat's schema includes unknown text, so the application can report that it did not understand that part of the request. Rosenwasser says language models tend to avoid disappointing the user and may invent a response unless the schema gives the application somewhere to put unsupported input. This lets the program show the user what it understood and what needs clarification. The example uses fewer than 40 lines of code, with the main application logic calling a translator for the selected type.
Multi-step tasks need a constrained program representation
Rosenwasser extends TypeChat from single commands to tasks that schedule appointments, chain several commands, pass results between steps, and reuse values. Generating unrestricted JavaScript or Python creates safety and reliability problems. A model may use loops, lambdas, or APIs outside the allowed subset, and the application still needs to sandbox the result. TypeChat experiments with a fake language represented as JSON. It includes references to earlier results, which Rosenwasser compares to ideas such as static single assignment. TypeChat builds an in-memory TypeScript program from that representation and checks that calls use only available methods and that values passed between steps have the expected types.
The program translator lets the application interpret validated calls
The math example expresses a calculator through typed methods. A request such as adding 1 to 41 and dividing by 7 becomes a sequence of method calls. TypeChat validates the generated program, then evaluates it through a callback that acts as an instruction interpreter. The application can implement that callback with objects, functions, or a switch statement. This separates the model's job, choosing typed operations and their order, from the application's job, executing those operations. Rosenwasser says the example is under 50 lines and that the same pattern supports richer tasks than a single JSON object.
Python support is an experiment with typed methods and comments
Rosenwasser describes early work to apply the approach outside TypeScript. In Python, the coffee-shop and calculator examples use types, class methods, and comments that give the model additional context. He also shows a CSV example with an API for reading columns, filtering values, dropping rows, and joining data. A generated program can read a CSV, find values equal to a target, and drop the matching rows through those constrained operations. He calls the Python work a prototype rather than a finished product. The examples show that the approach depends on defining an API and checking the model's selections against it.
"The language models have seen so much of that code that they're going to draw outside the lines"13:02
Who should watch
You are adding a natural-language interface to an existing application and need data that matches application types.
You want model output to trigger typed commands or multi-step operations without executing unrestricted generated code.
You work with TypeScript or Python and want a small experimental approach to validation, repair, and constrained model-generated programs.