Datasets

The Opik TypeScript SDK provides robust functionality for creating and managing datasets. Datasets in Opik serve as collections of data items that can be used for various purposes, including evaluation.

Dataset Fundamentals

A dataset in Opik is a named collection of data items. Each dataset:

  • Has a unique identifier and name
  • Contains items that share a common structure
  • Supports powerful deduplication capabilities
  • Using for evaluation

TypeScript Type Safety

One of the key features of the Opik SDK is strong TypeScript typing support for datasets. You can define custom types for your dataset items to ensure type safety throughout your application:

1// Define a custom dataset item type
2type QuestionAnswerItem = {
3 question: string;
4 answer: string;
5 metadata: {
6 category: string;
7 difficulty: string;
8 };
9};
10
11// Create a typed dataset
12const dataset = await opik.createDataset<QuestionAnswerItem>(
13 "qa-dataset", // Dataset name
14 "Question-Answer pairs for evaluation" // Dataset description
15);

Working with Datasets

Creating Datasets

1// Create a new dataset
2await opik.createDataset<YourItemType>(
3 "dataset-name",
4 "Optional dataset description"
5);
6
7// Get an existing dataset or create it if it doesn't exist
8const dataset = await opik.getOrCreateDataset<YourItemType>(
9 "dataset-name",
10 "Optional dataset description"
11);

Managing Dataset Items

1// Insert items
2await dataset.insert([
3 { id: "item1", question: "What is ML?", answer: "Machine learning is..." },
4 {
5 id: "item2",
6 question: "What is AI?",
7 answer: "Artificial intelligence is...",
8 },
9]);
10
11// Update existing items
12await dataset.update([
13 {
14 id: "item1",
15 question: "What is Machine Learning?",
16 answer: "Updated answer...",
17 },
18]);
19
20// Delete specific items
21await dataset.delete(["item1", "item2"]);
22
23// Clear all items from the dataset
24await dataset.clear();

The Opik SDK automatically handles deduplication when inserting items into a dataset. This feature ensures that identical items are not added multiple times.

Retrieving Dataset Items

1// Get a specific number of items
2const items = await dataset.getItems(10);
3
4// Get items with pagination
5const firstBatch = await dataset.getItems(10);
6const lastItemId = firstBatch[firstBatch.length - 1].id;
7const nextBatch = await dataset.getItems(10, lastItemId);

Working with JSON

1// Import items from a JSON string
2const jsonData = JSON.stringify([
3 {
4 query: "What is the capital of France?",
5 response: "Paris",
6 tags: ["geography", "europe"],
7 },
8]);
9
10// Map JSON keys to dataset item fields
11const keysMapping = {
12 query: "question", // 'query' in JSON becomes 'question' in dataset item
13 response: "answer", // 'response' in JSON becomes 'answer' in dataset item
14 tags: "metadata.tags", // 'tags' in JSON becomes 'metadata.tags' in dataset item
15};
16
17// Specify keys to ignore
18const ignoreKeys = ["irrelevant_field"];
19
20// Insert from JSON with mapping
21await dataset.insertFromJson(jsonData, keysMapping, ignoreKeys);
22
23// Export dataset to JSON with custom key mapping
24const exportMapping = { question: "prompt", answer: "completion" };
25const exportedJson = await dataset.toJson(exportMapping);

Working with Dataset Versions

Dataset versions are immutable snapshots. Use DatasetVersion for reproducible evaluations—ensuring the same data is used regardless of later changes.

Get a Specific Version

1const dataset = await opik.getDataset("my-dataset");
2
3// Get a read-only view of version v2
4const v2 = await dataset.getVersionView("v2");
5
6// Access version metadata
7console.log(v2.versionName); // "v2"
8console.log(v2.itemsTotal); // 150
9console.log(v2.createdAt); // Date object
10
11// Get items from this version
12const items = await v2.getItems();
13
14// Export to JSON with key mapping
15const json = await v2.toJson({ input: "question", output: "answer" });

Check Current Version

1// Get the latest version name
2const currentVersion = await dataset.getCurrentVersionName();
3// Returns: "v3" (or undefined if no versions)
4
5// Get detailed version info
6const info = await dataset.getVersionInfo();
7if (info) {
8 console.log(`${info.versionName}: ${info.itemsTotal} items`);
9 console.log(`Tags: ${info.tags?.join(", ")}`);
10}

Use in Evaluations

Pass a DatasetVersion to evaluate() for reproducible experiments:

1import { evaluate, Opik, ExactMatch } from "opik";
2
3const opik = new Opik();
4const dataset = await opik.getDataset("qa-dataset");
5
6// Pin to a specific version
7const v2 = await dataset.getVersionView("v2");
8
9const result = await evaluate({
10 dataset: v2, // Uses v2 items, not latest
11 task: myTask,
12 scoringMetrics: [new ExactMatch()],
13 experimentName: "Evaluation on v2",
14});

