Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit d2caf0f

Browse files
committed
FIX: handle error conditions when generating images gracefully
1 parent 47d3705 commit d2caf0f

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

lib/inference/open_ai_image_generator.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ def self.create_uploads!(
4040
output_format: output_format,
4141
)
4242

43+
raise api_responses[0] if api_responses.all? { |resp| resp.is_a?(StandardError) }
44+
45+
api_responses =
46+
api_responses.filter_map { |response| response.is_a?(StandardError) ? nil : response }
47+
4348
create_uploads_from_responses(api_responses, user_id, for_private_message, title)
4449
end
4550

@@ -151,11 +156,16 @@ def self.generate_images_in_threads(
151156
)
152157
rescue => e
153158
attempts += 1
154-
sleep 2
155-
retry if attempts < 3
156-
Discourse.warn_exception(e, message: "Failed to generate image for prompt #{prompt}")
159+
# to keep tests speedy
160+
if !Rails.env.test?
161+
retry if attempts < 3
162+
end
163+
Discourse.warn_exception(
164+
e,
165+
message: "Failed to generate image for prompt #{prompt}\n",
166+
)
157167
puts "Error generating image for prompt: #{prompt} #{e}" if Rails.env.development?
158-
nil
168+
e
159169
end
160170
end
161171
end

lib/personas/tools/create_image.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def prompts
3030
end
3131

3232
def chain_next_response?
33-
false
33+
!!@error
3434
end
3535

3636
def invoke
@@ -42,14 +42,20 @@ def invoke
4242

4343
results = nil
4444

45-
results =
46-
DiscourseAi::Inference::OpenAiImageGenerator.create_uploads!(
47-
max_prompts,
48-
model: "gpt-image-1",
49-
user_id: bot_user.id,
50-
)
45+
begin
46+
results =
47+
DiscourseAi::Inference::OpenAiImageGenerator.create_uploads!(
48+
max_prompts,
49+
model: "gpt-image-1",
50+
user_id: bot_user.id,
51+
)
52+
rescue => e
53+
@error = e
54+
return { prompts: max_prompts, error: e.message }
55+
end
5156

5257
if results.blank?
58+
@error = true
5359
return { prompts: max_prompts, error: "Something went wrong, could not generate image" }
5460
end
5561

spec/lib/personas/tools/create_image_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@
2222
end
2323

2424
describe "#process" do
25+
it "can reject generation of images and return a proper error to llm" do
26+
error_message = {
27+
error: {
28+
message:
29+
"Your request was rejected as a result of our safety system. Your request may contain content that is not allowed by our safety system.",
30+
type: "user_error",
31+
param: nil,
32+
code: "moderation_blocked",
33+
},
34+
}
35+
36+
WebMock.stub_request(:post, "https://api.openai.com/v1/images/generations").to_return(
37+
status: 400,
38+
body: error_message.to_json,
39+
)
40+
41+
info = create_image.invoke(&progress_blk).to_json
42+
expect(info).to include("Your request was rejected as a result of our safety system.")
43+
expect(create_image.chain_next_response?).to eq(true)
44+
end
2545
it "can generate images with gpt-image-1 model" do
2646
data = [{ b64_json: base64_image, revised_prompt: "a watercolor painting of flowers" }]
2747

0 commit comments

Comments
 (0)