A compatibility bridge between DSPy and LangChain — use DSPy optimizers inside LangChain pipelines and LangChain tools inside DSPy modules.
Project description
dspy-langchain-bridge
A production-grade compatibility bridge between DSPy and 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dspy_langchain_bridge-0.4.0.tar.gz.
File metadata
- Download URL: dspy_langchain_bridge-0.4.0.tar.gz
- Upload date:
- Size: 41.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eba0bb492c3ca3ce2b34785cf88b010c9bb1fc67346b4cd8d210f8253142651d
|
|
| MD5 |
6618b4ea61fd9f0561dcbe49c78b36f5
|
|
| BLAKE2b-256 |
08e4ada76ac2b16ec20a1699837b0212c859938e1f6ebf7a2bfd7c22c985ba30
|
Provenance
The following attestation bundles were made for dspy_langchain_bridge-0.4.0.tar.gz:
Publisher:
release.yml on ANIMESHIOLOGY/dspy-langchain-bridge
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dspy_langchain_bridge-0.4.0.tar.gz -
Subject digest:
eba0bb492c3ca3ce2b34785cf88b010c9bb1fc67346b4cd8d210f8253142651d - Sigstore transparency entry: 1384454616
- Sigstore integration time:
-
Permalink:
ANIMESHIOLOGY/dspy-langchain-bridge@beb7e34ec2b007c25181add10438258e91d4fe6b -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/ANIMESHIOLOGY
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@beb7e34ec2b007c25181add10438258e91d4fe6b -
Trigger Event:
push
-
Statement type:
File details
Details for the file dspy_langchain_bridge-0.4.0-py3-none-any.whl.
File metadata
- Download URL: dspy_langchain_bridge-0.4.0-py3-none-any.whl
- Upload date:
- Size: 25.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05b9407f12f0c5a8dd3f1e1b6b8afe35e989d324e64187ebdeaee7bc5a8870dc
|
|
| MD5 |
d0b0cceab4255e76db1cdf2dcaa64627
|
|
| BLAKE2b-256 |
c4c086bd793815d95d947168464669926f73600255e96a44547ddb87865f29e4
|
Provenance
The following attestation bundles were made for dspy_langchain_bridge-0.4.0-py3-none-any.whl:
Publisher:
release.yml on ANIMESHIOLOGY/dspy-langchain-bridge
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dspy_langchain_bridge-0.4.0-py3-none-any.whl -
Subject digest:
05b9407f12f0c5a8dd3f1e1b6b8afe35e989d324e64187ebdeaee7bc5a8870dc - Sigstore transparency entry: 1384454620
- Sigstore integration time:
-
Permalink:
ANIMESHIOLOGY/dspy-langchain-bridge@beb7e34ec2b007c25181add10438258e91d4fe6b -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/ANIMESHIOLOGY
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@beb7e34ec2b007c25181add10438258e91d4fe6b -
Trigger Event:
push
-
Statement type: