Skip to content
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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- **The RubyLLM provider refuses media it would drop** (`activeagent`). It
sends RubyLLM the text of each message and silently left out every other
content block, so a message with an image (`image_url`, `image`,
`input_image`), audio or a file reached the model as its text alone, or
as an empty message, and the model answered as if it had seen it. Such
a message now raises `ActiveAgent::Providers::Errors::InvalidRequest`,
naming the block types, before any provider call. Blocks that are not
media (an earlier turn's reasoning) are still left out, as before.

## [1.9.8] - 2026-11-12

The dashboard holds a run to its catalog's pass bars: a product's and a
Expand Down
10 changes: 10 additions & 0 deletions docs/providers/ruby_llm.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ For interleaved parallel OpenAI tool calls, use ruby_llm 2.x. ruby_llm 1.16 disc

See [Streaming](/agents/streaming) for ActionCable integration and real-time updates.

## Text-Only Content

The provider sends RubyLLM the text of each message. A message whose
content holds an image, audio or file block (`image_url`, `image`,
`input_image`, `input_audio`, `file`, `input_file`, `document`, and the
like) is refused with `ActiveAgent::Providers::Errors::InvalidRequest`
before any provider call, rather than sent without it. Use the OpenAI,
Anthropic or OpenRouter provider for images and files.

## When to Use RubyLLM vs Direct Providers

**Use RubyLLM when:**
Expand All @@ -253,6 +262,7 @@ See [Streaming](/agents/streaming) for ActionCable integration and real-time upd

**Use a direct provider (OpenAI, Anthropic) when:**
- You need provider-specific features (MCP servers, extended thinking, JSON schema mode)
- You send images, audio or files (see [Text-Only Content](#text-only-content))
- You want the tightest integration with a provider's gem SDK
- You need provider-specific error handling classes

Expand Down
21 changes: 21 additions & 0 deletions lib/active_agent/providers/ruby_llm_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ def extract_content_text(content)
when String
content
when Array
refuse_media_blocks!(content)
content.select { |block| block.is_a?(Hash) && block[:type] == "text" }
.map { |block| block[:text] }
.join("\n")
Expand All @@ -389,6 +390,26 @@ def extract_content_text(content)
end
end

# Content block types that carry media rather than text, in the shapes
# the OpenAI (Chat and Responses) and Anthropic transforms write.
MEDIA_BLOCK_TYPES = %w[image image_url input_image audio input_audio file input_file document video].freeze

# This provider sends RubyLLM text only, so an image, audio clip or
# file would be dropped while the model answered as if it had seen
# it. Such a request is refused before any provider call. Other
# blocks that are not text (an earlier turn's reasoning) are still
# left out.
def refuse_media_blocks!(content)
types = content.filter_map { |block| block[:type].to_s if block.is_a?(Hash) && MEDIA_BLOCK_TYPES.include?(block[:type].to_s) }.uniq
return if types.empty?

raise Errors::InvalidRequest.new(
"The RubyLLM provider sends text only; this message has #{types.join(', ')} content, which it would drop. " \
"Send it through a provider that accepts it, or as text.",
provider_tag: "RubyLLM"
)
end

# Converts ActiveAgent tool_calls to RubyLLM's ToolCall format.
#
# @param tool_calls [Array<Hash>] ActiveAgent format tool calls
Expand Down
42 changes: 30 additions & 12 deletions test/providers/ruby_llm/ruby_llm_provider_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -637,26 +637,44 @@ def complete(messages, **kwargs)
response = provider.prompt

assert_not_nil response
assert_equal "Hello\nWorld", @fake_provider.last_messages.find { |message| message.role == :user }.content
end

test "array content filters non-text blocks" do
# The provider sends RubyLLM text only. An image, audio clip or file in
# the content would be dropped, and the model would answer as if it had
# seen it, so the request is refused before any provider call.
[
{ type: "image_url", image_url: { url: "data:image/png;base64,iVBORw0KGgo=" } },
{ type: "image", source: { type: "url", url: "https://example.com/cat.png" } },
{ type: "input_audio", input_audio: { data: "UklGRg==", format: "wav" } },
{ type: "file", file: { filename: "report.pdf", file_data: "data:application/pdf;base64,JVBERi0=" } },
{ type: "document", source: { type: "url", url: "https://example.com/report.pdf" } }
].each do |block|
test "array content with a #{block[:type]} block is refused, not sent as text alone" do
provider = ActiveAgent::Providers::RubyLLMProvider.new(
service: "RubyLLM",
model: "gpt-4o-mini",
messages: [ { role: "user", content: [ block, { type: "text", text: "Describe this" } ] } ]
)

error = assert_raises(ActiveAgent::Providers::Errors::InvalidRequest) { provider.prompt }

assert_match(/sends text only.*#{block[:type]}/, error.message)
assert_equal "RubyLLM", error.provider_tag
assert_nil @fake_provider.last_messages, "nothing reached RubyLLM"
end
end

test "array content keeps leaving out blocks that are not media" do
provider = ActiveAgent::Providers::RubyLLMProvider.new(
service: "RubyLLM",
model: "gpt-4o-mini",
messages: [
{
role: "user",
content: [
{ type: "image_url", image_url: { url: "data:image/png;base64,..." } },
{ type: "text", text: "Describe this image" }
]
}
]
messages: [ { role: "user", content: [ { type: "thinking", thinking: "Earlier reasoning." }, { type: "text", text: "Describe this" } ] } ]
)

response = provider.prompt
provider.prompt

assert_not_nil response
assert_equal "Describe this", @fake_provider.last_messages.find { |message| message.role == :user }.content
end

# --- Empty/nil tool_calls ---
Expand Down
Loading