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.

Examples:

Accessing response data

res = agent.talk "Weather in Paris?", schema: Weather
res.content        # => raw text or structured data
res.content!       # => parsed JSON as LLM::Object
res.usage          # => token usage (input, output, cache, etc.)
res.messages       # => message history from this turn

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(res) ⇒ LLM::Response

Returns an instance of LLM::Response

Parameters:



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

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)



88
89
90
91
92
93
94
# File 'lib/llm/response.rb', line 88

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



35
36
37
# File 'lib/llm/response.rb', line 35

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.



51
52
53
# File 'lib/llm/response.rb', line 51

def body
  @res.body
end

#file?Boolean

Returns true if the response is from the Files API

Returns:

  • (Boolean)


82
83
84
# File 'lib/llm/response.rb', line 82

def file?
  false
end

#idString?

Returns the provider response id when present.

Returns:

  • (String, nil)


72
73
74
75
76
77
# File 'lib/llm/response.rb', line 72

def id
  return nil unless LLM::Object === body
  body.id          ||
  body.responseId  || body.response_id ||
  body.requestId   || body.request_id
end

#inspectString

Returns an inspection of the response object

Returns:

  • (String)


58
59
60
# File 'lib/llm/response.rb', line 58

def inspect
  "#<#{LLM::Utils.object_id(self)} @body=#{body.inspect} @res=#{@res.inspect}>"
end

#ok?Boolean

Returns true if the response is successful

Returns:

  • (Boolean)


65
66
67
# File 'lib/llm/response.rb', line 65

def ok?
  @res.success?
end