Skip to main content

dspy-langchain-bridge

A production-grade compatibility bridge between DSPy and LangChain.

PyPI version CI License: MIT Python 3.9+ DSPy LangChain


The Problem

DSPy and LangChain are the two most widely used LLM frameworks — and they don't talk to each other.

  • LangChain has a massive ecosystem: tools, retrievers, agents, memory, LangGraph.
  • DSPy has the best prompt optimizers in the ecosystem (BootstrapFewShot, MIPROv2, COPRO).

If you want DSPy's optimization inside a LangChain pipeline — or LangChain's retrievers inside a DSPy RAG module — you're currently on your own. dspy-langchain-bridge solves that.


Installation

pip install dspy-langchain-bridge

With optional extras:

pip install "dspy-langchain-bridge[openai,langgraph]"   # OpenAI + LangGraph support
pip install "dspy-langchain-bridge[all]"                # Everything

Quick Start

Use a DSPy module inside a LangChain chain

import dspy
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from dspy_lc_bridge import DSPyRunnable, LangChainLM

# Configure DSPy to use any LangChain LLM
llm = ChatOpenAI(model="gpt-4o")
dspy.settings.configure(lm=LangChainLM(llm))

# Build a DSPy module
cot = dspy.ChainOfThought("question -> answer")

# Drop it into an LCEL chain
prompt = ChatPromptTemplate.from_template("{question}")
chain = prompt | DSPyRunnable(cot)

result = chain.invoke({"question": "What is the capital of France?"})
print(result["answer"])

Use a LangChain retriever inside DSPy RAG

from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from dspy_lc_bridge import LangChainRetriever

vectorstore = FAISS.load_local("my_index", OpenAIEmbeddings())
retriever = LangChainRetriever(vectorstore.as_retriever(), k=3)

class RAG(dspy.Module):
    def __init__(self):
        self.retrieve = retriever
        self.generate = dspy.ChainOfThought("context, question -> answer")

    def forward(self, question):
        passages = self.retrieve(question).passages
        context = "\n".join(passages)
        return self.generate(context=context, question=question)

Optimize a LangChain prompt with DSPy

from langchain_core.prompts import ChatPromptTemplate
from dspy.teleprompt import BootstrapFewShot
from dspy_lc_bridge import optimize_langchain_prompt

prompt = ChatPromptTemplate.from_template("Answer the question: {question}")

optimized = optimize_langchain_prompt(
    prompt=prompt,
    optimizer=BootstrapFewShot(metric=my_metric),
    trainset=my_examples,
)
# optimized is a ChatPromptTemplate with few-shot examples injected

Compatibility Matrix

DSPy Primitive LangChain Equivalent Bridge Class / Function
dspy.Module Runnable DSPyRunnable
LangChain BaseLLM / BaseChatModel DSPy LM backend LangChainLM
dspy.Signature PromptTemplate signature_to_prompt / prompt_to_signature
LangChain BaseRetriever dspy.Retrieve LangChainRetriever
dspy.Retrieve LangChain BaseRetriever DSPyRetriever
dspy.Module LangChain BaseTool DSPyTool
LangChain BaseTool DSPy tool LangChainTool
dspy.Module LangGraph node function DSPyNode
LangChain BaseMemory DSPy history LangChainMemoryAdapter
DSPy optimizer LangChain prompt optimizer optimize_langchain_prompt

Public API

from dspy_lc_bridge import (
    # Core
    DSPyRunnable,              # DSPy module → LangChain Runnable
    LangChainLM,               # LangChain LLM → DSPy LM backend

    # Signatures
    signature_to_prompt,       # dspy.Signature → PromptTemplate
    prompt_to_signature,       # PromptTemplate → dspy.Signature

    # Retrievers
    LangChainRetriever,        # LangChain retriever → DSPy Retrieve
    DSPyRetriever,             # DSPy Retrieve → LangChain BaseRetriever

    # Tools & Agents
    DSPyTool,                  # DSPy module → LangChain Tool
    LangChainTool,             # LangChain Tool → DSPy tool
    DSPyNode,                  # DSPy module → LangGraph node

    # Memory
    LangChainMemoryAdapter,    # LangChain memory → DSPy history

    # Optimizers
    optimize_langchain_prompt, # DSPy optimizer → optimized LangChain prompt
)

Roadmap

Version Contents Status
v0.1.0 Core bridge: DSPyRunnable, LangChainLM, signature converters ✅ Released
v0.2.0 Retriever & memory bridges ✅ Released
v0.3.0 Agent & tool interop + LangGraph bridges ✅ Released
v0.4.0 Optimizer bridge ✅ Released
v1.0.0 Stable release, full docs, streaming, mypy compliance 🔜 Planned

Contributing

See CONTRIBUTING.md. PRs, issues, and discussions are welcome.

License

MIT

Metadata

Release files for dspy-langchain-bridge 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dspy-langchain-bridge 0.4.0
File Size Uploaded
dspy_langchain_bridge-0.4.0.tar.gz 41.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dspy-langchain-bridge 0.4.0
File Interpreter ABI Platform
dspy_langchain_bridge-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 66.2 kB

Release files / dspy_langchain_bridge-0.4.0.tar.gz

Download URL dspy_langchain_bridge-0.4.0.tar.gz
Size 41.2 kB
Tags Source
SHA-256 checksum
How to use checksums
eba0bb492c3ca3ce2b34785cf88b010c9bb1fc67346b4cd8d210f8253142651d
BLAKE2b-256 checksum
How to use checksums
08e4ada76ac2b16ec20a1699837b0212c859938e1f6ebf7a2bfd7c22c985ba30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 25, 2026.

Transparency log

Release files / dspy_langchain_bridge-0.4.0-py3-none-any.whl

Download URL dspy_langchain_bridge-0.4.0-py3-none-any.whl
Size 25.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
05b9407f12f0c5a8dd3f1e1b6b8afe35e989d324e64187ebdeaee7bc5a8870dc
BLAKE2b-256 checksum
How to use checksums
c4c086bd793815d95d947168464669926f73600255e96a44547ddb87865f29e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 25, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page