From 09f8e9c0a5777cb6faf5d454e05693c68a4aa25d Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Sun, 15 Nov 2026 10:16:40 +0800 Subject: [PATCH] Refuse media content the RubyLLM provider would drop RubyLLMProvider#extract_content_text kept only `type: "text"` blocks, so a message holding an image (image_url, image, input_image), audio or a file reached RubyLLM as its text alone, or as an empty string when it held only the image, and the reply read as if the model had seen it. The test named "array content filters non-text blocks" asserted only that a response came back. Such a message now raises Errors::InvalidRequest (provider_tag "RubyLLM") naming the block types, before any provider call. MEDIA_BLOCK_TYPES lists the shapes the OpenAI Chat, OpenAI Responses and Anthropic transforms write. Other non-text blocks, such as an earlier turn's reasoning, are still left out, so a history carried over from another provider keeps working. The provider guide gains a Text-Only Content section. Sending images through RubyLLM's attachments is left for a follow-up: it differs between ruby_llm 1.x (Content) and 2.x (Message attachments:), needs data: URLs decoded, and has RubyLLM fetch http(s) image URLs from the application server. --- CHANGELOG.md | 13 ++++++ docs/providers/ruby_llm.md | 10 +++++ .../providers/ruby_llm_provider.rb | 21 ++++++++++ .../ruby_llm/ruby_llm_provider_test.rb | 42 +++++++++++++------ 4 files changed, 74 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50e736f6e..35d43733e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/providers/ruby_llm.md b/docs/providers/ruby_llm.md index 5e7023a70..c5381e756 100644 --- a/docs/providers/ruby_llm.md +++ b/docs/providers/ruby_llm.md @@ -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:** @@ -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 diff --git a/lib/active_agent/providers/ruby_llm_provider.rb b/lib/active_agent/providers/ruby_llm_provider.rb index e5e6908ac..3515e6c5c 100644 --- a/lib/active_agent/providers/ruby_llm_provider.rb +++ b/lib/active_agent/providers/ruby_llm_provider.rb @@ -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") @@ -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] ActiveAgent format tool calls diff --git a/test/providers/ruby_llm/ruby_llm_provider_test.rb b/test/providers/ruby_llm/ruby_llm_provider_test.rb index 4d73dea4f..64725a39a 100644 --- a/test/providers/ruby_llm/ruby_llm_provider_test.rb +++ b/test/providers/ruby_llm/ruby_llm_provider_test.rb @@ -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 ---