--- slug: /upsert-data-of-api --- # upsert - Update or insert data The `upsert()` method is used to insert new records or update existing records. If a record with the given ID already exists, it will be updated; otherwise, a new record will be inserted. :::info This API is only available when using a Client connection. For more information about the Client, see [Client](../50.client.md). ::: ## Prerequisites * You have installed pyseekdb. For more information about how to install pyseekdb, see [Get Started](../../10.pyseekdb-sdk/10.pyseekdb-sdk-get-started.md). * You have connected to the database. For more information about how to connect, see [Client](../50.client.md). * If you are using seekdb or OceanBase Database in client mode, ensure that the connected user has the `INSERT` and `UPDATE` privileges on the target table. For more information about how to view the current user privileges, see [View user privileges](https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000003980135). If the user does not have the required privileges, contact the administrator to grant them. For more information about how to directly grant privileges, see [Directly grant privileges](https://www.oceanbase.com/docs/common-oceanbase-database-cn-1000000003980140). ## Request parameters ```python Upsert( ids=ids, embeddings=embeddings, documents=documents, metadatas=metadatas ) ``` |Parameter|Type|Required|Description|Example value| |---|---|---|---|---| |`ids`|string or List[str]|Yes|The ID to be added or modified. It can be a single ID or an array of IDs.|item1| |`embeddings`|List[float] or List[List[float]]|No|The vectors. If provided, they will be used directly (ignoring `embedding_function`). If not provided, you can provide `documents` to automatically generate vectors.|[0.1, 0.2, 0.3]| |`documents`|string or List[str]|No|The documents. If `vectors` are not provided, `documents` will be converted to vectors using the collection's `embedding_function`.|"Document text"| |`metadatas`|dict or List[dict]|No|The metadata. |`{"category": "AI"}`| ## Request example ```python import pyseekdb # Create a client client = pyseekdb.Client() collection = client.get_collection("my_collection") collection1 = client.get_collection("my_collection1") # Upsert single item (insert or update) collection.upsert( ids="item1", embeddings=[0.1, 0.2, 0.3], documents="Document text", metadatas={"category": "AI", "score": 95} ) # Upsert multiple items collection.upsert( ids=["item1", "item2", "item3"], embeddings=[ [0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9] ], documents=["Doc 1", "Doc 2", "Doc 3"], metadatas=[ {"category": "AI"}, {"category": "ML"}, {"category": "DL"} ] ) # Upsert with documents only - embeddings auto-generated by embedding_function # Requires: collection must have embedding_function set collection1.upsert( ids=["item1", "item2"], documents=["Document 1", "Document 2"], metadatas=[{"category": "AI"}, {"category": "ML"}] ) ``` ## Response parameters None ## References * [Insert data](200.add-data-of-api.md) * [Update data](300.update-data-of-api.md) * [Delete data](400.upsert-data-of-api.md)