LangChain

View as Markdown

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 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

$pip install -U langchain-youdotcom

Set your API key

$export YDC_API_KEY=your_api_key

Get your API key at 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.

1from langchain_youdotcom import YouSearchTool
2
3tool = YouSearchTool()
4result = tool.invoke("latest developments in quantum computing")
5print(result)

To customize search parameters, pass them via api_wrapper:

1from langchain_youdotcom import YouSearchAPIWrapper, YouSearchTool
2
3tool = YouSearchTool(
4 api_wrapper=YouSearchAPIWrapper(
5 count=5,
6 livecrawl="web", # "web", "news", or "all"
7 freshness="day", # "day", "week", "month", or "year"
8 safesearch="moderate", # "off", "moderate", or "strict"
9 )
10)
11result = tool.invoke("AI news today")

YouContentsTool

YouContentsTool fetches and extracts clean content from web pages.

1from langchain_youdotcom import YouContentsTool
2
3tool = YouContentsTool()
4result = tool.invoke({"urls": ["https://python.langchain.com"]})
5print(result[:500])

YouRetriever

YouRetriever implements LangChain’s retriever interface, making it a drop-in for any RAG pipeline.

1from langchain_youdotcom import YouRetriever
2
3retriever = YouRetriever(count=5)
4docs = retriever.invoke("climate change policy updates")
5
6for doc in docs:
7 print(doc.metadata["url"])
8 print(doc.page_content[:300])
9 print()

To use it in a retrieval chain:

1from langchain_youdotcom import YouRetriever
2from langchain_openai import ChatOpenAI
3from langchain_core.output_parsers import StrOutputParser
4from langchain_core.prompts import ChatPromptTemplate
5from langchain_core.runnables import RunnablePassthrough
6
7retriever = YouRetriever(count=5, livecrawl="web")
8llm = ChatOpenAI(model="gpt-4o-mini")
9
10prompt = ChatPromptTemplate.from_template(
11 """Answer based on the following context:
12
13{context}
14
15Question: {question}"""
16)
17
18
19def format_docs(docs):
20 return "\n\n".join(doc.page_content for doc in docs)
21
22
23chain = (
24 {"context": retriever | format_docs, "question": RunnablePassthrough()}
25 | prompt
26 | llm
27 | StrOutputParser()
28)
29
30for chunk in chain.stream("What is the current state of fusion energy research?"):
31 print(chunk, end="", flush=True)

Agent with LangGraph

Both tools work with any LangChain agent. Here’s an example using create_agent:

1from langchain_openai import ChatOpenAI
2from langchain_youdotcom import YouSearchTool, YouContentsTool
3from langchain.agents import create_agent
4
5llm = ChatOpenAI(model="gpt-4o-mini")
6tools = [YouSearchTool(), YouContentsTool()]
7agent = create_agent(llm, tools)
8
9response = agent.invoke(
10 {"messages": [{"role": "user", "content": "What are the top AI news stories this week?"}]}
11)
12print(response["messages"][-1].content)

Community package

You.com is also available via langchain-community:

1from langchain_community.retrievers import YouRetriever
2from 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