LangChain.js just merged native support for ModelsLab's image generation API. If you're building AI agents or pipelines that need to generate images, you can now call Flux, SDXL, Stable Diffusion, and 50,000+ community models directly from your LangChain.js workflow — no glue code required.
This tutorial walks you through setting up ModelsLabTextToImage in LangChain.js, configuring models, and wiring it into agents and chains.
Why Use ModelsLab with LangChain.js?
Most LangChain.js image tools are locked to a single provider (DALL-E 3, Stable Diffusion via HuggingFace, etc.). ModelsLab gives you one API key and access to:
- Flux — the state-of-the-art open image model from Black Forest Labs
- SDXL — Stability AI's high-resolution model
- Stable Diffusion 3 — the latest SD generation
- 50,000+ community models — fine-tuned models for anime, photorealism, concept art, product photography, and more
The ModelsLabTextToImage tool extends LangChain's standard Tool interface, so it slots into agents, chains, and tool-calling workflows without any changes.
Installation
Install the LangChain community package if you haven't already:
npm install @langchain/community
# or
yarn add @langchain/community
No separate ModelsLab SDK needed — the tool uses the ModelsLab REST API directly.
Get Your ModelsLab API Key
Sign up at modelslab.com/account/api-key and copy your API key. Add it to your environment:
export MODELSLAB_API_KEY=your_api_key_here
The tool reads MODELSLAB_API_KEY automatically — same pattern as OpenAI, Anthropic, and other LangChain integrations.
Basic Usage
import { ModelsLabTextToImage } from "@langchain/community/tools/modelslab";
const tool = new ModelsLabTextToImage({
apiKey: process.env.MODELSLAB_API_KEY,
modelId: "flux", // use "sdxl", "stable-diffusion-v4", or any model ID
});
// Returns the image URL
const imageUrl = await tool.invoke("a serene mountain landscape at sunset");
console.log(imageUrl);
// https://cdn.modelslab.com/generated/abc123.png
The invoke() call handles ModelsLab's async processing automatically — if the API returns a processing status (which happens for complex prompts or busy queues), the tool polls until the image is ready and returns the final URL.
Configuring Models
ModelsLab supports different models via the modelId parameter. Here are the most useful ones:
// Flux — best for photorealistic, general purpose
const fluxTool = new ModelsLabTextToImage({
apiKey: process.env.MODELSLAB_API_KEY,
modelId: "flux",
});
// SDXL — high resolution, excellent for artistic styles
const sdxlTool = new ModelsLabTextToImage({
apiKey: process.env.MODELSLAB_API_KEY,
modelId: "sdxl",
});
// Stable Diffusion 3 — fast, good for iterations
const sd3Tool = new ModelsLabTextToImage({
apiKey: process.env.MODELSLAB_API_KEY,
modelId: "stable-diffusion-v3",
});
To use a community fine-tuned model, pass its model ID from the ModelsLab model library:
// Anime-style model from the community library
const animeTool = new ModelsLabTextToImage({
apiKey: process.env.MODELSLAB_API_KEY,
modelId: "anything-v3",
});
Image Dimensions and Sampling
Control output size and the number of images with width, height, and samples:
const tool = new ModelsLabTextToImage({
apiKey: process.env.MODELSLAB_API_KEY,
modelId: "flux",
width: 1024, // default: 512
height: 1024, // default: 512
samples: 1, // number of images to generate
});
const imageUrl = await tool.invoke(
"product photo of a minimalist mechanical keyboard, studio lighting, white background"
);
For most LangChain agent use cases, keep samples: 1 — the tool returns a single URL string, which is easiest to pipe to downstream steps.
Using ModelsLabTextToImage in a LangChain Agent
The real power comes from wiring this into an agent that can decide when to generate an image:
import { ChatOpenAI } from "@langchain/openai";
import { AgentExecutor, createToolCallingAgent } from "langchain/agents";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ModelsLabTextToImage } from "@langchain/community/tools/modelslab";
const imageTool = new ModelsLabTextToImage({
apiKey: process.env.MODELSLAB_API_KEY,
modelId: "flux",
width: 1024,
height: 1024,
});
const model = new ChatOpenAI({
model: "gpt-4o",
temperature: 0,
});
const prompt = ChatPromptTemplate.fromMessages([
["system", "You are a helpful assistant that can generate images when requested."],
["human", "{input}"],
["placeholder", "{agent_scratchpad}"],
]);
const agent = createToolCallingAgent({
llm: model,
tools: [imageTool],
prompt,
});
const executor = new AgentExecutor({
agent,
tools: [imageTool],
verbose: true,
});
// The agent decides when to call image generation
const result = await executor.invoke({
input: "Generate an image of a futuristic city at night with neon lights",
});
console.log(result.output);
// Returns the generated image URL
Combining with Other Tools
Combine ModelsLabTextToImage with other tools in multi-step workflows. For example, an agent that searches for style references and then generates an image:
import { TavilySearchResults } from "@langchain/community/tools/tavily_search";
import { ModelsLabTextToImage } from "@langchain/community/tools/modelslab";
const tools = [
new TavilySearchResults({ maxResults: 3 }),
new ModelsLabTextToImage({
apiKey: process.env.MODELSLAB_API_KEY,
modelId: "sdxl",
width: 1024,
height: 768,
}),
];
// Agent can search for style inspiration, then generate
const result = await executor.invoke({
input: "Find the visual style of Blade Runner 2049 and create an image in that style of a lone figure walking through a desert",
});
Handling the Async Processing Pattern
ModelsLab's API sometimes returns a processing status for jobs that take longer than a few seconds (large images, busy servers, or complex prompts). The ModelsLabTextToImage tool handles this automatically:
- On first call, if the API returns
status: "processing", the tool polls thefetch_urlreturned by the API - Polling continues until the image is ready or a timeout is hit
- The final URL is returned once processing completes
You don't need to manage this in your application code — it works the same as any synchronous tool call from your agent's perspective.
Real-World Use Cases
Here are practical applications where ModelsLabTextToImage adds value in LangChain.js workflows:
- AI content pipelines — Generate blog featured images, social media visuals, or product mockups from text descriptions
- Design assistant agents — Agents that iterate on image concepts based on user feedback
- Document generation — Automatically illustrate AI-generated reports, proposals, or educational materials
- E-commerce automation — Generate product images from specs when actual photography isn't available
- Game asset generation — Generate concept art and sprites from design documents in your game dev pipeline
API Pricing and Limits
ModelsLab offers a free tier with credits to get started, and pay-as-you-go pricing for production use. Most image generation calls cost a fraction of a cent — far cheaper than DALL-E 3 for equivalent quality with Flux or SDXL.
The REST API has no per-minute rate limits that would affect normal LangChain usage. For high-throughput scenarios (generating hundreds of images), contact ModelsLab for enterprise pricing.
Get Started
The ModelsLabTextToImage integration is available now in @langchain/community. Get your free API key at modelslab.com and start generating images in your LangChain.js agents today.
