Repository navigation
Refuse media content the RubyLLM provider would drop - #717
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this does
RubyLLMProvider#extract_content_textkept onlytype: "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 capturingcomplete, on ruby_llm 1.16 and 2.0:The existing test "array content filters non-text blocks" asserted only that a response came back.
Such a message now raises
ActiveAgent::Providers::Errors::InvalidRequestbefore any provider call. The error hasprovider_tag: "RubyLLM"and a message naming the block types.MEDIA_BLOCK_TYPEScovers 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'sthinking, are still left out, so a conversation carried over from another provider keeps working.docs/providers/ruby_llm.mdgains a short "Text-Only Content" section.Tests, in
test/providers/ruby_llm/ruby_llm_provider_test.rb:image_urlas a data URL, Anthropicimage,input_audio,file,document). Each must raiseInvalidRequestnaming its type, with nothing reaching RubyLLM.thinkingblock is still dropped.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.
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 anhttp(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.prompt(image:)andprompt(document:)with RubyLLM already fail before this code, withActiveModel::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
Scope check
Related issue
None filed.
Quality check
bin/rubocopon the changed files)bin/testundergemfiles/ruby_llm_1.gemfile: 4613 runs, 0 failures, 11 errors. Undergemfiles/ruby_llm_2.gemfile: 4617 runs, 0 failures, 11 errors. The errors are allCliContractTest(rack-sessionundefined method 'id' for Hash), which fail identically on an untouchedmainin this environmentOwnership
API changes
Errors::InvalidRequest)