All Products
Search
Document Center

DashVector:Insert docs

Last Updated:Mar 11, 2026

Insert one or more documents into a DashVector collection using the Python SDK.

If a document ID already exists in the collection, the insert operation skips that document without overwriting it. To update an existing document, use the upsert method instead. If you omit the document ID, DashVector automatically generates one and returns it in the response.

Prerequisites

Before you begin, make sure that you have:

API definition

Collection.insert(
    docs: Union[Doc, List[Doc], Tuple, List[Tuple]],
    partition: Optional[str] = None,
    async_req: False
) -> DashVectorResponse

Examples

All examples use an existing collection named quickstart. Replace YOUR_API_KEY with your API key and YOUR_CLUSTER_ENDPOINT with the endpoint of your cluster before running the code.

import dashvector
from dashvector import Doc
import numpy as np

client = dashvector.Client(
    api_key='YOUR_API_KEY',
    endpoint='YOUR_CLUSTER_ENDPOINT'
)
collection = client.get(name='quickstart')
Note

Create the quickstart collection before running these examples. For details, see the example section in Create a collection.

Insert a single document

# Insert a document using a Doc object
ret = collection.insert(
    Doc(
        id='1',
        vector=[0.1, 0.2, 0.3, 0.4]
    )
)
assert ret

# Shorthand: use a tuple instead of a Doc object
ret = collection.insert(
    ('2', [0.1, 0.1, 0.1, 0.1])               # (id, vector)
)

Insert a document with fields

Attach metadata as key-value pairs through the fields parameter. Fields defined during collection creation must match their predefined types. Additional fields are schema-free and accept str, int, bool, or float values. For more information, see Schema-free.

# Insert a document with predefined and schema-free fields
ret = collection.insert(
    Doc(
        id='3',
        vector=np.random.rand(4),
        fields={
            # Predefined fields (types must match collection schema)
            # name:str, weight:float, age:int, id:dashvector.long
            'name': 'zhangsan', 'weight':70.0, 'age':30, 'id':1234567890,
            # Schema-free fields (no predefinition required)
            'anykey1': 'str-value', 'anykey2': 1,
            'anykey3': True, 'anykey4': 3.1415926
        }
    )
)

# Shorthand: use a tuple with fields
ret = collection.insert(
    ('4', np.random.rand(4), {'foo': 'bar'})  # (id, vector, fields)
)

Insert multiple documents

# Batch insert 10 documents using Doc objects
ret = collection.insert(
    [
        Doc(id=str(i+5), vector=np.random.rand(4)) for i in range(10)
    ]
)

# Batch insert using tuples with fields
ret = collection.insert(
    [
        ('15', [0.2,0.7,0.8,1.3], {'age': 20}),
        ('16', [0.3,0.6,0.9,1.2], {'age': 30}),
        ('17', [0.4,0.5,1.0,1.1], {'age': 40})
    ]                                         # List[(id, vector, fields)]
)
assert ret

Insert documents asynchronously

Set async_req=True to perform non-blocking inserts. Call .get() on the returned future to retrieve the result.

# Asynchronously insert 10 documents
ret_funture = collection.insert(
    [
        Doc(id=str(i+18), vector=np.random.rand(4), fields={'name': 'foo' + str(i)}) for i in range(10)
    ],
    async_req=True
)
# Block until the asynchronous insert completes
ret = ret_funture.get()

Insert a document with a sparse vector

Use the sparse_vector parameter to store sparse representations alongside dense vectors.

ret = collection.insert(
    Doc(
        id='28',
        vector=[0.1, 0.2, 0.3, 0.4],
        sparse_vector={1:0.4, 10000:0.6, 222222:0.8}
    )
)

Parameters

Request parameters

ParameterTypeDefaultDescription
docsUnion[Doc, List[Doc], Tuple, List[Tuple]]RequiredOne or more documents to insert
partitionOptional[str]NoneTarget partition name
async_reqboolFalseEnable asynchronous mode

Parameter notes:

  • Tuple format: Elements must follow the order (id, vector) or (id, vector, fields). A tuple is equivalent to a Doc object.

  • Field keys and values: Each field key must be a str. Values accept str, int, bool, or float.

    • If a field key is predefined during collection creation, the value must match the predefined type.

    • If a field key is not predefined, it is treated as a schema-free field and accepts any supported value type.

  • For more information about predefined fields, see Schema-free.

Response

The method returns a DashVectorResponse object with the following properties:

PropertyTypeDescriptionExample
codeintStatus code. 0 indicates success. See Status codes.0
messagestrResult messagesuccess
request_idstrUnique request identifier19215409-ea66-4db9-8764-26ce2eb5bb99