@beparallel/langchain-ts

中文文档
1.1.1发布于 2026/04/07MITTypeScriptES Module近 30 天下载 2839

Extracts Langchain prompts and generates TypeScript types.

原始使用文档
文档目录23 节
  1. Features
  2. Installation
  3. Configuration
  4. Usage
  5. CLI flags
  6. Examples
  7. package.json scripts
  8. How prompt export works
  9. Programmatic API
  10. Contributing
  11. Setup
  12. Environment
  13. Running locally
  14. Tests
  15. Release
  16. Changelog
  17. 1.1.0
  18. 1.0.0
  19. 0.3.0
  20. 0.2.8
  21. 0.2.0
  22. 0.1.0
  23. License

@beparallel/langchain-ts

A CLI tool that pulls prompts from LangSmith Hub and generates TypeScript types and optional JSON exports for offline use.

Features

  • Type generation -- produces TypeScript interfaces, enums, and Zod schemas from prompt input/output schemas
  • Prompt export -- serializes full prompt content to JSON files so you can load them offline without calling pull() at runtime
  • Filtering -- narrow results to a specific prompt by name, or override the Hub tag
  • Composable flags -- use --file and --export-dir independently or together in a single run

Installation

Install the package:

pnpm i @beparallel/langchain-ts

Configuration

Add the following environment variables (e.g. in a .env file):

VariableRequiredDescription
LANGCHAIN_API_KEYYesYour LangSmith API key
LANGCHAIN_TAGYesThe Hub tag to pull (e.g. latest, staging)
LANGCHAIN_API_KEY=your_api_key
LANGCHAIN_TAG=your_tag

Usage

At least one of --file or --export-dir is required.

CLI flags

FlagDescription
--file=PATHOutput path for the generated TypeScript types file
--export-dir=DIRDirectory to write per-prompt JSON files
--name=NAMEFilter prompts by name (partial match)
--tag=TAGOverride the LANGCHAIN_TAG environment variable

Examples

# Generate types only
npx langchain-types --file=./prompt.types.ts

# Export prompts as JSON only
npx langchain-types --export-dir=./prompts/

# Both at once
npx langchain-types --file=./prompt.types.ts --export-dir=./prompts/

# Filter to a single prompt
npx langchain-types --file=./prompt.types.ts --name=my-prompt

package.json scripts

A typical setup using dotenv-cli to load env vars:

{
  "scripts": {
    "prompt:generate": "dotenv -f .env run -- npx langchain-types --file=./types/prompts.ts",
    "prompt:export": "dotenv -f .env run -- npx langchain-types --export-dir=./prompts/",
    "prompt:all": "dotenv -f .env run -- npx langchain-types --file=./types/prompts.ts --export-dir=./prompts/"
  }
}

How prompt export works

The --export-dir flag calls LangChain's toJSON() on each ChatPromptTemplate and writes the result to <dir>/<tag>/<prompt-name>.json, organized by tag. For example, with --export-dir=./prompts/ --tag=staging:

prompts/
  staging/
    my-prompt.json
    another-prompt.json

These files contain the full serialized representation (message templates, input variables, output schema) and can be loaded with LangChain's load() deserialization -- no network call to the Hub required.

Programmatic API

The package also exports functions for use in your own scripts:

import { extractPrompts, generateTypes, exportPrompts } from '@beparallel/langchain-ts'

const prompts = await extractPrompts({
  langchainApiKey: process.env.LANGCHAIN_API_KEY!,
  langchainTag: 'latest',
})

generateTypes(prompts, './prompt.types.ts')
exportPrompts(prompts, './prompts/', 'latest')

Contributing

Setup

pnpm i

Environment

export LANGCHAIN_API_KEY=your_api_key
export LANGCHAIN_TAG=your_tag

Running locally

pnpm prompt:generate

Tests

pnpm test

Release

Publishing is automated via GitHub Actions. When a v* tag is pushed, the workflow runs tests, builds, publishes to npm, and creates a GitHub release with auto-generated notes.

# Bump version (patch, minor, or major) -- this updates package.json and creates a git tag
npm version patch   # 1.1.0 -> 1.1.1
npm version minor   # 1.1.0 -> 1.2.0
npm version major   # 1.1.0 -> 2.0.0

# Push the commit and tag to trigger the publish workflow
git push --follow-tags

Prerequisites: the repo needs NPM_TOKEN configured in GitHub Secrets (Settings > Secrets and variables > Actions).

Changelog

1.1.0

  • Add --export-dir CLI flag to serialize prompts as JSON files for offline use
  • Make --file optional (at least one of --file or --export-dir is now required)
  • Add exportPrompts() to the programmatic API
  • Clean up dead code in CLI entry point

1.0.0

  • Upgrade to LangChain v1 (@langchain/core ^1.x, langchain ^1.x)

0.3.0

  • Upgrade dependencies to latest versions
  • Refactor parser module to use named import for pull from langchain/hub

0.2.8

  • Add --name flag for filtering prompts by name (partial match)
  • Add --tag flag for overriding LANGCHAIN_TAG from the command line

0.2.0

  • Add ESLint, Jest, and Prettier configuration
  • Introduce utility functions for case conversion
  • Add unit tests for parser and type generator modules

0.1.0

  • Initial release
  • CLI for extracting LangSmith Hub prompts and generating TypeScript types
  • Enum, interface, and Zod schema generation from prompt JSON schemas

License

This project is licensed under the MIT License. See the LICENSE file for details.

版本与发布记录

@beparallel/langchain-ts 共发布 22 个版本,首次发布于 2025/05/16;过去 12 个月发布了 5 个版本。

版本发布日期距今
1.1.1latest2026/04/077 个月前
1.1.02026/04/077 个月前
1.0.02026/01/0610 个月前
0.3.02026/01/0610 个月前
0.2.82025/12/1210 个月前
0.2.72025/06/101.4 年前

关于 @beparallel/langchain-ts 的常见问题

@beparallel/langchain-ts 是什么?

Extracts Langchain prompts and generates TypeScript types.

如何安装 @beparallel/langchain-ts?

在项目目录运行 npm install @beparallel/langchain-ts,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 langchain-types,可以用 npx langchain-types 直接运行。

@beparallel/langchain-ts 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@beparallel/langchain-ts 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@beparallel/langchain-ts 可以商用吗?

@beparallel/langchain-ts 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@beparallel/langchain-ts 的下载量有多少?

根据 npm 官方下载统计,@beparallel/langchain-ts 在 2026/10/08 – 2026/11/06 这 30 天内被下载 2,839 次,日均约 95 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@beparallel/langchain-ts 还在维护吗?

最新版本 1.1.1 发布于 2026/04/07(7 个月前);至今共发布 22 个版本,其中过去 12 个月发布了 5 个;首次发布于 2025/05/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。