Skip to main content
LangChain is a popular framework for building applications with large language models. You can use LangChain’s built-in DallEAPIWrapper to integrate Lumenfall for image generation.

Installation

pip install langchain langchain-community langchain-openai

Generate images

Use the DallEAPIWrapper from langchain_community with a custom API base:
from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper

# Configure DallEAPIWrapper for Lumenfall
dalle = DallEAPIWrapper(
    model="gemini-3-pro-image",
    openai_api_key="lmnfl_your_api_key",
    openai_api_base="https://api.lumenfall.ai/openai/v1"
)

# Generate an image
image_url = dalle.run("A serene mountain landscape at sunset")
print(image_url)

Using different models

# GPT Image 1.5
gpt_image = DallEAPIWrapper(
    model="gpt-image-1.5",
    openai_api_key="lmnfl_your_api_key",
    openai_api_base="https://api.lumenfall.ai/openai/v1"
)

# Flux.2 Max
flux = DallEAPIWrapper(
    model="flux.2-max",
    openai_api_key="lmnfl_your_api_key",
    openai_api_base="https://api.lumenfall.ai/openai/v1"
)

Use the built-in DALL-E tool

LangChain provides OpenAIDALLEImageGenerationTool for use with agents:
from langchain_community.tools.openai_dalle_image_generation import OpenAIDALLEImageGenerationTool
from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper

dalle = DallEAPIWrapper(
    model="gemini-3-pro-image",
    openai_api_key="lmnfl_your_api_key",
    openai_api_base="https://api.lumenfall.ai/openai/v1"
)

tool = OpenAIDALLEImageGenerationTool(api_wrapper=dalle)

# Use directly
result = tool.invoke("A futuristic cityscape with flying cars")
print(result)

Create a custom tool

For more control, create a custom tool using the @tool decorator:
from langchain.tools import tool
from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper

dalle = DallEAPIWrapper(
    model="gemini-3-pro-image",
    openai_api_key="lmnfl_your_api_key",
    openai_api_base="https://api.lumenfall.ai/openai/v1"
)

@tool
def generate_image(prompt: str) -> str:
    """Generate an image from a text description.

    Args:
        prompt: A detailed description of the image to generate.

    Returns:
        URL of the generated image.
    """
    return dalle.run(prompt)

Use with LangChain agents

Create an agent that can generate images on demand using the built-in tool:
from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate
from langchain_community.tools.openai_dalle_image_generation import OpenAIDALLEImageGenerationTool
from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper

# Configure DallEAPIWrapper for Lumenfall
dalle = DallEAPIWrapper(
    model="gemini-3-pro-image",
    openai_api_key="lmnfl_your_api_key",
    openai_api_base="https://api.lumenfall.ai/openai/v1"
)

# Create the image generation tool
image_tool = OpenAIDALLEImageGenerationTool(api_wrapper=dalle)

# Create the agent
llm = ChatOpenAI(model="gpt-4o")
tools = [image_tool]

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant that can generate images."),
    ("human", "{input}"),
    ("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# Run the agent
result = executor.invoke({
    "input": "Create an image of a futuristic city with flying cars"
})
print(result["output"])

Edit images

LangChain’s DallEAPIWrapper doesn’t support image editing, so create a custom tool:
from langchain.tools import tool
from io import BytesIO
import httpx
from openai import OpenAI

# For image editing, use the OpenAI client directly
client = OpenAI(
    api_key="lmnfl_your_api_key",
    base_url="https://api.lumenfall.ai/openai/v1"
)

@tool
def edit_image(image_url: str, prompt: str) -> str:
    """Edit an existing image based on a text prompt.

    Args:
        image_url: URL of the image to edit.
        prompt: Description of the changes to make.

    Returns:
        URL of the edited image.
    """
    # Download the image
    image_response = httpx.get(image_url)

    # Create a file-like object
    image_data = BytesIO(image_response.content)
    image_data.name = "image.png"

    response = client.images.edit(
        model="gpt-image-1.5",
        image=image_data,
        prompt=prompt,
        n=1,
        size="1024x1024"
    )
    return response.data[0].url

Chains with image generation

Create a chain that generates both text and images using LCEL (LangChain Expression Language):
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnableLambda
from langchain_community.utilities.dalle_image_generator import DallEAPIWrapper

llm = ChatOpenAI(model="gpt-4o")
dalle = DallEAPIWrapper(
    model="gemini-3-pro-image",
    openai_api_key="lmnfl_your_api_key",
    openai_api_base="https://api.lumenfall.ai/openai/v1"
)

# Chain to generate an image prompt
prompt_generator = ChatPromptTemplate.from_messages([
    ("system", "You are an expert at creating detailed image prompts. Given a topic, create a vivid, detailed prompt for an AI image generator."),
    ("human", "Create an image prompt for: {topic}")
])

# Build the chain using LCEL
chain = (
    prompt_generator
    | llm
    | StrOutputParser()
    | RunnableLambda(lambda prompt: {"prompt": prompt, "image_url": dalle.run(prompt)})
)

# Usage
result = chain.invoke({"topic": "a cozy coffee shop"})
print(f"Generated prompt: {result['prompt']}")
print(f"Image URL: {result['image_url']}")

Next steps