Module: RubyLLM::Providers::OpenAI::Tools

Defined in:
lib/swarm_sdk/ruby_llm_patches/openai_thought_signature_patch.rb

Class Method Summary collapse

Class Method Details

.format_tool_calls(tool_calls) ⇒ Array<Hash>?

Serialize tool calls into OpenAI-format request data

Parameters:

  • tool_calls (Hash{String => ToolCall})

    Tool calls to serialize

Returns:

  • (Array<Hash>, nil)

    Serialized tool calls for API request



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/swarm_sdk/ruby_llm_patches/openai_thought_signature_patch.rb', line 60

def format_tool_calls(tool_calls)
  return unless tool_calls&.any?

  tool_calls.map do |_, tc|
    entry = {
      id: tc.id,
      type: "function",
      function: {
        name: tc.name,
        arguments: JSON.generate(tc.arguments),
      },
    }

    if tc.thought_signature
      entry[:extra_content] = { google: { thought_signature: tc.thought_signature } }
    end

    entry
  end
end

.parse_tool_call_arguments(tool_call) ⇒ Hash

Parse tool call arguments from raw hash

Parameters:

  • tool_call (Hash)

    Raw tool call hash

Returns:

  • (Hash)

    Parsed arguments



85
86
87
88
89
90
91
92
93
# File 'lib/swarm_sdk/ruby_llm_patches/openai_thought_signature_patch.rb', line 85

def parse_tool_call_arguments(tool_call)
  arguments = tool_call.dig("function", "arguments")

  if arguments.nil? || arguments.empty?
    {}
  else
    JSON.parse(arguments)
  end
end

.parse_tool_calls(tool_calls, parse_arguments: true) ⇒ Hash{String => ToolCall}?

Parse tool calls from OpenAI-format response data

Parameters:

  • tool_calls (Array<Hash>)

    Raw tool call data from API response

  • parse_arguments (Boolean) (defaults to: true)

    Whether to JSON-parse arguments (false during streaming)

Returns:

  • (Hash{String => ToolCall}, nil)

    Parsed tool calls keyed by ID



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/swarm_sdk/ruby_llm_patches/openai_thought_signature_patch.rb', line 34

def parse_tool_calls(tool_calls, parse_arguments: true)
  return unless tool_calls&.any?

  tool_calls.to_h do |tc|
    thought_sig = tc.dig("extra_content", "google", "thought_signature")

    [
      tc["id"],
      ToolCall.new(
        id: tc["id"],
        name: tc.dig("function", "name"),
        arguments: if parse_arguments
                     parse_tool_call_arguments(tc)
                   else
                     tc.dig("function", "arguments")
                   end,
        thought_signature: thought_sig,
      ),
    ]
  end
end