# Integration Langchain

> LangChain interoperability patterns for DSPy

- Skill: `j33bs/integration-langchain` (Agent Skill)
- Install (CLI): `npx skillmds@latest add j33bs/integration-langchain`
- Raw SKILL.md: https://api.skillmd.com/api/skills/j33bs/integration-langchain/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Integrations & APIs
- Author: j33bs (https://skillmd.com/u/j33bs)
- Updated: 2026-09-22
- Page: https://skillmd.com/skills/j33bs/integration-langchain

---


# LangChain Integration

## 🎯 Trigger Conditions
Use when asked about integrating DSPy with LangChain, combining frameworks, or building hybrid pipelines.

## 📚 Prerequisites
- `langchain` package installed
- `dspy` package installed
- Understanding of both frameworks

## 🛠️ Integration Patterns

### 1. DSPy as LangChain LLM
```python
from langchain.llms import LLM
import dspy

class DSPyLLM(LLM):
    def __init__(self, dspy_program):
        self.program = dspy_program
    
    @property
    def _identifying_params(self):
        return {"name": "dspy-program"}
    
    @property
    def _llm_type(self):
        return "dspy"
    
    def _call(self, prompt, stop=None):
        # Convert LangChain prompt to DSPy input
        result = self.program(question=prompt)
        return result.answer

# Use with LangChain
dspy_llm = DSPyLLM(your_dspy_program)
chain = dspy_llm | your_output_parser
```

### 2. LangChain as DSPy Tool
```python
from langchain.tools import Tool
import dspy

# Create LangChain tool
def langchain_tool(query):
    # Use LangChain for specific task
    result = your_langchain_chain.run(query)
    return result

langchain_tool = Tool(
    name="langchain-tool",
    func=langchain_tool,
    description="Use LangChain for..."
)

# Use with DSPy
class HybridProgram(dspy.Module):
    def __init__(self):
        self.dspy_step = dspy.Predict("input -> intermediate")
        self.langchain_tool = langchain_tool
    
    def forward(self, input):
        intermediate = self.dspy_step(input=input).intermediate
        result = self.langchain_tool(intermediate)
        return result
```

### 3. Hybrid Pipeline
```python
class HybridPipeline(dspy.Module):
    def __init__(self):
        self.dspy_retrieve = dspy.Retrieve(k=3)
        self.langchain_summarize = your_langchain_summarizer
        self.dspy_answer = dspy.Predict("context, question -> answer")
    
    def forward(self, question):
        # DSPy for retrieval
        context = self.dspy_retrieve(question).passages
        
        # LangChain for summarization
        summary = self.langchain_summarize(context)
        
        # DSPy for answer generation
        return self.dspy_answer(context=summary, question=question)
```

### 4. LangChain Chains with DSPy
```python
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate

# Create LangChain chain with DSPy
prompt = PromptTemplate(
    input_variables=["question"],
    template="Answer: {question}"
)

dspy_llm = DSPyLLM(your_program)
chain = LLMChain(llm=dspy_llm, prompt=prompt)

# Run chain
result = chain.run("Your question")
```

## ⚠️ Pitfalls
- **Complexity**: Combining frameworks increases complexity
- **Performance**: Additional abstraction layers add latency
- **Compatibility**: Not all components integrate smoothly
- **Debugging**: Cross-framework debugging is harder

## 📖 References
- [LangChain Documentation](https://python.langchain.com/)
- [DSPy Integration](https://dspy-docs.vercel.app/docs/integration)

