Node.js SDK (Unofficial)

JavaScript/TypeScript client for LangExtract information extraction

Important Notice

This is an unofficial Node.js SDK for LangExtract. It is not affiliated with or endorsed by Google. For the official Python library, please visit the official GitHub repository.

Installation

Install the LangExtract Node.js SDK using npm or yarn:

# npm
npm install langextract

# yarn
yarn add langextract

# pnpm
pnpm add langextract

Quick Start

Get started with LangExtract in Node.js with this simple example:

Basic Usage

Extract information from text using TypeScript:

import { extract, ExampleData } from "langextract";

const examples: ExampleData[] = [
  {
    text: "John Smith is 30 years old and works at Google.",
    extractions: [
      {
        extractionClass: "person",
        extractionText: "John Smith",
        attributes: {
          age: "30",
          employer: "Google"
        }
      }
    ]
  }
];

async function extractPersonInfo() {
  const result = await extract("Alice Johnson is 25 and works at Microsoft.", {
    promptDescription: "Extract person information including name, age, and employer",
    examples: examples,
    modelType: "gemini",
    apiKey: process.env.LANGEXTRACT_API_KEY
  });
  
  console.log(result.extractions);
}

Configuration

API Key Setup

Set up your API key for cloud models like Gemini:

# .env file
LANGEXTRACT_API_KEY=your-api-key-here

Supported Models

The SDK supports multiple LLM providers:

  • Google Gemini models (recommended)
  • OpenAI GPT models
  • Local Ollama models

API Reference

extract()

Main method for extracting information from text

async function extract(
  text: string,
  options: {
    promptDescription: string;
    examples: ExampleData[];
    modelType: "gemini" | "openai" | "ollama";
    apiKey?: string;
    modelId?: string;
  }
): Promise<ExtractionResult>

visualize()

Generate interactive HTML visualization of extraction results

function visualize(
  results: ExtractionResult[],
  options?: {
    theme?: "light" | "dark";
    highlightColors?: string[];
  }
): string

batchExtract()

Process multiple documents in parallel

async function batchExtract(
  documents: string[],
  options: ExtractOptions & {
    concurrency?: number;
    onProgress?: (completed: number, total: number) => void;
  }
): Promise<ExtractionResult[]>

Code Examples

Basic Extraction

Extract entities from simple text

const result = await extract("The product costs $99.99", {
  promptDescription: "Extract product price",
  examples: [{
    text: "Price: $50",
    extractions: [{
      extractionClass: "price",
      extractionText: "$50"
    }]
  }],
  modelType: "gemini"
});

Advanced Features

Using attributes and custom extraction classes

const examples = [{
  text: "Dr. Smith prescribed 500mg aspirin",
  extractions: [{
    extractionClass: "medication",
    extractionText: "aspirin",
    attributes: {
      dosage: "500mg",
      prescriber: "Dr. Smith"
    }
  }]
}];

Batch Processing

Process multiple documents efficiently

const documents = [
  "Document 1 content...",
  "Document 2 content...",
  "Document 3 content..."
];

const results = await batchExtract(documents, {
  promptDescription: "Extract entities",
  examples: examples,
  modelType: "gemini",
  concurrency: 3,
  onProgress: (done, total) => {
    console.log(`Progress: ${done}/${total}`);
  }
});

Visualization

Generate and save interactive visualizations

const html = visualize(results, {
  theme: "light",
  highlightColors: ["#3B82F6", "#10B981", "#F59E0B"]
});

// Save to file
import { writeFileSync } from "fs";
writeFileSync("results.html", html);

TypeScript Support

Full TypeScript support with type definitions included:

  • Complete type definitions for all API methods
  • IntelliSense support in VS Code and other IDEs
  • Type-safe extraction results
  • Generic types for custom extraction classes

Error Handling

Proper error handling patterns:

Error Handling Example

Handle API errors and validation failures

try {
  const result = await extract(text, options);
  console.log("Extraction successful:", result);
} catch (error) {
  if (error.code === "INVALID_API_KEY") {
    console.error("Invalid API key provided");
  } else if (error.code === "RATE_LIMIT_EXCEEDED") {
    console.error("Rate limit exceeded, retry after:", error.retryAfter);
  } else if (error.code === "VALIDATION_ERROR") {
    console.error("Validation error:", error.details);
  } else {
    console.error("Unexpected error:", error.message);
  }
}

Best Practices

  • 1Provide high-quality examples for better extraction accuracy
  • 2Use batch processing for multiple documents
  • 3Cache extraction results to reduce API calls
  • 4Implement proper error handling and retries
  • 5Use environment variables for API keys

Ready to Get Started?

Install LangExtract with pip and start extracting structured information from your text data in minutes.

npm install langextract