Skip to content

[BUG] RubyLLM::Content missing #empty? method causes NoMethodError in Anthropic provider #729

@judy

Description

@judy

Description

RubyLLM::Content does not implement #empty?, but several Anthropic provider code paths call msg.content.empty? on message content. When a message has ActiveStorage attachments, extract_content (in active_record/message_methods.rb) wraps the content in a Content object instead of returning a plain String. This causes a NoMethodError when the message is later formatted for the Anthropic API.

Steps to reproduce

  1. Create a message with ActiveStorage attachments (e.g., via has_many_attached :attachments on the message model)
  2. Have the agent respond with tool calls in the same conversation
  3. On the next turn, extract_content returns a RubyLLM::Content for the user message with attachments
  4. The Anthropic provider calls msg.content.empty? during format_tool_call_with_thinking or format_tool_result
  5. NoMethodError: undefined method 'empty?' for an instance of RubyLLM::Content

Affected code paths

  • lib/ruby_llm/providers/anthropic/chat.rb:179msg.content.nil? || msg.content.empty?
  • lib/ruby_llm/providers/anthropic/tools.rb:19msg.content.nil? || msg.content.empty?

Expected behavior

RubyLLM::Content should respond to #empty? (returning true when text is nil and attachments are empty), consistent with how String and Array respond to it.

Workaround

RubyLLM::Content.class_eval do
  def empty?
    text.nil? && attachments.empty?
  end
end

Environment

  • ruby_llm 1.14.0
  • Rails 8.1 with ActiveStorage
  • Anthropic provider (Claude models)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions