Class: RubyLLM::BedrockInvoke::Protocol

Inherits:
Protocols::Anthropic
  • Object
show all
Includes:
EventStream
Defined in:
lib/ruby_llm/bedrock_invoke/protocol_v2.rb

Overview

The Anthropic Messages wire format over Bedrock InvokeModel (RubyLLM 2.0).

Mirrors the in-tree VertexAI::Anthropic pattern: inherit the entire Anthropic protocol and override only the endpoints, the Bedrock body envelope (no top-level model/stream, anthropic_version marker), SigV4 signing, and AWS event-stream decoding for streaming.

Constant Summary collapse

ANTHROPIC_VERSION =
'bedrock-2023-05-31'

Constants included from EventStream

EventStream::EXCEPTION_STATUS

Instance Method Summary collapse

Methods included from EventStream

#each_event_stream_event, #event_stream_decoder

Instance Method Details

#build_thinking_payload(thinking, _model) ⇒ Object

2.0 gates thinking on registry reasoning_options, which assumed models never carry; Bedrock Claude supports budget thinking, so emit it directly. Adaptive/effort-only thinking is not available on Bedrock.



51
52
53
54
55
56
# File 'lib/ruby_llm/bedrock_invoke/protocol_v2.rb', line 51

def build_thinking_payload(thinking, _model)
  inner = RubyLLM::BedrockInvoke.budget_thinking_payload(thinking)
  return nil if inner.nil?

  { thinking: inner }
end

#completion_urlObject



16
17
18
# File 'lib/ruby_llm/bedrock_invoke/protocol_v2.rb', line 16

def completion_url
  "/model/#{RubyLLM::BedrockInvoke.escape_model_id(@model.id)}/invoke"
end

#format_message(msg) ⇒ Object

Replays messages that carried server-managed tool-search blocks verbatim; everything else renders through the stock Anthropic format.



41
42
43
44
45
46
# File 'lib/ruby_llm/bedrock_invoke/protocol_v2.rb', line 41

def format_message(msg, **)
  blocks = RubyLLM::BedrockInvoke.raw_blocks(msg)
  return super unless blocks

  { role: 'assistant', content: blocks }
end

#parse_completion_body(data, raw:) ⇒ Object



33
34
35
36
37
# File 'lib/ruby_llm/bedrock_invoke/protocol_v2.rb', line 33

def parse_completion_body(data, raw:)
  message = super
  preserve_server_blocks(message, data)
  message
end

#render_payload(messages, **options) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/ruby_llm/bedrock_invoke/protocol_v2.rb', line 24

def render_payload(messages, **options)
  payload = super
  payload.delete(:model)
  payload.delete(:stream)
  payload[:anthropic_version] = ANTHROPIC_VERSION
  ToolSearch.apply!(payload, options[:tools] || {})
  payload
end

#stream_urlObject



20
21
22
# File 'lib/ruby_llm/bedrock_invoke/protocol_v2.rb', line 20

def stream_url
  "/model/#{RubyLLM::BedrockInvoke.escape_model_id(@model.id)}/invoke-with-response-stream"
end

#supports_provider_file_references?Boolean

Bedrock has no Anthropic Files API.

Returns:

  • (Boolean)


59
60
61
# File 'lib/ruby_llm/bedrock_invoke/protocol_v2.rb', line 59

def supports_provider_file_references?
  false
end