An integration package connecting Apify and LangChain
Project description
🎉 Apify MCP server released! 🎉
Apify has released its MCP (Model Context Protocol) server, which offers more features. You can use it through the LangChain MCP Adapter. It allows you to run Apify Actors, access Apify storage, search and read Apify documentation, and much more.
👉 https://mcp.apify.com 👈
LangChain Apify: A full-stack scraping platform built on Apify's infrastructure and LangChain's AI tools. Maintained by Apify.
Build web scraping and automation workflows in Python by connecting Apify Actors with LangChain. This package gives you programmatic access to Apify's infrastructure - run scraping tasks, handle datasets, and use the API directly through LangChain's tools.
Agentic LLMs
If you are an agent or an LLM, refer to the llms.txt file to get package context and learn how to work with this package.
Installation
pip install langchain-apify
Prerequisites
You should configure credentials by setting the following environment variables:
APIFY_API_TOKEN- Apify API token
Register your free Apify account here and learn how to get your API token in the Apify documentation.
Tools
ApifyActorsTool class provides access to Apify Actors, which are cloud-based web scraping and automation programs that you can run without managing any infrastructure. For more detailed information, see the Apify Actors documentation.
ApifyActorsTool is useful when you need to run an Apify Actor as a tool in LangChain. You can use the tool to interact with the Actor manually or as part of an agent workflow.
Example usage of ApifyActorsTool with the RAG Web Browser Actor, which searches for information on the web:
import os
import json
from langchain_apify import ApifyActorsTool
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"
os.environ["APIFY_API_TOKEN"] = "YOUR_APIFY_API_TOKEN"
browser = ApifyActorsTool('apify/rag-web-browser')
search_results = browser.invoke(input={
"run_input": {"query": "what is Apify Actor?", "maxResults": 3}
})
# use the tool with an agent
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
model = ChatOpenAI(model="gpt-4o-mini")
tools = [browser]
agent = create_react_agent(model, tools)
for chunk in agent.stream(
{"messages": [("human", "search for what is Apify?")]},
stream_mode="values"
):
chunk["messages"][-1].pretty_print()
Document loaders
ApifyDatasetLoader class provides access to Apify datasets as document loaders. Datasets are storage solutions that store results from web scraping, crawling, or data processing.
ApifyDatasetLoader is useful when you need to process data from an Apify Actor run. If you are extracting webpage content, you would typically use this loader after running an Apify Actor manually from the Apify console, where you can access the results stored in the dataset.
Example usage for ApifyDatasetLoader with a custom dataset mapping function for loading webpage content and source URLs as a list of Document objects containing the page content and source URL.
import os
from langchain_apify import ApifyDatasetLoader
os.environ["APIFY_API_TOKEN"] = "YOUR_APIFY_API_TOKEN"
# Example dataset structure
# [
# {
# "text": "Example text from the website.",
# "url": "http://example.com"
# },
# ...
# ]
loader = ApifyDatasetLoader(
dataset_id="your-dataset-id",
dataset_mapping_function=lambda dataset_item: Document(
page_content=dataset_item["text"],
metadata={"source": dataset_item["url"]}
),
)
Wrappers
ApifyWrapper class wraps the Apify API to easily convert Apify datasets into documents. It is useful when you need to run an Apify Actor programmatically and process the results in LangChain. Available methods include:
- call_actor: Runs an Apify Actor and returns an
ApifyDatasetLoaderfor the results. - acall_actor: Asynchronous version of
call_actor. - call_actor_task: Runs a saved Actor task and returns an
ApifyDatasetLoaderfor the results. Actor tasks allow you to create and reuse multiple configurations of a single Actor for different use cases. - acall_actor_task: Asynchronous version of
call_actor_task.
For more information, see the Apify LangChain integration documentation.
Example usage for call_actor involves running the Website Content Crawler Actor, which extracts content from webpages. The wrapper then returns the results as a list of Document objects containing the page content and source URL:
import os
from langchain_apify import ApifyWrapper
from langchain_core.documents import Document
os.environ["APIFY_API_TOKEN"] = "YOUR_APIFY_API_TOKEN"
apify = ApifyWrapper()
loader = apify.call_actor(
actor_id="apify/website-content-crawler",
run_input={
"startUrls": [{"url": "https://python.langchain.com/docs/get_started/introduction"}],
"maxCrawlPages": 10,
"crawlerType": "cheerio"
},
dataset_mapping_function=lambda item: Document(
page_content=item["text"] or "",
metadata={"source": item["url"]}
),
)
documents = loader.load()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_apify-0.1.4.tar.gz.
File metadata
- Download URL: langchain_apify-0.1.4.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dfe5d6ae5731f286e3cb84bfd66003fc195057beb6377364e9b5604086dc4305
|
|
| MD5 |
cc7817a7c25fb0e2c581783861004f57
|
|
| BLAKE2b-256 |
45a0385e28434005341d1acaf15a7ed4fb528e8105995ce843f64b940e1a338e
|
Provenance
The following attestation bundles were made for langchain_apify-0.1.4.tar.gz:
Publisher:
release.yml on apify/langchain-apify
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_apify-0.1.4.tar.gz -
Subject digest:
dfe5d6ae5731f286e3cb84bfd66003fc195057beb6377364e9b5604086dc4305 - Sigstore transparency entry: 410114838
- Sigstore integration time:
-
Permalink:
apify/langchain-apify@1174d64455b91154f7f5c2482a649385a96638e4 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/apify
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@1174d64455b91154f7f5c2482a649385a96638e4 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file langchain_apify-0.1.4-py3-none-any.whl.
File metadata
- Download URL: langchain_apify-0.1.4-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06a36685d14eabefce2d7cc6bfdd0b76dd537b42b587c1a9fd6b79044a6bd6e1
|
|
| MD5 |
b068321fe79c912412eeeabfef92ee7a
|
|
| BLAKE2b-256 |
c5dccc67014b6c5e74486c4bca18a78d395b9f308074ff9b6745a0bbf7a64d27
|
Provenance
The following attestation bundles were made for langchain_apify-0.1.4-py3-none-any.whl:
Publisher:
release.yml on apify/langchain-apify
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
langchain_apify-0.1.4-py3-none-any.whl -
Subject digest:
06a36685d14eabefce2d7cc6bfdd0b76dd537b42b587c1a9fd6b79044a6bd6e1 - Sigstore transparency entry: 410114875
- Sigstore integration time:
-
Permalink:
apify/langchain-apify@1174d64455b91154f7f5c2482a649385a96638e4 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/apify
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@1174d64455b91154f7f5c2482a649385a96638e4 -
Trigger Event:
workflow_dispatch
-
Statement type: