Class: PromptBuilder::Items::FunctionCallOutput

Inherits:
Base
  • Object
show all
Defined in:
lib/prompt_builder/items/function_call_output.rb

Overview

Represents the output of a function call. This is added to the conversation after a tool has been invoked.

Constant Summary

Constants inherited from Base

Base::TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(call_id:, output:, id: nil, status: nil, **extra) ⇒ FunctionCallOutput

Create a new FunctionCallOutput item.

Parameters:

  • id (String, nil) (defaults to: nil)

    the function call output identifier

  • call_id (String)

    the call identifier

  • status (String, nil) (defaults to: nil)

    the function call output status

  • output (String, Array<Content::Base, Hash>, nil)

    the function output; Hash elements in an array are normalized into Content::Base objects

  • extra (Hash)

    provider-specific extra keyword arguments



32
33
34
35
36
37
38
# File 'lib/prompt_builder/items/function_call_output.rb', line 32

def initialize(call_id:, output:, id: nil, status: nil, **extra)
  @id = id&.to_s
  @call_id = call_id&.to_s
  @status = status&.to_s
  @output = normalize_output(output)
  @extra = extra.transform_keys(&:to_s)
end

Instance Attribute Details

#call_idString (readonly)

Returns the call identifier this output corresponds to.

Returns:

  • (String)

    the call identifier this output corresponds to



12
13
14
# File 'lib/prompt_builder/items/function_call_output.rb', line 12

def call_id
  @call_id
end

#extraHash? (readonly)

Returns provider-specific extra data.

Returns:

  • (Hash, nil)

    provider-specific extra data



22
23
24
# File 'lib/prompt_builder/items/function_call_output.rb', line 22

def extra
  @extra
end

#idString? (readonly)

Returns the function call output identifier.

Returns:

  • (String, nil)

    the function call output identifier



9
10
11
# File 'lib/prompt_builder/items/function_call_output.rb', line 9

def id
  @id
end

#outputString, ... (readonly)

Returns the output from the function; either a plain string, an array of content objects, or nil (serialized as “”).

Returns:

  • (String, Array<Content::Base>, nil)

    the output from the function; either a plain string, an array of content objects, or nil (serialized as “”)



19
20
21
# File 'lib/prompt_builder/items/function_call_output.rb', line 19

def output
  @output
end

#statusString? (readonly)

Returns the function call output status.

Returns:

  • (String, nil)

    the function call output status



15
16
17
# File 'lib/prompt_builder/items/function_call_output.rb', line 15

def status
  @status
end

Class Method Details

.from_h(hash) ⇒ FunctionCallOutput

Deserialize a FunctionCallOutput from a Hash.

Parameters:

  • hash (Hash)

    a Hash with string keys

Returns:



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/prompt_builder/items/function_call_output.rb', line 45

def from_h(hash)
  output = hash["output"]
  output = output.map { |c| Content::Base.from_h(c) } if output.is_a?(Array)
  new(
    id: hash["id"],
    call_id: hash["call_id"],
    status: hash["status"],
    output: output,
    **hash.except("type", "id", "call_id", "status", "output").transform_keys(&:to_sym)
  )
end

Instance Method Details

#to_hHash

Serialize to a Hash with string keys. A nil output is emitted as an empty string so the on-the-wire shape matches the Open Responses API.

Returns:

  • (Hash)


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/prompt_builder/items/function_call_output.rb', line 62

def to_h
  hash = {
    "type" => "function_call_output",
    "call_id" => @call_id,
    "output" => serialize_output
  }
  hash["id"] = @id if @id
  hash["status"] = @status if @status
  hash = PromptBuilder.jsonify(@extra).merge(hash) unless @extra.empty?
  hash
end