You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/_core_features/chat.md
+7-5Lines changed: 7 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -653,18 +653,18 @@ You can register blocks to be called when certain events occur during the chat l
653
653
654
654
### Available Event Handlers
655
655
656
-
RubyLLM provides four event handlers that cover the complete chat lifecycle:
656
+
RubyLLM provides two callback styles. The `on_*`handlers replace any previously registered handler for the same event, which is useful when you want to override behavior. The Rails-style `before_*` and `after_*` callbacks are additive, so multiple registrations for the same event all run. Additive callbacks are available from v1.15+.
657
657
658
658
```ruby
659
659
chat =RubyLLM.chat
660
660
661
661
# Called at first chunk received from the assistant
662
-
chat.on_new_messagedo
662
+
chat.before_messagedo
663
663
print"Assistant > "
664
664
end
665
665
666
666
# Called after the complete assistant message (including tool calls/results) is received
667
-
chat.on_end_messagedo |message|
667
+
chat.after_messagedo |message|
668
668
puts"Response complete!"
669
669
# Note: message might be nil if an error occurred during the request
670
670
if message && message.output_tokens
@@ -673,19 +673,21 @@ chat.on_end_message do |message|
673
673
end
674
674
675
675
# Called when the AI decides to use a tool
676
-
chat.on_tool_calldo |tool_call|
676
+
chat.before_tool_calldo |tool_call|
677
677
puts"AI is calling tool: #{tool_call.name} with arguments: #{tool_call.arguments}"
678
678
end
679
679
680
680
# Called after a tool returns its result
681
-
chat.on_tool_resultdo |result|
681
+
chat.after_tool_resultdo |result|
682
682
puts"Tool returned: #{result}"
683
683
end
684
684
685
685
# These callbacks work for both streaming and non-streaming requests
686
686
chat.ask "What is metaprogramming in Ruby?"
687
687
```
688
688
689
+
The older `on_new_message`, `on_end_message`, `on_tool_call`, and `on_tool_result` handlers are still available and keep their replacing behavior. RubyLLM logs a deprecation warning when one of these handlers is used; prefer the additive Rails-style callbacks for new code.
690
+
689
691
## Raw Responses
690
692
691
693
You can access the raw response from the API provider with `response.raw`.
> Raising an exception in `on_tool_call` breaks the conversation flow - the LLM expects a tool response after requesting a tool call. This can leave the chat in an inconsistent state. Consider using better models or clearer tool descriptions to prevent loops instead of hard limits.
424
+
> Raising an exception in `before_tool_call` breaks the conversation flow - the LLM expects a tool response after requesting a tool call. This can leave the chat in an inconsistent state. Consider using better models or clearer tool descriptions to prevent loops instead of hard limits.
0 commit comments