*** title: LangChain 'og:title': You.com + LangChain | Real-Time Web Search for LangChain Agents 'og:description': >- Add real-time web search to LangChain agents and RAG pipelines using the langchain-youdotcom package. ---------------------------- LangChain is one of the most popular frameworks for building LLM-powered applications. By connecting You.com's search and content APIs to LangChain, you can ground your agents and RAG pipelines in real-time, accurate web data instead of relying on static training knowledge alone. The [`langchain-youdotcom`](https://pypi.org/project/langchain-youdotcom/) package provides three integration points: a search tool (`YouSearchTool`), a content extraction tool (`YouContentsTool`), and a retriever (`YouRetriever`). Drop them into any LangChain agent or chain to give it live web access. *** ## Getting Started ### Install the package ```bash pip install -U langchain-youdotcom ``` ### Set your API key ```bash export YDC_API_KEY=your_api_key ``` Get your API key at [you.com/platform/api-keys](https://you.com/platform/api-keys). *** ## Usage ### YouSearchTool `YouSearchTool` wraps the You.com Search API as a LangChain tool, making it available to any agent that uses tools. ```python from langchain_youdotcom import YouSearchTool tool = YouSearchTool() result = tool.invoke("latest developments in quantum computing") print(result) ``` To customize search parameters, pass them via `api_wrapper`: ```python from langchain_youdotcom import YouSearchAPIWrapper, YouSearchTool tool = YouSearchTool( api_wrapper=YouSearchAPIWrapper( count=5, livecrawl="web", # "web", "news", or "all" freshness="day", # "day", "week", "month", or "year" safesearch="moderate", # "off", "moderate", or "strict" ) ) result = tool.invoke("AI news today") ``` ### YouContentsTool `YouContentsTool` fetches and extracts clean content from web pages. ```python from langchain_youdotcom import YouContentsTool tool = YouContentsTool() result = tool.invoke({"urls": ["https://python.langchain.com"]}) print(result[:500]) ``` ### YouRetriever `YouRetriever` implements LangChain's retriever interface, making it a drop-in for any RAG pipeline. ```python from langchain_youdotcom import YouRetriever retriever = YouRetriever(count=5) docs = retriever.invoke("climate change policy updates") for doc in docs: print(doc.metadata["url"]) print(doc.page_content[:300]) print() ``` To use it in a retrieval chain: ```python from langchain_youdotcom import YouRetriever from langchain_openai import ChatOpenAI from langchain_core.output_parsers import StrOutputParser from langchain_core.prompts import ChatPromptTemplate from langchain_core.runnables import RunnablePassthrough retriever = YouRetriever(count=5, livecrawl="web") llm = ChatOpenAI(model="gpt-4o-mini") prompt = ChatPromptTemplate.from_template( """Answer based on the following context: {context} Question: {question}""" ) def format_docs(docs): return "\n\n".join(doc.page_content for doc in docs) chain = ( {"context": retriever | format_docs, "question": RunnablePassthrough()} | prompt | llm | StrOutputParser() ) for chunk in chain.stream("What is the current state of fusion energy research?"): print(chunk, end="", flush=True) ``` ### Agent with LangGraph Both tools work with any LangChain agent. Here's an example using `create_agent`: ```python from langchain_openai import ChatOpenAI from langchain_youdotcom import YouSearchTool, YouContentsTool from langchain.agents import create_agent llm = ChatOpenAI(model="gpt-4o-mini") tools = [YouSearchTool(), YouContentsTool()] agent = create_agent(llm, tools) response = agent.invoke( {"messages": [{"role": "user", "content": "What are the top AI news stories this week?"}]} ) print(response["messages"][-1].content) ``` *** ## Community package You.com is also available via `langchain-community`: ```python from langchain_community.retrievers import YouRetriever from langchain_community.tools.you import YouSearchTool ``` The standalone `langchain-youdotcom` package is recommended for new projects as it stays up to date with the latest You.com API features. *** ## Resources Official LangChain documentation for YouSearchTool Official LangChain documentation for YouRetriever Full Search API parameters and response schema Full Contents API parameters and response schema