When comparing experiments (A/B tests), use the same dataset version to isolate the effect of your changes from data variations.

API Reference

The generic type parameter T represents the DatasetItem type that defines the structure of items stored in this dataset.

OpikClient Dataset Methods

createDataset<T>

Creates a new dataset.

Arguments:

  • name: string - The name of the dataset
  • description?: string - Optional description of the dataset

Returns: Promise<Dataset<T>> - A promise that resolves to the created Dataset object

getDataset<T>

Retrieves an existing dataset by name.

Arguments:

  • name: string - The name of the dataset to retrieve

Returns: Promise<Dataset<T>> - A promise that resolves to the Dataset object

getOrCreateDataset<T>

Retrieves an existing dataset by name or creates it if it doesn’t exist.

Arguments:

  • name: string - The name of the dataset
  • description?: string - Optional description (used only if creating a new dataset)

Returns: Promise<Dataset<T>> - A promise that resolves to the existing or newly created Dataset object

getDatasets<T>

Retrieves a list of datasets.

Arguments:

  • maxResults?: number - Optional maximum number of datasets to retrieve (default: 100)

Returns: Promise<Dataset<T>[]> - A promise that resolves to an array of Dataset objects

deleteDataset

Deletes a dataset by name.

Arguments:

  • name: string - The name of the dataset to delete

Returns: Promise<void>

Dataset Class Methods

insert

Inserts new items into the dataset with automatic deduplication.

Arguments:

  • items: T[] - List of objects to add to the dataset

Returns: Promise<void>

update

Updates existing items in the dataset.

Arguments:

  • items: T[] - List of objects to update in the dataset (must include IDs)

Returns: Promise<void>

delete

Deletes items from the dataset.

Arguments:

  • itemIds: string[] - List of item IDs to delete

Returns: Promise<void>

clear

Deletes all items from the dataset.

Returns: Promise<void>

getItems

Retrieves items from the dataset.

Arguments:

  • nbSamples?: number - Optional number of items to retrieve (if not set, all items are returned)
  • lastRetrievedId?: string - Optional ID of the last retrieved item for pagination

Returns: Promise<T[]> - A promise that resolves to an array of dataset items

insertFromJson

Inserts items from a JSON string into the dataset.

Arguments:

  • jsonArray: string - JSON string in array format
  • keysMapping?: Record<string, string> - Optional dictionary that maps JSON keys to dataset item field names
  • ignoreKeys?: string[] - Optional array of keys to ignore when constructing dataset items

Returns: Promise<void>

toJson

Exports the dataset to a JSON string.

Arguments:

  • keysMapping?: Record<string, string> - Optional dictionary that maps dataset item field names to output JSON keys

Returns: Promise<string> - A JSON string representation of all items in the dataset

Dataset Version Methods

getVersionView

Get a read-only view of a specific dataset version.

Arguments:

  • versionName: string - The version name (e.g., “v1”, “v2”)

Returns: Promise<DatasetVersion<T>>

Throws: DatasetVersionNotFoundError if version doesn’t exist

getCurrentVersionName

Get the name of the latest version.

Returns: Promise<string | undefined> - Version name or undefined if no versions

getVersionInfo

Get metadata about the latest version.

Returns: Promise<DatasetVersionPublic | undefined> - Version info or undefined

DatasetVersion Class

A read-only view of dataset items at a specific version. Cannot modify data.

Properties

PropertyTypeDescription
namestringDataset name
idstringDataset ID
versionIdstring | undefinedVersion’s unique ID
versionNamestring | undefinedVersion name (e.g., “v1”)
versionHashstring | undefinedContent hash
tagsstring[] | undefinedVersion tags
isLatestboolean | undefinedWhether this is the latest version
itemsTotalnumber | undefinedTotal items in version
itemsAddednumber | undefinedItems added since previous
itemsModifiednumber | undefinedItems modified since previous
itemsDeletednumber | undefinedItems deleted since previous
changeDescriptionstring | undefinedVersion notes
createdAtDate | undefinedCreation timestamp
createdBystring | undefinedCreator

getItems

Retrieve items from this version.

Arguments:

  • nbSamples?: number - Number of items to retrieve (default: all)

Returns: Promise<T[]> - Array of dataset items

toJson

Export version items to JSON string.

Arguments:

  • keysMapping?: Record<string, string> - Map field names to output keys

Returns: Promise<string> - JSON string

getVersionInfo

Get the full version metadata object.

Returns: DatasetVersionPublic - Version info