File tree Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Expand file tree Collapse file tree 1 file changed +11
-6
lines changed Original file line number Diff line number Diff line change @@ -153,12 +153,17 @@ chat_record = Chat.create!(model_id: 'gpt-4.1-nano', user: current_user)
153
153
# messages in your database.
154
154
# 6. Returns the final `RubyLLM::Message` object on success, or raises the
155
155
# `RubyLLM::Error` on failure.
156
- response = chat_record.ask " What is the capital of France?"
157
-
158
- # `response` is the RubyLLM::Message object from the API call.
159
- # The persisted record is associated with `chat_record`.
160
- assistant_message_record = chat_record.messages.last
161
- puts assistant_message_record.content # => "The capital of France is Paris."
156
+ begin
157
+ response = chat_record.ask " What is the capital of France?"
158
+ # `response` is the RubyLLM::Message object from the successful API call.
159
+ # The assistant message is now fully persisted.
160
+ assistant_message_record = chat_record.messages.last
161
+ puts assistant_message_record.content # => "The capital of France is Paris."
162
+ rescue RubyLLM ::Error => e
163
+ # The empty assistant message record has been cleaned up automatically.
164
+ # Only the user message remains for this turn.
165
+ puts " API Call Failed: #{ e.message } "
166
+ end
162
167
163
168
# Continue the conversation
164
169
chat_record.ask " Tell me more about that city"
You can’t perform that action at this time.
0 commit comments