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

@molecule/api-ai-document-extraction

v1.0.1

Published

Upload document → extract text → AI extract structured fields

Readme

@molecule/api-ai-document-extraction

Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit src/index.ts JSDoc, not this file.

@molecule/api-ai-document-extraction — feed text + a schema of fields, get back a structured object via the bonded AI provider.

Extracted from ai-document-processor flagship. For PDF/image inputs, pre-process with @molecule/api-pdf or your OCR provider to get the text argument.

Quick Start

import { extractFields, missingRequiredFields } from '@molecule/api-ai-document-extraction'
import type { ExtractionField } from '@molecule/api-ai-document-extraction'

const fields: ExtractionField[] = [
  { name: 'invoice_number', type: 'string', required: true, description: 'The invoice ID/number' },
  {
    name: 'total_amount',
    type: 'number',
    required: true,
    description: 'Total amount due in cents',
  },
  { name: 'due_date', type: 'date', description: 'Payment due date' },
  { name: 'vendor', type: 'string', description: 'Vendor / supplier name' },
]

const result = await extractFields({
  text: invoiceText,
  fields,
  context: 'Invoice from a B2B vendor',
})
const missing = missingRequiredFields(result, fields)
if (missing.length) console.warn('Could not extract:', missing)

Type

utility

Installation

npm install @molecule/api-ai-document-extraction @molecule/api-ai @molecule/api-bonds-default-express @molecule/api-database @molecule/api-i18n @molecule/api-middleware-validation

API

Interfaces

ExtractionField

A field to extract from a document.

interface ExtractionField {
  /** Field name (becomes a JSON key in the result). */
  name: string
  /** Plain-English description of what to extract. */
  description: string
  /** Expected type — hints to the LLM and validates the result. */
  type: 'string' | 'number' | 'boolean' | 'date' | 'array' | 'object'
  /** Whether the field is required. Default false. */
  required?: boolean
}

ExtractionResult

Result of a single extraction run.

interface ExtractionResult<T = Record<string, unknown>> {
  /** Extracted fields keyed by `field.name`. */
  data: T
  /** AI's confidence per field (0..1). May be partial. */
  confidence?: Partial<Record<keyof T, number>>
  /** Free-form reasoning the AI provided. */
  reasoning?: string
}

Functions

extractFields(opts)

Extract a set of fields from a document. The AI prompt enumerates each field with its type and description; the response is validated and any missing/required fields are flagged.

function extractFields(opts: {
  text: string
  fields: ExtractionField[]
  context?: string
  model?: string
  temperature?: number
}): Promise<ExtractionResult<T>>

missingRequiredFields(result, fields)

Validate that all required fields were extracted as non-null. Returns the list of missing field names.

function missingRequiredFields(result: ExtractionResult<T>, fields: ExtractionField[]): string[]

Injection Notes

Requirements

Peer dependencies:

  • @molecule/api-bonds-default-express ^1.0.1
  • @molecule/api-database ^1.0.1
  • @molecule/api-i18n ^1.0.1
  • @molecule/api-middleware-validation ^1.0.1
  • @molecule/api-ai ^1.0.1

Runtime Dependencies

  • @molecule/api-ai
  • @molecule/api-bonds-default-express
  • @molecule/api-database
  • @molecule/api-i18n
  • @molecule/api-middleware-validation

Requires a bonded AI provider: extractFields resolves the singleton via requireProvider() from @molecule/api-ai — wire your AI bond at startup (whichever provider bond the app uses) or the call THROWS. It also throws when multiple named providers are bonded with no default; set the default at bond time rather than selecting per-call.

The result is BEST-EFFORT and never throws on content: malformed model output yields { data: {}, reasoning: 'AI returned malformed JSON' }, any field may be null, and confidence is optional/partial. ALWAYS validate with missingRequiredFields(result, fields) before trusting result.data — treat a non-empty return as "extraction failed for these fields", not an exception. temperature defaults to 0 for determinism.

Text in, structure out: this package does no OCR/PDF parsing (pre-process with @molecule/api-pdf or your OCR provider) and no chunking — split or truncate very long documents yourself before calling.