# TypeChat: Schema Engineering as a Replacement for Prompt Engineering

> TypeChat is a TypeScript library from Microsoft that converts natural language into validated JSON by using developer-defined types as the schema. It removes hand-tuned prompts from the picture, automatically repairs non-conforming LLM output, and is also available for Python and C#.

**microsoft/TypeChat** — TypeChat is a library that makes it easy to build natural language interfaces using types.

- Repository: https://github.com/microsoft/TypeChat
- Website: https://microsoft.github.io/TypeChat/
- Stars: 8,686 · Forks: 414
- Language: TypeScript
- License: MIT
- Published: 2026-09-09 · Updated: 2026-09-09 · Language: en
- Canonical page: https://hysenlabs.com/projects/microsoft-typechat

## The Problem TypeChat Addresses: Unstructured LLM Output

Applications that process natural language input traditionally required complex decision trees or custom parsing logic to extract intent and structured data from user messages. Large language models made intent detection easier but introduced a different problem: the model might return malformed JSON, add extra commentary around the output, or drift from the expected format as the prompt grows larger.

Prompt engineering attempts to solve this through careful instruction writing, but longer prompts are more fragile and harder to maintain. TypeChat takes a different approach. Instead of telling the model how to format output in natural language, you define TypeScript types that describe the allowed responses. TypeChat then constructs the prompt from the type definition, validates the LLM response against that type, and uses further LLM interaction to repair the output if it does not conform.

The result is that the schema becomes the single source of truth for both what the model should produce and what the application expects to receive.

## Schema Engineering: How TypeChat Turns Types Into Prompts

The TypeChat workflow has three steps according to the README. First, the developer defines TypeScript types that represent the intents their application supports. This could be a simple discriminated union for sentiment classification or a more complex interface for a shopping cart or music player command set. Second, TypeChat constructs a prompt from those types and sends it to the LLM. Third, it validates that the LLM response is a valid instance of the type. If validation fails, TypeChat uses an additional LLM interaction to repair the output.

To extend the set of supported intents, a developer adds a new type to the discriminated union. No prompt rewriting is needed. To create hierarchical intents, TypeChat supports a meta-schema approach where a top-level type dispatches to one or more sub-schemas based on user input. This composability is the primary advantage over hand-written prompts, which need manual updates whenever the intent set changes.

The README states the validation and summarization step is performed without an LLM call, using the type system directly, which keeps that part of the pipeline deterministic.

## Installing TypeChat and Building a Typed Interface

TypeChat installs from npm for TypeScript and JavaScript projects:

```
npm install typechat
```

The repository also includes a Python implementation under the python/ directory and a C# implementation as a separate repository at github.com/microsoft/TypeChat.net. The TypeScript implementation is the primary one, with the most complete documentation.

The repository includes a .env.example file that shows the two LLM configurations TypeChat supports out of the box:

```
OPENAI_MODEL=
OPENAI_API_KEY=
AZURE_OPENAI_ENDPOINT=
AZURE_OPENAI_API_KEY=
```

After installing and filling in credentials, the recommended next step is to run the example projects in the typescript/examples directory. The README suggests using a GitHub Codespace to explore the examples without local setup. Examples include a shopping cart parser, a sentiment classifier, and a music player command set, each demonstrating a different type structure.

The documentation site at microsoft.github.io/TypeChat contains additional guidance beyond the README.

## Validation, Repair, and the Role of the Type System

The repair loop is what distinguishes TypeChat from a simple JSON-extraction prompt. When the LLM returns output that does not conform to the schema, TypeChat feeds the validation error back to the model and asks it to produce a corrected version. This loop runs until the output validates or a retry limit is reached.

This design has a concrete implication for model selection. Models that follow instructions well and have good JSON formatting behavior will complete in fewer repair iterations. Models that frequently deviate from structured formats will require more LLM calls per request, increasing both latency and cost.

The README also mentions a summarization step that confirms the validated instance aligns with the user's original intent. This step does not use an LLM call, so it adds no additional API cost. The mechanism relies on TypeScript's type system, meaning the guarantees it provides are the guarantees of TypeScript's type checking.

