Skip to main content

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.

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
v0.1.0 Core bridge: DSPyRunnable, LangChainLM, signature converters
v0.2.0 Retriever & memory bridges
v0.3.0 Agent & tool interop + LangGraph bridges
v0.4.0 Optimizer bridge
v1.0.0 Stable release, full docs, streaming, mypy compliance

Contributing

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

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dspy_langchain_bridge-0.1.0.tar.gz (40.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dspy_langchain_bridge-0.1.0-py3-none-any.whl (24.9 kB view details)

Uploaded Python 3

File details

Details for the file dspy_langchain_bridge-0.1.0.tar.gz.

File metadata

  • Download URL: dspy_langchain_bridge-0.1.0.tar.gz
  • Upload date:
  • Size: 40.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dspy_langchain_bridge-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a864746a3f9cfd926d5f878e9300d5afba8b9f206aff08b253a93d6fd65610a3
MD5 58a96d9e08558476cbaf0ca9faa75480
BLAKE2b-256 9fdbdc5fb77554d07ed100c0d77f14c571a0095e32cde914a05158498e05cd44

See more details on using hashes here.

Provenance

The following attestation bundles were made for dspy_langchain_bridge-0.1.0.tar.gz:

Publisher: release.yml on ANIMESHIOLOGY/dspy-langchain-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dspy_langchain_bridge-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dspy_langchain_bridge-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 be43e468ee7047147ca95399ac211df01ce570fb536effb0fe29bbd3301d2118
MD5 2db792260eff121b36c1e09796b14740
BLAKE2b-256 146bf9b60fa7b23df01283fbc5132c5733c3f4d4de7147e7d88e2772ab16ae45

See more details on using hashes here.

Provenance

The following attestation bundles were made for dspy_langchain_bridge-0.1.0-py3-none-any.whl:

Publisher: release.yml on ANIMESHIOLOGY/dspy-langchain-bridge

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page