Class: Langsmith::RunTree

Inherits:
Object
  • Object
show all
Defined in:
lib/langsmith/run_tree.rb

Overview

RunTree manages the creation and lifecycle of trace runs. It handles parent-child relationships and coordinates with the batch processor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, run_type: "chain", inputs: nil, metadata: nil, tags: nil, extra: nil, parent_run_id: nil, tenant_id: nil, project: nil) ⇒ RunTree

Returns a new instance of RunTree.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/langsmith/run_tree.rb', line 12

def initialize(
  name:,
  run_type: "chain",
  inputs: nil,
  metadata: nil,
  tags: nil,
  extra: nil,
  parent_run_id: nil,
  tenant_id: nil,
  project: nil
)
  effective_parent_id = parent_run_id || Context.current_parent_run_id
  effective_tenant_id = tenant_id || Context.current_run&.tenant_id

  # Child traces must use the same project as their parent to keep the trace tree together.
  effective_project = Context.current_run&.session_name || project
  effective_trace_id = Context.current_run&.trace_id
  parent_dotted_order = Context.current_run&.dotted_order

  # Inject evaluation context: session_id on ALL runs, reference_example_id only on root runs.
  eval_ctx = Context.evaluation_context
  effective_session_id = eval_ctx&.dig(:experiment_id)
  effective_ref_example_id = eval_ctx&.dig(:example_id) unless effective_parent_id

  @run = Run.new(
    name: name,
    run_type: run_type,
    inputs: inputs,
    parent_run_id: effective_parent_id,
    metadata: ,
    tags: tags,
    extra: extra,
    tenant_id: effective_tenant_id,
    session_name: effective_project,
    trace_id: effective_trace_id,
    parent_dotted_order: parent_dotted_order,
    reference_example_id: effective_ref_example_id,
    session_id: effective_session_id
  )

  register_evaluation_root_run(effective_parent_id)
  @posted_start = @posted_end = false
end

Instance Attribute Details

#runObject (readonly)

Returns the value of attribute run.



10
11
12
# File 'lib/langsmith/run_tree.rb', line 10

def run
  @run
end

Instance Method Details

#add_metadataObject

Convenience methods that delegate to the run



91
92
93
# File 'lib/langsmith/run_tree.rb', line 91

def (...)
  @run.(...)
end

#add_tagsObject



95
96
97
# File 'lib/langsmith/run_tree.rb', line 95

def add_tags(...)
  @run.add_tags(...)
end

#create_child(name:, run_type: "chain", **kwargs) ⇒ Object

Create a child run tree



128
129
130
131
132
133
134
135
# File 'lib/langsmith/run_tree.rb', line 128

def create_child(name:, run_type: "chain", **kwargs)
  RunTree.new(
    name: name,
    run_type: run_type,
    parent_run_id: @run.id,
    **kwargs
  )
end

#executeObject

Execute a block within this run's context



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/langsmith/run_tree.rb', line 73

def execute
  return yield(@run) unless Langsmith.tracing_enabled?

  post_start

  Context.with_run(@run) do
    result = yield(@run)
    @run.finish(outputs: sanitize_outputs(result))
    result
  end
rescue StandardError => e
  @run.finish(error: e)
  raise
ensure
  post_end if Langsmith.tracing_enabled?
end

#idObject



119
120
121
# File 'lib/langsmith/run_tree.rb', line 119

def id
  @run.id
end

#parent_run_idObject



123
124
125
# File 'lib/langsmith/run_tree.rb', line 123

def parent_run_id
  @run.parent_run_id
end

#post_endObject

Post the run end to LangSmith



65
66
67
68
69
70
# File 'lib/langsmith/run_tree.rb', line 65

def post_end
  return if @posted_end || !Langsmith.tracing_enabled?

  Langsmith.batch_processor.enqueue_update(@run)
  @posted_end = true
end

#post_startObject

Post the run start to LangSmith



57
58
59
60
61
62
# File 'lib/langsmith/run_tree.rb', line 57

def post_start
  return if @posted_start || !Langsmith.tracing_enabled?

  Langsmith.batch_processor.enqueue_create(@run)
  @posted_start = true
end

#set_inputs(inputs) ⇒ Object



99
100
101
# File 'lib/langsmith/run_tree.rb', line 99

def set_inputs(inputs)
  @run.inputs = inputs
end

#set_modelObject



111
112
113
# File 'lib/langsmith/run_tree.rb', line 111

def set_model(...)
  @run.set_model(...)
end

#set_outputs(outputs) ⇒ Object



103
104
105
# File 'lib/langsmith/run_tree.rb', line 103

def set_outputs(outputs)
  @run.outputs = outputs
end

#set_streaming_metricsObject



115
116
117
# File 'lib/langsmith/run_tree.rb', line 115

def set_streaming_metrics(...)
  @run.set_streaming_metrics(...)
end

#set_token_usageObject



107
108
109
# File 'lib/langsmith/run_tree.rb', line 107

def set_token_usage(...)
  @run.set_token_usage(...)
end