Class: LLM::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/response.rb

Overview

LLM::Response is the normalized base shape for provider and endpoint responses in llm.rb.

Provider calls return an instance of this class, then extend it with provider-, endpoint-, or context-specific modules so response handling can share one common surface without flattening away specialized behavior.

The normalized response still keeps the original Net::HTTPResponse available through #res when callers need direct access to raw HTTP details such as headers, status codes, or unadapted bodies.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(res) ⇒ LLM::Response

Returns an instance of LLM::Response

Parameters:

  • res (Net::HTTPResponse)

    HTTP response



30
31
32
# File 'lib/llm/response.rb', line 30

def initialize(res)
  @res = res
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, **kwargs, &b) ⇒ Object (private)



66
67
68
69
70
71
72
# File 'lib/llm/response.rb', line 66

def method_missing(m, *args, **kwargs, &b)
  if LLM::Object === body
    body.respond_to?(m) ? body[m.to_s] : super
  else
    super
  end
end

Instance Attribute Details

#resNet::HTTPResponse (readonly)

Returns the HTTP response

Returns:

  • (Net::HTTPResponse)


23
24
25
# File 'lib/llm/response.rb', line 23

def res
  @res
end

Instance Method Details

#bodyLLM::Object, String

Returns the response body

Returns:

  • (LLM::Object, String)

    Returns an LLM::Object when the response body is JSON, otherwise returns a raw string.



39
40
41
# File 'lib/llm/response.rb', line 39

def body
  @res.body
end

#file?Boolean

Returns true if the response is from the Files API

Returns:

  • (Boolean)


60
61
62
# File 'lib/llm/response.rb', line 60

def file?
  false
end

#inspectString

Returns an inspection of the response object

Returns:

  • (String)


46
47
48
# File 'lib/llm/response.rb', line 46

def inspect
  "#<#{self.class.name}:0x#{object_id.to_s(16)} @body=#{body.inspect} @res=#{@res.inspect}>"
end

#ok?Boolean

Returns true if the response is successful

Returns:

  • (Boolean)


53
54
55
# File 'lib/llm/response.rb', line 53

def ok?
  Net::HTTPSuccess === @res
end