Create Knowledge Base and Import

Updated at:

Create a Knowledge Base and import files in a single call (merges CreateIndex and SubmitIndexJob). On success, the response returns the Knowledge Base ID (pipelineId) and the import job ID (ingestionId).

Prerequisites

Get an API key and a workspace ID, and configure authentication first. See API overview and Authentication.

Endpoint

POST /api/v1/indices/rag/index/create_v2

Create a Knowledge Base and import files in a single call (merges CreateIndex and SubmitIndexJob).

Request body

WarningThe file ID parameter is docIds — not file_ids or fileIds. However, the validation error message uses the internal name file_ids.

FieldRequiredTypeDescription
nameYesstringKnowledge Base name, 1-20 characters.
descriptionYesstringKnowledge Base description, 1-200 characters.
structureTypeYesstringStructure type: unstructured or structured.
knowledgeTypeNostringKnowledge Base type, corresponding to the console Knowledge Base type: document (Document Search), table (Data Query), image (Image Q&A), multimedia (Audio and Video Search). The value must match structureType: document, image, and multimedia pair with unstructured, while table pairs with structured. Must be supplied together with knowledgeScene or omitted together. When both are omitted, the system applies defaults based on structureType.
knowledgeSceneNostringUsage scenario, corresponding to the console usage scenario. The value depends on knowledgeType: document supports basic_document_qa (basic document Q&A), visual_perception_qa (visual understanding, rich text documents), and lite_document_qa (express Q&A); table is fixed to basic_table_qa; image is fixed to image_qa; multimedia is fixed to basic_multimedia_qa. The console only offers scenario options for the Document Search type; other types are filled with fixed values automatically, but you must still pass them explicitly when creating through the API. Must be supplied together with knowledgeType or omitted together. Note: lite_document_qa requires sinkType to be BUILT_IN; visual_perception_qa and image_qa require a multimodal embedding model through multimodalEmbeddingModelName. document also accepts visual_document_qa (rich media answers), but the console no longer exposes this option.
sinkTypeYesstringStorage type. Default: DEFAULT. BUILT_IN uses the built-in vector storage of the platform. It must be BUILT_IN when knowledgeScene is lite_document_qa (express Q&A).
sourceTypeYesstringData source type, for example DATA_CENTER_FILE.
embeddingModelNameNostringEmbedding model name, for example text-embedding-v4.
multimodalEmbeddingModelNameNostringMultimodal embedding model name, for example qwen3-vl-embedding. Required when knowledgeScene is image_qa (Image Q&A) or visual_perception_qa (visual understanding). If missing or invalid, the request returns invalid multi embedding model.
chunkSizeNointegerDocument chunk size in characters. Recommended: 300-800.
docIdsYesarray<string>List of file IDs to import when creating the Knowledge Base. Values come from the fileId returned after registering a file with addFile, or query existing files through listFile. We recommend importing no more than 10,000. The parameter name is docIds — not file_ids or fileIds. However, validation error messages use the name file_ids.
categoryIdsNoarray<string>Files can also be imported by category when creating the Knowledge Base. Specifying category IDs imports all files under the corresponding categories. We recommend importing no more than 10,000.
dataSourcesYesarray<object>Data source configuration list. Sub-field: sourceType (string, data source type, for example DATA_CENTER_FILE).

Knowledge Base type and usage scenario

knowledgeType and knowledgeScene correspond to Knowledge Base Type and Usage Scenario in step 1 of the console creation wizard. They must be supplied together or omitted together — sending only one returns knowledgeType and knowledgeScene cannot be empty. When both are omitted, the system applies defaults based on structureType.

The console only offers a Usage Scenario choice for Document Search. The other three types have a fixed scenario that the console fills in automatically, but you must still send the pair explicitly when creating a Knowledge Base through the API.

