@_j thanks so much for the response.
I have tried multiple ways to tweak the timeout param from the SDK, I observed that the default is 10 mins, but in no way does the connection actually last more than 3 mins (180s), whether I tried it locally or on other servers. May I know where do you deploy your server? I will give it another try. I am ok to have the request run for longer as long as it eventually gives me a respond without timeout.
Same as what you mentioned, I also want to avoid retries, as it likely just time out anyways.
I have tried partial image + streaming also, it also time out.
Here’s my code for reference:
import OpenAI from 'openai';
import * as fs from 'fs';
import * as path from 'path';
import * as dotenv from 'dotenv';
dotenv.config();
async function main() {
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
timeout: 900000,
});
// Define the input images (same as the attached file)
const imageFiles = [
"./scene/baby_face.jpeg",
"./scene/scene_bedroom.jpeg",
];
console.log(`start time: ${new Date().toISOString()}`);
const startTime = new Date();
// Same prompt as the attached file
const prompt = `
Put the baby inside the crib in the bedroom. Show her waking up happily.
Create the illustration in a Disney animation style.
`;
try {
console.log('Starting OpenAI SDK streaming image edit...');
// Read both image files as buffers and create File objects
const imageBuffer1 = fs.readFileSync(imageFiles[0]);
const imageBuffer2 = fs.readFileSync(imageFiles[1]);
const imageFile1 = new File([imageBuffer1], 'baby_face.jpeg', { type: 'image/jpeg' });
const imageFile2 = new File([imageBuffer2], 'scene_bedroom.jpeg', { type: 'image/jpeg' });
// Use OpenAI SDK with streaming for images.edit
const stream = await openai.images.edit({
model: "gpt-image-1",
prompt: prompt,
image: [imageFile1, imageFile2], // Use both images as per documentation
n: 1,
size: "1024x1024",
quality: "high",
stream: true, // Enable streaming
partial_images: 2,
input_fidelity: "high"
}, {
timeout: 900000,
});
console.log('OpenAI SDK streaming response started!');
// Handle the streaming response
let imageCount = 0;
for await (const event of stream) {
console.log('event:', event);
if (event.type === "image_edit.partial_image") {
console.log('Received partial image event');
const idx = event.partial_image_index;
const imageBase64 = event.b64_json;
const imageBuffer = Buffer.from(imageBase64, "base64");
const fileName = `character_openai_sdk_streaming_partial_${Date.now()}_${imageCount}.png`;
const filePath = path.join(process.cwd(), 'generated_images', fileName);
// Create directory if it doesn't exist
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
fs.writeFileSync(filePath, imageBuffer);
console.log(`Image saved successfully to: ${filePath}`);
// fs.writeFileSync(`character_openai_sdk_streaming_${Date.now()}.png`, imageBuffer);
} else if (event.type === 'image_edit.completed') {
console.log('Processing completed image chunk...');
// Decode base64 data directly from chunk
const imageBuffer = Buffer.from(event.b64_json, 'base64');
const fileName = `character_openai_sdk_streaming_completed_${Date.now()}_${imageCount}.png`;
const filePath = path.join(process.cwd(), 'generated_images', fileName);
// Create directory if it doesn't exist
const dir = path.dirname(filePath);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
console.log(`Character image would be saved to: ${filePath}`);
// Save the decoded image
try {
fs.writeFileSync(filePath, imageBuffer);
console.log(`Image saved successfully to: ${filePath}`);
imageCount++;
} catch (saveError) {
console.error('Error saving image:', saveError);
}
}
}
console.log(`Streaming completed! Total images saved: ${imageCount}`);
} catch (error) {
const endTime = new Date();
const elapsed = endTime.getTime() - startTime.getTime();
console.error('Error during OpenAI SDK streaming image edit request:', error);
console.error(`Elapsed time before error: ${elapsed}ms`);
throw error;
}
console.log(`end time: ${new Date().toISOString()}`);
console.log(`total time: ${new Date().getTime() - startTime.getTime()}ms`);
}
main();