Class: Braintrust::Dataset

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/braintrust/dataset.rb

Overview

High-level interface for working with Braintrust datasets. Provides both eager loading and lazy enumeration for efficient access to dataset records.

Examples:

Basic usage (uses global state)

Braintrust.init(api_key: "...")
dataset = Braintrust::Dataset.new(name: "my-dataset", project: "my-project")
dataset.each { |record| puts record[:input] }

With explicit state

state = Braintrust.init(api_key: "...")
dataset = Braintrust::Dataset.new(name: "my-dataset", project: "my-project", state: state)

Eager loading for small datasets

records = dataset.fetch_all(limit: 100)

Using Enumerable methods

dataset.take(10)
dataset.select { |r| r[:tags]&.include?("important") }

With version pinning

dataset = Braintrust::Dataset.new(name: "my-dataset", project: "my-project", version: "1.0")

Defined Under Namespace

Classes: ID

Constant Summary collapse

DEFAULT_PAGE_SIZE =

Default number of records to fetch per API page

1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, id: nil, project: nil, version: nil, state: nil) ⇒ Dataset

Initialize a dataset reference

Parameters:

  • name (String, nil) (defaults to: nil)

    Dataset name (required if id not provided)

  • id (String, nil) (defaults to: nil)

    Dataset UUID (required if name not provided)

  • project (String, nil) (defaults to: nil)

    Project name (required if using name)

  • version (String, nil) (defaults to: nil)

    Optional version to pin to

  • state (State, nil) (defaults to: nil)

    Braintrust state (defaults to global state)



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/braintrust/dataset.rb', line 42

def initialize(name: nil, id: nil, project: nil, version: nil, state: nil)
  @name = name
  @provided_id = id
  @project = project
  @version = version
  @api = API.new(state: state)
  @resolved_id = nil
  @metadata = nil

  validate_params!
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/braintrust/dataset.rb', line 34

def name
  @name
end

#projectObject (readonly)

Returns the value of attribute project.



34
35
36
# File 'lib/braintrust/dataset.rb', line 34

def project
  @project
end

#versionObject (readonly)

Returns the value of attribute version.



34
35
36
# File 'lib/braintrust/dataset.rb', line 34

def version
  @version
end

Instance Method Details

#each {|Hash| ... } ⇒ Object

Iterate over records lazily (implements Enumerable) Fetches pages on demand for memory efficiency with large datasets.

Yields:

  • (Hash)

    Each record with :input, :expected, :tags, :metadata, :origin



84
85
86
87
# File 'lib/braintrust/dataset.rb', line 84

def each(&block)
  return enum_for(:each) unless block_given?
  each_record(&block)
end

#fetch_all(limit: nil) ⇒ Array<Hash>

Fetch all records eagerly into an array

Parameters:

  • limit (Integer, nil) (defaults to: nil)

    Maximum records to return (nil for all)

Returns:

  • (Array<Hash>)

    Array of records with :input, :expected, :tags, :metadata, :origin



75
76
77
78
79
# File 'lib/braintrust/dataset.rb', line 75

def fetch_all(limit: nil)
  records = []
  each_record(limit: limit) { |record| records << record }
  records
end

#idString

Get the dataset ID, resolving from name if necessary

Returns:

  • (String)

    Dataset UUID



56
57
58
59
60
# File 'lib/braintrust/dataset.rb', line 56

def id
  return @provided_id if @provided_id
  resolve_name! unless @resolved_id
  @resolved_id
end

#metadataHash

Get the dataset metadata from the API Makes an API call if metadata hasn’t been fetched yet. Note: When initialized with name, metadata is fetched during name resolution. When initialized with ID, this triggers a separate get_by_id call.

Returns:

  • (Hash)

    Dataset metadata including name, description, created, etc.



67
68
69
70
# File 'lib/braintrust/dataset.rb', line 67

def 
  fetch_metadata! unless @metadata
  @metadata
end