For AI agents: the complete documentation index is available at https://viteval.dev/llms.txt, the full documentation bundle is available at https://viteval.dev/llms-full.txt, and this page is available as Markdown at https://viteval.dev/api/sample-items.md.
V1 release candidate available.Migration guide →
GitHub

sampleItems()

Create a dataset generator from a single-item factory and a fixed count. This generates new items. It does not sample an existing array.

Import

import { sampleItems } from 'viteval/dataset';

Signature

function sampleItems<DATA_ITEM extends DataItem>(
  params: SampleItemsParams<DATA_ITEM>
): Result<() => Promise<DATA_ITEM[]>>;

Parameters

Parameters
paramsSampleItemsParams<DATA_ITEM>required

Item count and generator factory.

countnumberrequired

A positive safe integer. Zero, negative, fractional, and non-finite values return an error Result without calling the factory.

item(index: number) => DATA_ITEM | Promise<DATA_ITEM>required

Factory receiving a zero-based index. Items are generated sequentially in index order whenever the returned generator is called.

Example

import { defineDataset, sampleItems } from 'viteval/dataset';

const sampled = sampleItems({
  count: 20,
  item: (index) => ({
    input: `What is ${index} + 1?`,
    expected: String(index + 1),
  }),
});
if (!sampled.ok) throw sampled.error;

export default defineDataset({
  name: 'addition',
  data: sampled.value,
});

Return value and errors

Returns a synchronous Result containing an asynchronous generator. Calling sampleItems() validates the count but does not generate cases. If the item factory throws or rejects later, the generator rejects. When used by evaluate(), that is an evaluation data-loading failure.

See defineDataset() for persistence and overwrite behavior.