DSPy with LangChain integration
Quick answer
Use
dspy to define structured LLM signatures and predictions, then integrate with langchain_openai.ChatOpenAI by wrapping DSPy calls inside LangChain chains or tools. This enables combining DSPy's declarative LLM programming with LangChain's orchestration and vectorstore capabilities.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0 dspy langchain_openai langchain_community
Setup
Install the required packages with pip and set your OpenAI API key in the environment.
pip install openai dspy langchain_openai langchain_community- Set environment variable:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows)
pip install openai dspy langchain_openai langchain_community Step by step
This example shows how to define a DSPy signature for a simple Q&A task, create a DSPy predictor using OpenAI's gpt-4o-mini, then wrap it in a LangChain chain for integration.
import os
from openai import OpenAI
import dspy
from langchain_openai import ChatOpenAI
from langchain_core.schema import HumanMessage
# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Define DSPy LM wrapper using OpenAI client
lm = dspy.LM("openai/gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"])
dspy.configure(lm=lm)
# Define a DSPy signature for Q&A
class QA(dspy.Signature):
question: str = dspy.InputField()
answer: str = dspy.OutputField()
# Create a DSPy predictor
qa = dspy.Predict(QA)
# LangChain wrapper function
class DSPyChain:
def __init__(self, predictor):
self.predictor = predictor
def __call__(self, input_text: str) -> str:
result = self.predictor(question=input_text)
return result.answer
# Instantiate LangChain ChatOpenAI (for demonstration, not used directly here)
chat_llm = ChatOpenAI(model_name="gpt-4o-mini", openai_api_key=os.environ["OPENAI_API_KEY"])
# Wrap DSPy predictor in LangChain chain
dspy_chain = DSPyChain(qa)
# Run example
question = "What is Retrieval-Augmented Generation?"
answer = dspy_chain(question)
print("Q:", question)
print("A:", answer) output
Q: What is Retrieval-Augmented Generation? A: Retrieval-Augmented Generation (RAG) is a technique that combines retrieval of relevant documents with language model generation to produce more accurate and context-aware responses.
Common variations
- Use different OpenAI models by changing
"openai/gpt-4o-mini"to another DSPy-supported model. - Integrate DSPy with LangChain tools or agents by wrapping DSPy calls inside LangChain tool classes.
- Use async DSPy calls with
awaitif DSPy predictor supports async.
Troubleshooting
- If you get authentication errors, verify your
OPENAI_API_KEYenvironment variable is set correctly. - If DSPy prediction fails, ensure you have the latest
dspyandopenaipackages installed. - For model not found errors, confirm the model name matches DSPy's supported models and your API key has access.
Key Takeaways
- Use
dspy.LMto configure DSPy with OpenAI models for structured LLM calls. - Wrap DSPy predictors in simple Python classes to integrate with LangChain chains or tools.
- Leverage LangChain's orchestration while using DSPy for declarative LLM programming.
- Always use environment variables for API keys to keep credentials secure.
- Test your integration with simple Q&A before scaling to complex chains or agents.