Module: DSPy::LM::ResponseMetadataFactory

Extended by:
T::Sig
Defined in:
lib/dspy/lm/response.rb

Overview

Factory for creating response metadata objects

Class Method Summary collapse

Class Method Details

.create(provider, metadata) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/dspy/lm/response.rb', line 145

def self.create(provider, )
  # Handle nil metadata
   ||= {}
  
  # Normalize provider name
  provider_name = provider.to_s.downcase
  
  # Extract common fields
  common_fields = {
    provider: provider,
    model: [:model],
    response_id: [:response_id] || [:id],
    created: [:created],
    structured_output: [:structured_output]
  }
  
  case provider_name
  when 'openai'
    .new(
      **common_fields,
      system_fingerprint: [:system_fingerprint],
      finish_reason: [:finish_reason]&.to_s
    )
  when 'anthropic'
    .new(
      **common_fields,
      stop_reason: [:stop_reason]&.to_s,
      stop_sequence: [:stop_sequence]&.to_s,
      tool_calls: [:tool_calls]
    )
  when 'gemini'
    .new(
      **common_fields,
      finish_reason: [:finish_reason]&.to_s,
      safety_ratings: [:safety_ratings],
      streaming: [:streaming]
    )
  else
    .new(**common_fields)
  end
rescue => e
  DSPy.logger.debug("Failed to create response metadata: #{e.message}")
  # Fallback to basic metadata
  .new(
    provider: provider,
    model: [:model]
  )
end