knowledgeTypeConsole labelstructureTypeAvailable knowledgeScene
documentDocument Searchunstructuredbasic_document_qa (basic document Q&A)
visual_perception_qa (visual understanding, rich text documents)
lite_document_qa (express Q&A)
tableData Querystructuredbasic_table_qa (fixed)
imageImage Q&Aunstructuredimage_qa (fixed)
multimediaAudio and Video Searchunstructuredbasic_multimedia_qa (fixed)

Note

  • An incompatible knowledgeType and structureType pair returns knowledgeType and structureType do not match. A knowledgeScene that does not belong to the selected type returns knowledgeType and knowledgeScene do not match.
  • lite_document_qa (express Q&A) requires sinkType to be BUILT_IN. Using the default DEFAULT returns Lite Rag only supports BUILT_IN sink type.
  • visual_perception_qa and image_qa must specify a multimodal embedding model through multimodalEmbeddingModelName (for example qwen3-vl-embedding), otherwise the request returns invalid multi embedding model.
  • The document type also has a visual_document_qa (rich media answers) scenario. The API still accepts this value, but the console no longer exposes it, so avoid it for new Knowledge Bases.

Example request body for Image Q&A:

{
  "name": "image-kb",
  "description": "Image Q&A Knowledge Base",
  "structureType": "unstructured",
  "knowledgeType": "image",
  "knowledgeScene": "image_qa",
  "sinkType": "BUILT_IN",
  "sourceType": "DATA_CENTER_FILE",
  "multimodalEmbeddingModelName": "qwen3-vl-embedding",
  "docIds": ["your-file-id"],
  "dataSources": [{ "sourceType": "DATA_CENTER_FILE" }]
}

Request example

curl -X POST "$BASE_URL/api/v1/indices/rag/index/create_v2" \
  -H "Authorization: Bearer $BAILIAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "your-kb-name",
    "description": "your-kb-description",
    "structureType": "unstructured",
    "sinkType": "DEFAULT",
    "sourceType": "DATA_CENTER_FILE",
    "embeddingModelName": "text-embedding-v4",
    "chunkSize": 600,
    "docIds": ["your-file-id-1", "your-file-id-2"],
    "dataSources": [{"sourceType": "DATA_CENTER_FILE"}]
  }'

BASE_URL is https://{workspace_id}.cn-beijing.maas.aliyuncs.com ({workspace_id} is the workspace ID), and BAILIAN_API_KEY is your Alibaba Cloud Model Studio API key.

Response example

Returns 200 on success.

{
  "code": "Success",
  "status_code": 200,
  "success": true,
  "message": "success",
  "data": {
    "pipelineId": "your-kb-id",
    "ingestionId": "your-ingestion-id",
    "status": "PENDING",
    "created_at": 1783657930436,
    "updated_at": 1783657930436
  },
  "request_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "status": "SUCCESS"
}

Response fields

FieldTypeDescription
codestringResponse code. Success when successful.
status_codeintegerHTTP status code.
successbooleanWhether the request succeeded. true when successful.
messagestringResponse message. success when successful.
request_idstringUnique request identifier. Provide this ID when troubleshooting.
statusstringRequest status. SUCCESS when successful.
dataobjectResponse data. Sub-fields: pipelineId (string, Knowledge Base ID), ingestionId (string, import job ID), status (string, job status; PENDING after creation), created_at (integer, Knowledge Base creation time as a Unix timestamp in milliseconds), updated_at (integer, last update time as a Unix timestamp in milliseconds).

Error codes

HTTP status codeError code (code)Description
400Index.InvalidParameterdescription missing or too long: Required parameter(description length range[1-200]) missing or invalid, please check the request parameters.
400Index.InvalidParameterdocIds missing or an empty array: Required parameter(file_ids) missing or invalid, please check the request parameters.
400Index.FileEmptyErrorA file ID in docIds does not exist: fetch empty file list from data center.
401InvalidApiKeyInvalid or missing API key: Invalid API-key provided.