Skip to content

TypePromptLLM prompts that fail at build time

Write prompt templates in dedicated files. TypePrompt validates them, then compiles them into a type-safe API across 8 languages. Works with any LLM SDK.

$npx @typeprompt/cli init
Tilly
Tilly wears sunglasses indoors. Don't ask why.
Just stop putting prompts in string literals.

See it in action โ€‹

Prompt templates are plain XML files that bring together prompt instructions, schemas, tools, and few-shot examples into a single source of truth.

<template name="DecodeCatMood">
  <meta>
    model: gpt-4o
    temperature: 0.2
  </meta>

  <schema>
    // Variables provided to render the prompt
    interface Input {
      catAction: string;
      bowlStatus: "empty" | "half-empty";
    }

    // Structured JSON response expected from the model
    interface Output {
      demand: "feed-me" | "kneel" | "chaos";
      urgency: "chill" | "crisis";
      /** What the human servant must immediately do. */
      humanAction: string;
    }
  </schema>

  <system>
    You are an expert feline behavior interpreter.
    Decode cat actions into their true demands.
  </system>

  <user>
    <insert val="input" format="yaml" />
  </user>

  <examples>
    - input:
        catAction: Yowl at 3am
        bowlStatus: half-empty
      output:
        demand: feed-me
        urgency: crisis
        humanAction: Shake the bowl to cover the bottom.
  </examples>
</template>

npx @typeprompt/cli gen compiles your templates into a type-safe API in your target language. Here's how you use it:

typescript
// MyApp.ts

import { prompts } from "./gen/prompts.js";
import { executePrompt } from "./llm.js";

// 1. Render the prompt template with strongly-typed input
const prompt = prompts.decodeCatMood.render({
  catAction: "Sit on keyboard",
  bowlStatus: "half-empty",
});

// 2. Send it via your own generic helper wrapping any LLM SDK.
// It forwards the rendered messages, tools, examples, and JSON Schema,
// calls prompt.parseResponse(...), and returns the strongly-typed output.
const result = await executePrompt(prompt);

console.log(result.humanAction);
// "Surrender warm laptop and apologize for trying to work."
python
# my_app.py

from my_app.generated.prompts import prompts, schema
from my_app.llm import execute_prompt

# 1. Render the prompt template with strongly-typed input
prompt = prompts.decode_cat_mood.render(
    schema.decode_cat_mood.Input(
        cat_action="Sit on keyboard",
        bowl_status="half-empty",
    )
)

# 2. Send it via your own generic helper wrapping any LLM SDK.
# It forwards the rendered messages, tools, examples, and JSON Schema,
# calls prompt.parse_response(...), and returns the strongly-typed output.
result = await execute_prompt(prompt)

print(result.human_action)
# "Surrender warm laptop and apologize for trying to work."
kotlin
// MyApp.kt

import ai.myapp.generated.prompts.prompts
import ai.myapp.generated.prompts.Schema.DecodeCatMood
import ai.myapp.llm.executePrompt

// 1. Render the prompt template with strongly-typed input
val prompt = prompts.renderDecodeCatMood(
    DecodeCatMood.Input(
        catAction = "Sit on keyboard",
        bowlStatus = DecodeCatMood.Input_BowlStatus.HalfEmpty,
    )
)

// 2. Send it via your own generic helper wrapping any LLM SDK.
// It forwards the rendered messages, tools, examples, and JSON Schema,
// calls prompt.parseResponse(...), and returns the strongly-typed output.
val result = executePrompt(prompt)

println(result.humanAction)
// "Surrender warm laptop and apologize for trying to work."
rust
// my_app.rs

use crate::llm::execute_prompt;
use my_app::gen::prompts::{schema, PROMPTS};

// 1. Render the prompt template with strongly-typed input
let prompt = PROMPTS.render_decode_cat_mood(&schema::decode_cat_mood::Input {
    cat_action: "Sit on keyboard".to_string(),
    bowl_status: schema::decode_cat_mood::Input_BowlStatus::HalfEmpty,
});

// 2. Send it via your own generic helper wrapping any LLM SDK.
// It forwards the rendered messages, tools, examples, and JSON Schema,
// calls prompt.parse_response(...), and returns the strongly-typed output.
let result = execute_prompt(&prompt).await?;

println!("{}", result.human_action);
// "Surrender warm laptop and apologize for trying to work."
cpp
// my_app.cc

#include "gen/prompts.h"
#include "llm.h"
#include <iostream>

using namespace typeprompt;
using Template = templates::DecodeCatMood;

// 1. Render the prompt template with strongly-typed input
auto prompt = Render<Template>({
    .cat_action = "Sit on keyboard",
    .bowl_status = schema::decode_cat_mood::Input_BowlStatus::kHalfEmpty,
});

// 2. Send it via your own generic helper wrapping any LLM SDK.
// It forwards the rendered messages, tools, examples, and JSON Schema,
// calls ParseResponse<Template>(...), and returns the strongly-typed output.
auto result = ExecutePrompt<Template>(prompt);

std::cout << result->human_action << "\n";
// "Surrender warm laptop and apologize for trying to work."
swift
// MyApp.swift

import MyPrompts

// 1. Render the prompt template with strongly-typed input
let prompt = prompts.renderDecodeCatMood(
    Schema.DecodeCatMood.Input(
        catAction: "Sit on keyboard",
        bowlStatus: .halfEmpty
    )
)

// 2. Send it via your own generic helper wrapping any LLM SDK.
// It forwards the rendered messages, tools, examples, and JSON Schema,
// calls prompt.parseResponse(...), and returns the strongly-typed output.
let result = try await executePrompt(prompt)

print(result.humanAction)
// "Surrender warm laptop and apologize for trying to work."
dart
// my_app.dart

import 'package:my_app/gen/prompts.dart';
import 'package:my_app/llm.dart';

// 1. Render the prompt template with strongly-typed input
final prompt = prompts.renderDecodeCatMood(
  DecodeCatMood_Input(
    catAction: "Sit on keyboard",
    bowlStatus: DecodeCatMood_Input_BowlStatus.halfEmpty,
  ),
);

// 2. Send it via your own generic helper wrapping any LLM SDK.
// It forwards the rendered messages, tools, examples, and JSON Schema,
// calls prompt.parseResponse(...), and returns the strongly-typed output.
final result = await executePrompt(prompt);

print(result.humanAction);
// "Surrender warm laptop and apologize for trying to work."
csharp
// MyApp.cs

using MyApp.Llm;
using MyApp.Prompts;

// 1. Render the prompt template with strongly-typed input
var prompt = Prompts.Default.RenderDecodeCatMood(new Schema.DecodeCatMood.Input
{
    CatAction = "Sit on keyboard",
    BowlStatus = Schema.DecodeCatMood.Input_BowlStatus.HalfEmpty
});

// 2. Send it via your own generic helper wrapping any LLM SDK.
// It forwards the rendered messages, tools, examples, and JSON Schema,
// calls prompt.ParseResponse(...), and returns the strongly-typed output.
var result = await LlmClient.ExecutePrompt(prompt);

Console.WriteLine(result.HumanAction);
// "Surrender warm laptop and apologize for trying to work."

If your application code and your templates drift apart, your compiler or type checker catches it โ€” not at runtime, in production, after the model replies.

Features โ€‹

Test your templates where you write them โ€‹

Run sample inputs against a real model and see the output without leaving your IDE.

Ready to try it?
Add type-safe prompts to your project with a single command.
Get started
$npx @typeprompt/cli init

Made with ๐Ÿฉท in New York and outer space