LangChain retrievers for Stack Overflow — search questions and fetch full answers across the Stack Exchange network as LangChain Document objects. No API keys required (optional for higher rate limits).
Perfect for building coding assistants, RAG applications, and troubleshooting agents.
| Retriever | What it does | API Key Required? |
|---|---|---|
StackOverflowSearchRetriever |
Search questions by keyword or tags | ❌ No! |
StackOverflowAnswerRetriever |
Fetch all answers for a question | ❌ No! |
pip install langchain-stackoverflowfrom langchain_stackoverflow import StackOverflowSearchRetriever
retriever = StackOverflowSearchRetriever(
max_results=3,
sort="votes", # "relevance", "votes", "creation", "activity"
site="stackoverflow", # any Stack Exchange site (e.g. "serverfault")
tagged="python;pandas", # Optional: filter by tags
accepted_only=True, # Optional: only show questions with accepted answers
)
docs = retriever.invoke("how to merge dataframes")
for doc in docs:
print(f"❓ {doc.page_content.splitlines()[0]}") # Print question title
print(f" 👍 {doc.metadata['score']} votes | 💬 {doc.metadata['answer_count']} answers")
print(f" 🔗 {doc.metadata['url']}")from langchain_stackoverflow import StackOverflowAnswerRetriever
retriever = StackOverflowAnswerRetriever(sort="votes")
# Pass the Stack Overflow URL or just the question ID
docs = retriever.invoke("https://stackoverflow.com/questions/53645882/pandas-merge-two-dataframes-with-different-columns")
for doc in docs:
if doc.metadata['is_accepted']:
print("✅ ACCEPTED ANSWER:")
else:
print("💡 ANSWER:")
print(f"Score: {doc.metadata['score']}")
print(f"{doc.page_content[:150]}...\n")By default, the Stack Exchange API allows 300 requests per day per IP address without an API key.
If you need more, you can register a free app at Stack Apps to get an API key, which increases your quota to 10,000 requests per day.
Pass your key to the retriever:
retriever = StackOverflowSearchRetriever(api_key="your_key_here")MIT — see LICENSE for details.