Skip to content

Remove spurious 'single system message' warning from Anthropic provider#768

Merged
crmne merged 1 commit into
crmne:mainfrom
untitledstartup:fix/anthropic-multi-system-warning
May 12, 2026
Merged

Remove spurious 'single system message' warning from Anthropic provider#768
crmne merged 1 commit into
crmne:mainfrom
untitledstartup:fix/anthropic-multi-system-warning

Conversation

@timherby
Copy link
Copy Markdown
Contributor

What this does

Removes a stale RubyLLM.logger.warn(...) call in Anthropic::Chat#build_system_content that fires on every request whenever a chat carries more than one :system-role message. The warning claims Anthropic only supports a single system message and that the gem will merge them -- neither is still true. Anthropic's system parameter has accepted an array of text content blocks (with per-block cache_control) since the prompt-caching launch in August 2024 (GA December 2024), and the gem already serializes correctly via flat_map over Media.format_content. Only the warning is stale.

At high request volume the warning floods logs for applications that intentionally split system content into a cached prefix + a per-conversation envelope. The Bedrock provider (lib/ruby_llm/providers/bedrock/chat.rb) handles the same case without warning, so this also aligns the two providers.

Adds direct unit coverage for build_system_content:

  • returns [] for zero messages
  • returns one block for one message
  • returns two blocks (in order) for two messages
  • does not log a warning when multiple :system messages are passed
  • preserves cache_control on a Content::Raw block when paired with a plain-text block
  • flattens mixed Content::Raw + plain-string content correctly

Fixes #767.

References:

Type of change

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

Scope check

  • I read the Contributing Guide
  • This aligns with RubyLLM's focus on LLM communication
  • This isn't application-specific logic that belongs in user code
  • This benefits most users, not just my specific use case

Required for new features

Quality check

  • I ran overcommit --install and all hooks pass
  • I tested my changes thoroughly
    • For provider changes: Re-recorded VCR cassettes with bundle exec rake vcr:record[provider_name] (N/A -- this change is wire-format-neutral; no provider request/response shape changed, so existing cassettes still match)
    • All tests pass: bundle exec rspec spec/ruby_llm/providers/anthropic/chat_spec.rb (13 examples, 0 failures -- 6 new + 7 existing) and bundle exec rspec spec/ruby_llm/providers/anthropic_spec.rb (2 examples, 0 failures)
  • I updated documentation if needed (no user-facing docs reference this warning)
  • I didn't modify auto-generated files manually (models.json, aliases.json)

AI-generated code

  • I used AI tools to help write this code
  • I have reviewed and understand all generated code (required if above is checked)

API changes

  • Breaking change
  • New public methods/classes
  • Changed method signatures
  • No API changes

The build_system_content method already serializes multiple :system
messages into Anthropic's `system: [...]` content-block array
correctly (preserving per-block cache_control). The warning predates
that support and is now misleading -- applications that intentionally
split system content into separate blocks (e.g. a static cached
prefix + a per-conversation envelope) get spurious log spew on every
request. Match the Bedrock provider, which handles the same case
without warning.

Adds unit coverage for build_system_content directly: zero/one/two
messages, the no-warn assertion, cache_control round-trip, and mixed
Raw + plain string content.

Fixes crmne#767
@codecov
Copy link
Copy Markdown

codecov Bot commented May 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.05%. Comparing base (ff39289) to head (9cb1ea9).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #768      +/-   ##
==========================================
+ Coverage   87.04%   87.05%   +0.01%     
==========================================
  Files         119      119              
  Lines        5596     5594       -2     
  Branches     1409     1407       -2     
==========================================
- Hits         4871     4870       -1     
+ Misses        725      724       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@crmne crmne merged commit 4942d6c into crmne:main May 12, 2026
23 checks passed
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.

Spurious "single system message" warning when multiple :system messages are passed to the Anthropic provider

2 participants