Skip to content

Refuse media content the RubyLLM provider would drop - #717

Merged
TonsOfFun merged 1 commit into
activeagents:mainfrom
yorzi:fix/ruby-llm-refuse-media
Nov 17, 2026
Merged

TonsOfFun merged 1 commit into
activeagents:mainfrom
yorzi:fix/ruby-llm-refuse-media

Conversation

@yorzi

@yorzi yorzi commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

What this does

RubyLLMProvider#extract_content_text kept only type: "text" blocks. A message holding an image, audio or a file reached RubyLLM as its text alone, and the reply read as if the model had seen it. Through an agent (generate_with :ruby_llm) with a fake RubyLLM provider capturing complete, on ruby_llm 1.16 and 2.0:

prompt(messages: [{ role: "user", content: [image_url block, text "Describe this image"] }])
  → RubyLLM receives content "Describe this image";   answer "A cat."
prompt(messages: [{ role: "user", content: [image_url block] }])
  → RubyLLM receives content "";                      answer "A cat."

The existing test "array content filters non-text blocks" asserted only that a response came back.

Such a message now raises ActiveAgent::Providers::Errors::InvalidRequest before any provider call. The error has provider_tag: "RubyLLM" and a message naming the block types. MEDIA_BLOCK_TYPES covers the shapes this repo's OpenAI Chat, OpenAI Responses and Anthropic transforms write: image, image_url, input_image, audio, input_audio, file, input_file, document, video. Other non-text blocks, such as an earlier turn's thinking, are still left out, so a conversation carried over from another provider keeps working. docs/providers/ruby_llm.md gains a short "Text-Only Content" section.

Tests, in test/providers/ruby_llm/ruby_llm_provider_test.rb:

  • Fail before the change: one case per media shape (image_url as a data URL, Anthropic image, input_audio, file, document). Each must raise InvalidRequest naming its type, with nothing reaching RubyLLM.
  • Guard existing behavior: text blocks are joined as before, with the content now asserted rather than only a non-nil response. A non-media thinking block is still dropped.
  • Green on ruby_llm 1.16 (gemfiles/ruby_llm_1.gemfile) and 2.0 (gemfiles/ruby_llm_2.gemfile).

Decision for the maintainer

This PR is also a proposal. Please confirm it, redirect it, or close it.

  • Refuse, or support images? I chose to refuse, as the smallest change that ends the silent loss. Supporting images through RubyLLM's attachments is a bigger, separate decision. The API differs between ruby_llm 1.x (RubyLLM::Content.new(text, attachments)) and 2.x (Message.new(attachments:)). data: URLs would need decoding, since 1.16 reads a non-http string as a file path. And RubyLLM fetches an http(s) image from the application server rather than passing the URL to the vendor, so a user-supplied URL becomes a server-side fetch. I'm happy to follow up with image support if you want it, as a separate PR.
  • Behavior change. A caller that sends media blocks through RubyLLM today gets an error instead of a text-only answer. That answer was already wrong, but it is visible in the CHANGELOG entry.
  • Not touched: prompt(image:) and prompt(document:) with RubyLLM already fail before this code, with ActiveModel::UnknownAttributeError: unknown attribute 'image' for ...RubyLLM::Messages::User. Giving them the same clear error would be a small follow-up in the request types.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Performance improvement

Scope check

  • I read the contributing guide
  • This fits activeagent's stated focus
  • This isn't application-specific logic that belongs in user code
  • This benefits most users, not just my use case

Related issue

None filed.

Quality check

  • The project's hooks and linters pass (bin/rubocop on the changed files)
  • The new tests fail before this change and pass after it
  • The full test suite passes. bin/test under gemfiles/ruby_llm_1.gemfile: 4613 runs, 0 failures, 11 errors. Under gemfiles/ruby_llm_2.gemfile: 4617 runs, 0 failures, 11 errors. The errors are all CliContractTest (rack-session undefined method 'id' for Hash), which fail identically on an untouched main in this environment
  • I updated documentation where needed
  • I didn't edit generated files by hand

Ownership

  • I understand every line of this change and can answer questions about it

API changes

  • Breaking change
  • New public methods or classes
  • Changed method signatures
  • No API changes (a request that silently lost media now raises Errors::InvalidRequest)

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.
@TonsOfFun
TonsOfFun merged commit 3ba8c48 into activeagents:main Nov 17, 2026
18 checks passed
TonsOfFun added a commit that referenced this pull request Nov 17, 2026
@TonsOfFun TonsOfFun added this to the 1.9.9 milestone Nov 17, 2026
@TonsOfFun TonsOfFun mentioned this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants