Module: Langfuse::TracedExecution Private

Defined in:
lib/langfuse/traced_execution.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Shared traced execution logic for running a callable within a Langfuse observe block, capturing output, trace_id, and any task error.

Used by DatasetItemClient#run and ExperimentRunner to avoid duplicating the observe/trace_id/begin-rescue/update pattern.

Class Method Summary collapse

Class Method Details

.call(trace_name:, input:, task:, metadata: {}) {|span, trace_id| ... } ⇒ Array<(Object, String, String, StandardError | nil)>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute a task proc within a traced observe block.

Parameters:

  • trace_name (String)

    name for the observe span

  • input (Object)

    input set on the trace

  • metadata (Hash) (defaults to: {})

    metadata set on the trace

  • task (Proc)

    the callable to execute — receives the span

Yields:

  • (span, trace_id)

    optional pre-task hook (e.g., dataset run linking)

Returns:

  • (Array<(Object, String, String, StandardError | nil)>)

    output, trace_id, observation_id, error



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/langfuse/traced_execution.rb', line 20

def self.call(trace_name:, input:, task:, metadata: {})
  output = nil
  trace_id = nil
  observation_id = nil
  task_error = nil

  Langfuse.observe(trace_name) do |span|
    trace_id = span.trace_id
    observation_id = span.id
    span.update_trace(input: input, metadata: )
    yield(span, trace_id) if block_given?
    begin
      output = task.call(span)
      span.update(output: output)
    rescue StandardError => e
      span.update(output: "Error: #{e.message}", level: "ERROR")
      task_error = e
    end
  end

  [output, trace_id, observation_id, task_error]
end