npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ts-prompt-fn

v0.1.1

Published

TypeScript library for creating strongly-typed LLM functions with structured output validation

Downloads

17

Readme

ts-prompt-fn

A TypeScript library for creating strongly-typed functions that use LLMs to process natural language and return structured data. All LLM responses are validated using Zod schemas for type safety.

Features

  • 💪 Strongly Typed: Full TypeScript support with Zod schema validation
  • 🔀 Multiple Providers: Support for OpenAI, Gemini, Anthropic, and xAI/Grok
  • 🧩 Simple API: Create LLM-powered functions with just a few lines of code
  • Validation: Automatic validation and parsing of LLM responses
  • 🛠️ Customizable: Configure providers, models, and prompt templates

Installation

# npm
npm install ts-prompt-fn zod

# yarn
yarn add ts-prompt-fn zod

# pnpm
pnpm add ts-prompt-fn zod

Quick Start

import { configure, createLLMPrompt, z } from 'ts-prompt-fn';

// Configure your providers (store API keys in environment variables)
configure({
  providers: {
    openai: { apiKey: process.env.OPENAI_API_KEY, model: "gpt-4o" },
    // Add other providers as needed
  },
  defaultProvider: 'openai'
});

// Define your output schema with Zod
const AnalysisSchema = z.object({
  sentiment: z.enum(["positive", "negative", "neutral"]),
  keywords: z.array(z.string()),
  summary: z.string()
});

// Create your typed prompt function
const analyzeText = createLLMPrompt<
  { text: string },     // Input type
  z.infer<typeof AnalysisSchema>  // Output type
>(
  // Prompt template
  'Analyze the following text and extract the sentiment, keywords, and a brief summary:\n"{text}"',
  { outputSchema: AnalysisSchema }
);

// Use your function with type safety
async function main() {
  const result = await analyzeText({ 
    text: "I really enjoyed the new product launch. The features are innovative and user-friendly!"
  });
  
  // Fully typed result
  console.log(`Sentiment: ${result.sentiment}`);
  console.log(`Keywords: ${result.keywords.join(', ')}`);
  console.log(`Summary: ${result.summary}`);
}

main();

Documentation

Configuration

First, configure the library with your LLM provider details:

configure({
  providers: {
    openai: { apiKey: "YOUR_API_KEY", model: "gpt-4o" },
    gemini: { apiKey: "YOUR_API_KEY", model: "gemini-1.5-flash" },
    anthropic: { apiKey: "YOUR_API_KEY", model: "claude-3-haiku-20240307" },
    xai: { apiKey: "YOUR_API_KEY" }
  },
  defaultProvider: 'openai' // Default provider to use
});

Creating Prompt Functions

const myFunction = createLLMPrompt<InputType, OutputType>(
  promptTemplate,
  options
);

Parameters:

  • promptTemplate: String with placeholder variables like {varName}
  • options: Configuration including:
    • outputSchema: Zod schema that defines the expected output structure
    • provider: (Optional) Override the default provider
    • model: (Optional) Override the default model
    • promptHeader: (Optional) Custom instructions for the LLM

Working with Multiple Providers

You can specify which provider to use for each prompt function:

const openaiFunction = createLLMPrompt<InputType, OutputType>(
  promptTemplate,
  { 
    outputSchema: MySchema,
    provider: 'openai',
    model: 'gpt-4-turbo'
  }
);

const geminiFunction = createLLMPrompt<InputType, OutputType>(
  promptTemplate,
  { 
    outputSchema: MySchema,
    provider: 'gemini'
  }
);