Class: DSPy::Teleprompt::GEPA::ExecutionTrace

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/teleprompt/gepa.rb

Overview

Immutable execution trace record using Ruby’s Data class Captures execution events for GEPA’s reflective analysis

Constant Summary collapse

AttributesHash =

Type aliases for better type safety

T.type_alias { T::Hash[T.any(String, Symbol), T.untyped] }
MetadataHash =
T.type_alias { T::Hash[Symbol, T.untyped] }

Instance Method Summary collapse

Constructor Details

#initialize(trace_id:, event_name:, timestamp:, span_id: nil, attributes: {}, metadata: nil) ⇒ ExecutionTrace

Returns a new instance of ExecutionTrace.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dspy/teleprompt/gepa.rb', line 63

def initialize(trace_id:, event_name:, timestamp:, span_id: nil, attributes: {}, metadata: nil)
  # Freeze nested structures for true immutability
  frozen_attributes = attributes.freeze
   = &.freeze

  super(
    trace_id: trace_id,
    event_name: event_name,
    timestamp: timestamp,
    span_id: span_id,
    attributes: frozen_attributes,
    metadata: 
  )
end

Instance Method Details

#llm_trace?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/dspy/teleprompt/gepa.rb', line 80

def llm_trace?
  event_name.start_with?('llm.') || event_name.start_with?('lm.')
end

#model_nameObject



141
142
143
# File 'lib/dspy/teleprompt/gepa.rb', line 141

def model_name
  attributes['gen_ai.request.model'] || attributes[:model]
end

#module_trace?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
# File 'lib/dspy/teleprompt/gepa.rb', line 86

def module_trace?
  !llm_trace? && (
    event_name.include?('chain_of_thought') ||
    event_name.include?('react') ||
    event_name.include?('codeact') ||
    event_name.include?('predict')
  )
end

#prompt_textObject



129
130
131
# File 'lib/dspy/teleprompt/gepa.rb', line 129

def prompt_text
  attributes[:prompt] || attributes['prompt']
end

#response_textObject



135
136
137
# File 'lib/dspy/teleprompt/gepa.rb', line 135

def response_text
  attributes[:response] || attributes['response']
end

#signature_nameObject



147
148
149
# File 'lib/dspy/teleprompt/gepa.rb', line 147

def signature_name
  attributes['dspy.signature'] || attributes[:signature]
end

#to_hObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/dspy/teleprompt/gepa.rb', line 116

def to_h
  {
    trace_id: trace_id,
    event_name: event_name,
    timestamp: timestamp,
    span_id: span_id,
    attributes: attributes,
    metadata: 
  }
end

#token_usageObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dspy/teleprompt/gepa.rb', line 97

def token_usage
  return 0 unless llm_trace?

  # Try different token attribute keys
  [
    'gen_ai.usage.total_tokens',
    'gen_ai.usage.prompt_tokens',
    'tokens',
    :tokens
  ].each do |key|
    value = attributes[key]
    return value.to_i if value
  end

  0
end