## What TypeChat Cannot Fix

TypeChat addresses output structure, not output correctness. A model can produce a perfectly typed response that is factually wrong or unhelpful. If the user asks for a product recommendation and the model returns valid JSON with an incorrect product name, TypeChat will accept it.

Typing also does not prevent hallucination. A model might fill a required field with plausible-sounding but invented data. TypeChat validates the shape of the response, not the truth of its contents.

For applications where the output must be free-form text, such as a general-purpose chatbot or a document drafting tool, TypeChat adds complexity without benefit. The schema-based approach works best when the application's intent space is finite and describable as types.

TypeChat also requires that your TypeScript (or Python or C#) types accurately describe all valid outputs. If the type definition is incomplete or incorrect, the repair loop may produce outputs that are technically valid but semantically wrong.

## TypeChat vs. Hand-Written Parsers and Zod-Based Validation

An alternative to TypeChat is to write a regular expression or a custom parser that extracts structured data from LLM output. This is fragile: any change in how the model formats its response breaks the parser, and these changes happen with model updates or prompt changes.

Zod is a TypeScript schema validation library that is widely used in TypeScript applications. Combining Zod with a hand-written prompt that requests JSON output achieves some of the same goals as TypeChat, but without the automatic repair loop. When the LLM returns invalid JSON, a Zod-based approach returns an error; the application must decide what to do next. TypeChat's repair loop handles that retry automatically and feeds the validation error back to the model in a structured way.

The trade-off is that TypeChat requires TypeScript types as the schema source rather than Zod schemas, which means two separate type systems for teams that already use Zod throughout. TypeChat is a tighter integration if you are starting fresh; Zod may be a better fit for an application that already has an established validation layer.

## Language Support, Maintenance, and License

TypeChat ships with TypeScript as the primary implementation. The repository contains a python/ directory with a Python version and a link to the separate C# repository at github.com/microsoft/TypeChat.net. The Python and C# implementations are referenced in the README but are not described in equal detail.

The repository has no GitHub releases. The last push was on 2026-09-09. It is not archived. The project is MIT-licensed, which allows commercial use, modification, and distribution with attribution.

The project requires contributors to sign a Contributor License Agreement, as noted in the CONTRIBUTING section of the README. Microsoft has adopted the Microsoft Open Source Code of Conduct for this repository.

## Conclusion

TypeChat is the right choice when you want LLM responses to conform to a typed contract without writing custom validation or repair logic. It is a poor fit for tasks where the output format is intentionally open-ended or where you are working outside TypeScript, Python, or C#. Before starting, verify your LLM credentials are available through the OPENAI_API_KEY or AZURE_OPENAI_ENDPOINT variables listed in the .env.example file.

## FAQ

### What is TypeChat and what does it do?

TypeChat is a TypeScript library from Microsoft that uses developer-defined types as the schema for LLM responses. It constructs a prompt from the type, validates the LLM output, and automatically repairs non-conforming responses through further model interaction.

### Does TypeChat support languages other than TypeScript?

The repository includes a Python implementation in the python/ directory and references a C# implementation at github.com/microsoft/TypeChat.net. The TypeScript version is the primary implementation with the most complete documentation.

### What types of applications work best with TypeChat?

TypeChat works best when the application's intent space is finite and describable as types, such as shopping cart commands, music player controls, or sentiment classification. It is not suited for free-form generation tasks where the output format is intentionally open.

## Sources

- [Issues](https://github.com/microsoft/TypeChat/issues)
- [License: MIT](https://github.com/microsoft/TypeChat/blob/main/LICENSE)
- [microsoft/TypeChat on GitHub](https://github.com/microsoft/TypeChat)
- [Project website](https://microsoft.github.io/TypeChat/)
- [README](https://github.com/microsoft/TypeChat/blob/main/README.md)

---

Hysen Labs editorial analysis, written from the project's own repository and release notes. Cite the canonical page: https://hysenlabs.com/projects/microsoft-typechat
