Module: Raif::Concerns::ToolCallValidation

Extended by:
ActiveSupport::Concern
Included in:
Agents::NativeToolCallingAgent, Raif::ConversationEntry
Defined in:
app/models/raif/concerns/tool_call_validation.rb

Overview

Shared validation logic for developer-managed tool calls returned by an LLM. Produces a structured result so callers (agents, conversations) can apply their own retry/error semantics.

Validation checks, in order:

  1. Tool exists in the provided tool map.

  2. prepare_tool_arguments on the tool class yields a Hash.

  3. The prepared Hash satisfies the tool’s tool_arguments_schema.

Defined Under Namespace

Classes: ValidationResult

Instance Method Summary collapse

Instance Method Details

#validate_tool_call(tool_call, available_model_tools_map) ⇒ ValidationResult

Parameters:

  • tool_call (Hash)

    A tool call Hash as returned by ‘ModelCompletion#response_tool_calls`. Expected shape: { “name” => String, “arguments” => anything, “provider_tool_call_id” => String? }

  • available_model_tools_map (Hash{String => Class})

    Map of tool name to tool class.

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/raif/concerns/tool_call_validation.rb', line 24

def validate_tool_call(tool_call, available_model_tools_map)
  tool_name = tool_call["name"]
  raw_arguments = tool_call["arguments"]
  tool_klass = available_model_tools_map[tool_name]

  if tool_klass.nil?
    return ValidationResult.new(
      status: :unknown_tool,
      tool_name: tool_name,
      raw_arguments: raw_arguments,
      prepared_arguments: nil,
      tool_klass: nil,
      errors: nil
    )
  end

  # Tools may override `prepare_tool_arguments`; the base implementation is
  # safe, but subclass overrides can raise on unexpected input shapes. Rescue
  # so the retry loop sees a structured :preparation_error rather than
  # exploding out of validation.
  begin
    prepared = tool_klass.prepare_tool_arguments(raw_arguments)
  rescue StandardError => e
    return ValidationResult.new(
      status: :preparation_error,
      tool_name: tool_name,
      raw_arguments: raw_arguments,
      prepared_arguments: nil,
      tool_klass: tool_klass,
      errors: ["#{e.class.name}: #{e.message}"]
    )
  end

  unless prepared.is_a?(Hash)
    return ValidationResult.new(
      status: :non_hash_arguments,
      tool_name: tool_name,
      raw_arguments: raw_arguments,
      prepared_arguments: prepared,
      tool_klass: tool_klass,
      errors: nil
    )
  end

  validation_errors = JSON::Validator.fully_validate(tool_klass.tool_arguments_schema, prepared)
  if validation_errors.any?
    return ValidationResult.new(
      status: :schema_mismatch,
      tool_name: tool_name,
      raw_arguments: raw_arguments,
      prepared_arguments: prepared,
      tool_klass: tool_klass,
      errors: validation_errors
    )
  end

  ValidationResult.new(
    status: :ok,
    tool_name: tool_name,
    raw_arguments: raw_arguments,
    prepared_arguments: prepared,
    tool_klass: tool_klass,
    errors: nil
  )
end