Skip to content

Anthropic: Fix system prompt (use plain text instead of serialized JSON) #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/ruby_llm/providers/anthropic/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def render_payload(messages, tools:, temperature:, model:, stream: false, schema
system_messages, chat_messages = separate_messages(messages)
system_content = build_system_content(system_messages)

build_base_payload(chat_messages, temperature, model, stream).tap do |payload|
add_optional_fields(payload, system_content:, tools:)
build_base_payload(chat_messages, model, stream).tap do |payload|
add_optional_fields(payload, system_content:, tools:, temperature:)
end
end

Expand All @@ -32,22 +32,22 @@ def build_system_content(system_messages)
)
end

system_messages.map { |msg| format_message(msg)[:content] }.join("\n\n")
system_messages.map(&:content).join("\n\n")
end

def build_base_payload(chat_messages, temperature, model, stream)
def build_base_payload(chat_messages, model, stream)
{
model: model,
messages: chat_messages.map { |msg| format_message(msg) },
temperature: temperature,
stream: stream,
max_tokens: RubyLLM.models.find(model)&.max_tokens || 4096
}
end

def add_optional_fields(payload, system_content:, tools:)
def add_optional_fields(payload, system_content:, tools:, temperature:)
payload[:tools] = tools.values.map { |t| Tools.function_for(t) } if tools.any?
payload[:system] = system_content unless system_content.empty?
payload[:temperature] = temperature unless temperature.nil?
end

def parse_completion_response(response)
Expand Down
7 changes: 3 additions & 4 deletions lib/ruby_llm/providers/bedrock/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,15 @@ def render_payload(messages, tools:, temperature:, model:, stream: false, schema
system_messages, chat_messages = Anthropic::Chat.separate_messages(messages)
system_content = Anthropic::Chat.build_system_content(system_messages)

build_base_payload(chat_messages, temperature, model).tap do |payload|
Anthropic::Chat.add_optional_fields(payload, system_content:, tools:)
build_base_payload(chat_messages, model).tap do |payload|
Anthropic::Chat.add_optional_fields(payload, system_content:, tools:, temperature:)
end
end

def build_base_payload(chat_messages, temperature, model)
def build_base_payload(chat_messages, model)
{
anthropic_version: 'bedrock-2023-05-31',
messages: chat_messages.map { |msg| format_message(msg) },
temperature: temperature,
max_tokens: RubyLLM.models.find(model)&.max_tokens || 4096
}
end
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading