The @decartai/langchain package provides a LangChain Tool that gives any LLM agent the ability to generate and edit images using Decart.
Prerequisites
- A Decart API key
- Node.js 18+
- An LLM provider (Anthropic, OpenAI, etc.) for agent usage
Installation
Install the package along with the required @langchain/core peer dependency:
npm install @decartai/langchain @langchain/core
Setup
Set your Decart API key as an environment variable:
export DECART_API_KEY="your-api-key"
Get your API key from platform.decart.ai. See authentication for details.
Or pass your API key directly to the tool:
import { DecartImageTool } from '@decartai/langchain';
const tool = new DecartImageTool({ apiKey: 'your-api-key' });
Quick start
Text-to-image
Generate an image from a text prompt:
import { DecartImageTool } from '@decartai/langchain';
const tool = new DecartImageTool();
const image = await tool.invoke({
prompt: 'A serene mountain landscape at sunset',
resolution: '720p',
});
// Returns: data:image/png;base64,...
Image editing
Edit an existing image by providing an imageUrl:
const editedImage = await tool.invoke({
prompt: 'Change the sky to aurora borealis',
imageUrl: 'https://example.com/original.jpg',
});
The tool automatically selects the right model: lucy-pro-t2i for text-to-image (no imageUrl), and lucy-pro-i2i for image-to-image editing (with imageUrl).
Using with LLM agents
Bind the tool to an LLM to let it generate images on its own:
import { ChatAnthropic } from '@langchain/anthropic';
import { HumanMessage } from '@langchain/core/messages';
import { DecartImageTool } from '@decartai/langchain';
const llm = new ChatAnthropic({ model: 'claude-sonnet-4-20250514' });
const tool = new DecartImageTool();
const llmWithTools = llm.bindTools([tool]);
const response = await llmWithTools.invoke([
new HumanMessage('Generate an image of a futuristic city at night'),
]);
if (response.tool_calls?.length > 0) {
const result = await tool.invoke(response.tool_calls[0].args);
console.log('Generated image:', result);
}
This works with any LangChain-compatible LLM that supports tool calling — including OpenAI, Google, and Mistral models.
Parameters
Text description for image generation, or an edit instruction for image-to-image.
resolution
"480p" | "720p"
default:"720p"
Output resolution.
Output orientation. Only applies to text-to-image generation.
Seed value for reproducible results.
Source image URL for image-to-image editing. When provided, the tool uses lucy-pro-i2i instead of lucy-pro-t2i.
Automatically enhance the prompt for better results.
Model selection
The tool selects the model automatically based on your input:
| Input | Model | Mode |
|---|
prompt only | lucy-pro-t2i | Text-to-image generation |
prompt + imageUrl | lucy-pro-i2i | Image-to-image editing |
Constructor options
Decart API key. Defaults to the DECART_API_KEY environment variable.
Next steps