Skip to content

Commit 2bdc422

Browse files
committed
fix(docs): Improve error handling in Rails integration example for API calls
1 parent 80a459f commit 2bdc422

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

docs/guides/rails.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,17 @@ chat_record = Chat.create!(model_id: 'gpt-4.1-nano', user: current_user)
153153
# messages in your database.
154154
# 6. Returns the final `RubyLLM::Message` object on success, or raises the
155155
# `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
162167

163168
# Continue the conversation
164169
chat_record.ask "Tell me more about that city"

0 commit comments

Comments
 (0)