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 keeps the transport response available through #res. When the default net/http transport is in use, LLM::Transport::Response::HTTP keeps the original Net::HTTPResponse available through its own #res.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(res) ⇒ LLM::Response

Returns an instance of LLM::Response

Parameters:



32
33
34
# File 'lib/llm/response.rb', line 32

def initialize(res)
  @res = LLM::Transport::Response.from(res)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

#resLLM::Transport::Response (readonly)

Returns the HTTP response



25
26
27
# File 'lib/llm/response.rb', line 25

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.



41
42
43
# File 'lib/llm/response.rb', line 41

def body
  @res.body
end

#file?Boolean

Returns true if the response is from the Files API

Returns:

  • (Boolean)


62
63
64
# File 'lib/llm/response.rb', line 62

def file?
  false
end

#inspectString

Returns an inspection of the response object

Returns:

  • (String)


48
49
50
# File 'lib/llm/response.rb', line 48

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)


55
56
57
# File 'lib/llm/response.rb', line 55

def ok?
  @res.success?
end