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

langchain-pubmed

v0.1.2

Published

LangChain.js integration for PubMed API - search biomedical literature and retrieve article metadata

Downloads

14

Readme

🧬 langchain-pubmed

LangChain.js integration for PubMed API - search biomedical literature and retrieve article metadata.

npm version License: MIT

Features

  • 🔍 Search PubMed's 35+ million biomedical literature citations
  • 🤖 Ready-to-use LangChain Tool for AI agents
  • 📄 Returns structured article metadata (title, abstract, publication date)
  • ⚡ Built-in retry logic and rate limit handling
  • 🔄 Supports streaming results with async iterators
  • 🎯 Converts to LangChain Documents for RAG applications

Installation

npm install langchain-pubmed @langchain/core

Quick Start

import { PubMedTool } from "langchain-pubmed";

const tool = new PubMedTool({
  topKResults: 3,
  email: "[email protected]", // Recommended for better rate limits
});

const result = await tool.invoke("covid-19 vaccine efficacy");
console.log(result);

Environment Variables

Set these environment variables to avoid hardcoding credentials:

export PUBMED_EMAIL="[email protected]"
export PUBMED_API_KEY="your_ncbi_api_key"  # Optional, for higher rate limits

Then use without passing credentials:

const tool = new PubMedTool({ topKResults: 3 });

Usage

1. As a LangChain Tool

import { PubMedTool } from "langchain-pubmed";

const tool = new PubMedTool({
  topKResults: 3,
  email: "[email protected]",
});

// Direct invocation
const result = await tool.invoke("diabetes treatment");
console.log(result);

2. With an AI Agent

import { ChatOpenAI } from "@langchain/openai";
import { createAgent } from "langchain";
import { PubMedTool } from "langchain-pubmed";
import dotenv from "dotenv";

dotenv.config();

const model = new ChatOpenAI({
  model: "gpt-5-nano",
});

const tools = [
  new PubMedTool({
    topKResults: 5,
  }),
];

const agent = createAgent({ model, tools });

const result = await agent.invoke({
  messages: [
    {
      role: "user",
      content: "What are the latest treatments for Alzheimer's disease?",
    },
  ],
});

console.log(result.messages[result.messages.length - 1].content);

3. Direct API Wrapper

import { PubMedAPIWrapper } from "langchain-pubmed";

const pubmed = new PubMedAPIWrapper({
  topKResults: 5,
  email: "[email protected]",
  apiKey: "your_ncbi_api_key", // Optional: for higher rate limits
});

// Get structured metadata
const articles = await pubmed.load("cancer immunotherapy");
articles.forEach((article) => {
  console.log(article.Title);
  console.log(article.Summary);
});

// Get LangChain Documents
const documents = await pubmed.loadDocs("machine learning healthcare");

// Stream results
for await (const article of pubmed.lazyLoad("alzheimer disease")) {
  console.log(article.Title);
}

4. For RAG Applications

import { PubMedAPIWrapper } from "langchain-pubmed";

const pubmed = new PubMedAPIWrapper({
  topKResults: 10,
  email: "[email protected]",
});

// Get LangChain Documents for vector store
const documents = await pubmed.loadDocs("CRISPR gene editing");

// Add to your vector store
// await vectorStore.addDocuments(documents);

Configuration Options

| Option | Type | Default | Description | | -------------------------------------------------- | -------- | -------------------------- | ------------------------------- | | topKResults | number | 3 | Number of results to return | | maxQueryLength | number | 300 | Max query length (chars) | | docContentCharsMax | number | 2000 | Max content length (chars) | | maxRetry | number | 5 | Max retries on rate limit | | sleepTime | number | 200 | Initial retry delay (ms) | | email | string | "[email protected]" | Email for PubMed API | | apiKey | string | "" | NCBI API key (optional) | | Plus all ToolParams from @langchain/core/tools | | | Callbacks, tags, metadata, etc. |

Rate Limits

  • Without API key: 3 requests/second
  • With API key: 10 requests/second

Get a free API key at: https://www.ncbi.nlm.nih.gov/account/settings/

Development

# Install dependencies
npm install

# Build
npm run build

# Run tests
npm test

# Run e2e tests (calls actual PubMed API)
npm run test:e2e

# Run unit tests
npm run test:unit

# Lint
npm run lint

# Format
npm run format

Example

See example.ts for a complete working example.

# Build first, then run the example
npm run build
npx ts-node example.ts

API

PubMedTool

Main tool class for LangChain agents.

const tool = new PubMedTool(options);
const result = await tool.invoke(query);

PubMedAPIWrapper

Core API wrapper with multiple access methods.

Methods:

  • run(query) - Get formatted search results string
  • load(query) - Get array of article metadata
  • loadDocs(query) - Get array of LangChain Documents
  • lazyLoad(query) - Async iterator over article metadata
  • lazyLoadDocs(query) - Async iterator over Documents

License

MIT

Credits

TypeScript port of the Python LangChain PubMed integration.

Resources