External Knowledge Integration#
Dify's external knowledge integration lets tenants connect any retrieval system (e.g. RagFlow, Bedrock, custom RAG stacks) to Dify's knowledge pipeline without ingesting documents into Dify itself. The feature is built around three concepts: a fixed API contract that the external service must implement, a credential/API template scoped to a tenant, and a binding that links a Dify dataset to an external knowledge source ID.
Architecture Overview#
Datasetβ whenprovider = "external", the dataset carries no documents; retrieval is fully delegated outward.ExternalKnowledgeApisβ tenant-scoped API template storingendpointandapi_keyas a JSON blob in thesettingscolumn.ExternalKnowledgeBindingsβ join table mapping adataset_idto anexternal_knowledge_api_idand anexternal_knowledge_id(the ID used in the remote system).
API Contract#
The external service must expose POST {endpoint}/retrieval . Dify constructs this URL by appending /retrieval to the configured endpoint and validates the endpoint is reachable before saving a new API template .
Request Body#
| Field | Required | Description |
|---|---|---|
knowledge_id | Yes | The external system's ID for this knowledge source |
query | Yes | User's search query |
retrieval_setting.top_k | Yes | Max results to return |
retrieval_setting.score_threshold | Yes | Minimum score (0.0 when disabled) |
metadata_condition | No | Filtering conditions (programmatic only) |
See full request schema including all supported metadata_condition operators.
Response Body#
HTTP 200 with {"records": [...]} . Each record:
| Field | Description |
|---|---|
content | Text chunk passed to the LLM as context |
score | Similarity score 0β1 |
title | Source document title |
metadata | Arbitrary key-value pairs; must be {} (not null) |
Any non-200 response raises an ExternalKnowledgeRetrievalError that surfaces as 502 external_knowledge_failed to callers .
Authentication#
Dify sends the configured API key as Authorization: Bearer {api_key} . During retrieval this header is assembled directly from the stored settings . The service layer also supports bearer, basic, and custom header schemes for generic HTTP tool calls via assembling_headers.
Tenant-Scoped Credential and Dataset Management#
Creating an API Template#
ExternalDatasetService.create_external_knowledge_api stores name, description, and settings (JSON with endpoint + api_key) in external_knowledge_apis, scoped by tenant_id.
check_endpoint_and_api_key fires a live POST {endpoint}/retrieval probe before the record is saved, rejecting on 400, 403, 404, or 502.
When updating, if the caller sends api_key = HIDDEN_VALUE, the service preserves the stored key to avoid leaking credentials in round-trip API responses.
Binding a Dataset#
create_external_dataset atomically:
- Creates a
Datasetrow withprovider = "external"and the retrieval model config. - Creates an
ExternalKnowledgeBindingsrow linkingdataset_id β external_knowledge_api_id + external_knowledge_id.
All lookups are scoped by tenant_id to prevent cross-tenant access .
external_knowledge_api_use_check counts binding references within the same tenant before allowing deletion.
Retrieval Flow#
fetch_external_knowledge_retrieval is the main call site:
- Resolves the binding for
(tenant_id, dataset_id). - Resolves the API template for the binding's
external_knowledge_api_id, scoped totenant_id. - Builds the request body with
knowledge_id,query,retrieval_setting, and optionalmetadata_condition. - POSTs via
ssrf_proxy(SSRF-safe HTTP client). - Returns
records[]on HTTP 200; raisesExternalKnowledgeRetrievalErroron any other status.
DatasetRetrieval._retriever and single_retrieve both call this service when dataset.provider == "external" . Retrieved records are tagged with provider="external" and merged with Dify-native results before scoring and ranking.
REST API Entry Points#
Managed by api/controllers/console/datasets/external.py:
| Endpoint | Method | Purpose |
|---|---|---|
/datasets/external-knowledge-api | GET / POST | List or create API templates |
/datasets/external-knowledge-api/<id> | GET / PATCH / DELETE | Read, update, or delete a template |
/datasets/external-knowledge-api/<id>/use-check | GET | Check binding count before deletion |
/datasets/external | POST | Create external dataset + binding |
/datasets/<id>/external-hit-testing | POST | Test retrieval against a bound dataset |
Key Source Files#
| File | Purpose |
|---|---|
api/services/external_knowledge_service.py | All CRUD + retrieval logic |
api/models/dataset.py | ExternalKnowledgeApis and ExternalKnowledgeBindings ORM models |
api/core/rag/retrieval/dataset_retrieval.py | Retrieval engine that dispatches to external service |
en/cloud/use-dify/knowledge/external-knowledge-api.mdx | Official API contract documentation |