Class: SorbetBaml::DescriptionExtractor

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/sorbet_baml/description_extractor.rb

Overview

Extracts description parameters from T::Struct prop and const declarations

Class Method Summary collapse

Class Method Details

.extract_prop_descriptions(klass) ⇒ Object



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
# File 'lib/sorbet_baml/description_extractor.rb', line 12

def self.extract_prop_descriptions(klass)
  descriptions = {}

  # Check if this is a T::Struct with props
  return descriptions unless klass.respond_to?(:props)

  begin
    # DSPy >= 0.34 stores descriptions via DSPy::Ext::StructDescriptions#field_descriptions
    if klass.respond_to?(:field_descriptions)
      T.unsafe(klass).field_descriptions.each do |field_name, desc|
        descriptions[field_name.to_s] = desc if desc.is_a?(String)
      end
    end

    # Fall back to the legacy :extra hash for structs that store descriptions there
    if descriptions.empty?
      T.unsafe(klass).props.each do |field_name, prop_info|
        next unless prop_info.is_a?(Hash)

        extra = prop_info[:extra]
        descriptions[field_name.to_s] = extra[:description] if extra.is_a?(Hash) && extra[:description].is_a?(String)
      end
    end
  rescue StandardError
    # Handle any errors gracefully and return empty hash
    return {}
  end

  descriptions
end