| title | Data Source Plugin |
|---|
Data source plugins are a new type of plugin introduced in Dify 1.9.0. In a knowledge pipeline, they serve as the document data source and the starting point for the entire pipeline.
This article describes how to develop a data source plugin, covering plugin architecture, code examples, and debugging methods, to help you quickly develop and launch your data source plugin.
Before reading on, ensure you have a basic understanding of the knowledge pipeline and some knowledge of plugin development. You can find relevant information here:
Dify supports three types of data source plugins: web crawler, online document, and online drive. When implementing the plugin code, the class that provides the plugin's functionality must inherit from a specific data source class. Each of the three plugin types corresponds to a different parent class.
To learn how to inherit from a parent class to implement plugin functionality, see [Tool Plugin: Preparing Tool Code](/en/develop-plugin/dev-guides-and-walkthroughs/tool-plugin#4-preparing-tool-code).Each data source plugin type supports multiple data sources. For example:
- Web Crawler: Jina Reader, FireCrawl
- Online Document: Notion, Confluence, GitHub
- Online Drive: OneDrive, Google Drive, Box, AWS S3, Tencent COS
The relationship between data source types and data source plugin types is illustrated below.
You can use the scaffolding command-line tool to create a data source plugin by selecting the datasource type. After completing the setup, the command-line tool will automatically generate the plugin project code.
dify plugin initA data source plugin consists of three main components:
- The
manifest.yamlfile: Describes the basic information about the plugin. - The
providerdirectory: Contains the plugin provider's description and authentication implementation code. - The
datasourcesdirectory: Contains the description and core logic for fetching data from the data source.
├── _assets
│ └── icon.svg
├── datasources
│ ├── your_datasource.py
│ └── your_datasource.yaml
├── main.py
├── manifest.yaml
├── PRIVACY.md
├── provider
│ ├── your_datasource.py
│ └── your_datasource.yaml
├── README.md
└── requirements.txt
-
In the
manifest.yamlfile, set the minimum supported Dify version as follows:minimum_dify_version: 1.9.0
-
In the
manifest.yamlfile, add the following tag to display the plugin under the data source category in the Dify Marketplace:tags: - rag
-
In the
requirements.txtfile, set the plugin SDK version used for data source plugin development as follows:dify-plugin>=0.5.0,<0.6.0
The content of a provider YAML file is essentially the same as that for tool plugins, with only the following two differences:
# Specify the provider type for the data source plugin: online_drive, online_document, or website_crawl
provider_type: online_drive # online_document, website_crawl
# Specify data sources
datasources:
- datasources/PluginName.yamlTo configure OAuth, see Add OAuth Support to Your Tool Plugin.
-
When using API Key authentication mode, the provider code file for data source plugins is identical to that for tool plugins. You only need to change the parent class inherited by the provider class to
DatasourceProvider.class YourDatasourceProvider(DatasourceProvider): def _validate_credentials(self, credentials: Mapping[str, Any]) -> None: try: """ IMPLEMENT YOUR VALIDATION HERE """ except Exception as e: raise ToolProviderCredentialValidationError(str(e))
-
When using OAuth authentication mode, data source plugins differ slightly from tool plugins. When obtaining access permissions via OAuth, data source plugins can simultaneously return the username and avatar to be displayed on the frontend. Therefore,
_oauth_get_credentialsand_oauth_refresh_credentialsneed to return aDatasourceOAuthCredentialstype that containsname,avatar_url,expires_at, andcredentials.The
DatasourceOAuthCredentialsclass is defined as follows and must be set to the corresponding type when returned:class DatasourceOAuthCredentials(BaseModel): name: str | None = Field(None, description="The name of the OAuth credential") avatar_url: str | None = Field(None, description="The avatar url of the OAuth") credentials: Mapping[str, Any] = Field(..., description="The credentials of the OAuth") expires_at: int | None = Field( default=-1, description="""The expiration timestamp (in seconds since Unix epoch, UTC) of the credentials. Set to -1 or None if the credentials do not expire.""", )
The function signatures for _oauth_get_authorization_url, _oauth_get_credentials, and _oauth_refresh_credentials are as follows:
The YAML file format and data source code format vary across the three types of data sources.
In the provider YAML file for a web crawler data source plugin, output_schema must always return four parameters: source_url, content, title, and description.
output_schema:
type: object
properties:
source_url:
type: string
description: the source url of the website
content:
type: string
description: the content from the website
title:
type: string
description: the title of the website
"description":
type: string
description: the description of the websiteIn the main logic code for a web crawler plugin, the class must inherit from WebsiteCrawlDatasource and implement the _get_website_crawl method. You then need to use the create_crawl_message method to return the web crawl message.
To crawl multiple web pages and return them in batches, you can set WebSiteInfo.status to processing and use the create_crawl_message method to return each batch of crawled pages. After all pages have been crawled, set WebSiteInfo.status to completed.
class YourDataSource(WebsiteCrawlDatasource):
def _get_website_crawl(
self, datasource_parameters: dict[str, Any]
) -> Generator[ToolInvokeMessage, None, None]:
crawl_res = WebSiteInfo(web_info_list=[], status="", total=0, completed=0)
crawl_res.status = "processing"
yield self.create_crawl_message(crawl_res)
### your crawl logic
...
crawl_res.status = "completed"
crawl_res.web_info_list = [
WebSiteInfoDetail(
title="",
source_url="",
description="",
content="",
)
]
crawl_res.total = 1
crawl_res.completed = 1
yield self.create_crawl_message(crawl_res)The return value for an online document data source plugin must include at least a content field to represent the document's content. For example:
output_schema:
type: object
properties:
workspace_id:
type: string
description: workspace id
page_id:
type: string
description: page id
content:
type: string
description: page contentIn the main logic code for an online document plugin, the class must inherit from OnlineDocumentDatasource and implement two methods: _get_pages and _get_content.
When a user runs the plugin, it first calls the _get_pages method to retrieve a list of documents. After the user selects a document from the list, it then calls the _get_content method to fetch the document's content.
An online drive data source plugin returns a file, so it must adhere to the following specification:
output_schema:
type: object
properties:
file:
$ref: "https://dify.ai/schemas/v1/file.json"In the main logic code for an online drive plugin, the class must inherit from OnlineDriveDatasource and implement two methods: _browse_files and _download_file.
When a user runs the plugin, it first calls _browse_files to get a file list. At this point, prefix is empty, indicating a request for the root directory's file list. The file list contains both folder and file type variables. If the user opens a folder, the _browse_files method is called again. At this point, the prefix in OnlineDriveBrowseFilesRequest will be the folder ID used to retrieve the file list within that folder.
After a user selects a file, the plugin uses the _download_file method and the file ID to get the file's content. You can use the _get_mime_type_from_filename method to get the file's MIME type, allowing the pipeline to handle different file types appropriately.
When the file list contains multiple files, you can set OnlineDriveFileBucket.is_truncated to True and set OnlineDriveFileBucket.next_page_parameters to the parameters needed to fetch the next page of the file list, such as the next page's request ID or URL, depending on the service provider.
credentials = self.runtime.credentials
bucket_name = request.bucket
prefix = request.prefix or "" # Allow empty prefix for root folder; When you browse the folder, the prefix is the folder id
max_keys = request.max_keys or 10
next_page_parameters = request.next_page_parameters or {}
files = []
files.append(OnlineDriveFile(
id="",
name="",
size=0,
type="folder" # or "file"
))
return OnlineDriveBrowseFilesResponse(result=[
OnlineDriveFileBucket(
bucket="",
files=files,
is_truncated=False,
next_page_parameters={}
)
])
```
file_content = bytes()
file_name = ""
mime_type = self._get_mime_type_from_filename(file_name)
yield self.create_blob_message(file_content, meta={
"file_name": file_name,
"mime_type": mime_type
})
def _get_mime_type_from_filename(self, filename: str) -> str:
"""Determine MIME type from file extension."""
import mimetypes
mime_type, _ = mimetypes.guess_type(filename)
return mime_type or "application/octet-stream"
```
For storage services like AWS S3, the prefix, bucket, and id variables have special uses and can be applied flexibly as needed during development:
prefix: Represents the file path prefix. For example,prefix=container1/folder1/retrieves the files or file list from thefolder1folder in thecontainer1bucket.bucket: Represents the file bucket. For example,bucket=container1retrieves the files or file list in thecontainer1bucket. This field can be left blank for non-standard S3 protocol drives.id: Since the_download_filemethod does not use theprefixvariable, the full file path must be included in theid. For example,id=container1/folder1/file1.txtindicates retrieving thefile1.txtfile from thefolder1folder in thecontainer1bucket.
Data source plugins support two debugging methods: remote debugging or installing as a local plugin for debugging. Note the following:
- If the plugin uses OAuth authentication, the
redirect_urifor remote debugging differs from that of a local plugin. Update the relevant configuration in your service provider's OAuth App accordingly. - While data source plugins support single-step debugging, we still recommend testing them in a complete knowledge pipeline to ensure full functionality.
Before packaging and publishing, make sure you've completed all of the following:
- Set the minimum supported Dify version to
1.9.0. - Set the SDK version to
dify-plugin>=0.5.0,<0.6.0. - Write the
README.mdandPRIVACY.mdfiles. - Include only English content in the code files.
- Replace the default icon with the data source provider's logo.
In the plugin directory, run the following command to generate a .difypkg plugin package:
dify plugin package . -o your_datasource.difypkg
Next, you can:
- Import and use the plugin in your Dify environment.
- Publish the plugin to Dify Marketplace by submitting a pull request.
{/* Contributing Section DO NOT edit this section! It will be automatically generated by the script. */}

