Skip to content

Conversation

@kieranklaassen
Copy link
Contributor

@kieranklaassen kieranklaassen commented Nov 20, 2025

Problem

When using acts_as_chat, persistence callbacks were silently overwritten when users added their own callbacks:

chat = Chat.create!(model: 'gpt-4') chat.to_llm.on_new_message { puts "my callback" } # This overwrote persistence! chat.ask("Hello") # Messages not saved to database

Solution

Add a simple CallbackFanout class that appends callbacks instead of replacing them:

# Now both callbacks run chat.to_llm.on_new_message { puts "my callback" } chat.ask("Hello") # Messages saved AND user callback runs

Changes

  • lib/ruby_llm/chat.rb: Add 18-line CallbackFanout class
  • lib/ruby_llm/active_record/*.rb: Remove 32 lines of brittle callback wrapping code

Net result: -20 lines while fixing the bug.

When using acts_as_chat, persistence callbacks were being overwritten when users added their own callbacks via on_new_message, on_end_message, etc. This caused silent data loss. The fix adds a simple CallbackFanout class that appends callbacks instead of replacing them, so both persistence and user callbacks run. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
@kieranklaassen kieranklaassen force-pushed the fix-callback-persistence-chaining branch from dce45e2 to 6e80976 Compare November 22, 2025 22:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant