ExamplescriptIntermediateRunnableguided-flow
Sora 2 with a reference image: generate the image, then the video
Generate a reference image with gpt-5 through the Responses API, resize it to 720x1280 and pass it to Sora 2 as input_reference. Moderation on references is strict.
Key Facts
- Level
- Intermediate
- Runtime
- Python • OpenAI API
- Pattern
- Inspectable flow with visible system boundaries
- Interaction
- Live sandbox • Script
- Code last changed
- 7 October 2025
Navigate this example
Library
Browse examplesReopen the wider library to compare adjacent patterns and linked learning paths.Interaction
Run sandbox nowTry the interaction directly in this example’s guided sandbox surface.Source
Open full sourceRead the real implementation, highlighted checkpoints, and runtime requirements.MCP
Call via MCPUse the same resource inside agents, deterministic exports, and MCP setup flows.
Linked principles
2-references.py
python
import time
from openai import OpenAI
from pathlib import Path
import base64
from utils.resizer import resize_image
from utils.downloader import download_sora_video
openai = OpenAI()
# --------------------------------------------------------------
# Generate a reference image
# --------------------------------------------------------------
# Takes abbout 2 minutes
response = openai.responses.create(
model="gpt-5",
input="A professional studio desk setup with Shure SM7B microphone on boom arm. Dark background with soft blue LED light glow creating ambient atmosphere. Clean desk surface, professional lighting equipment visible. Cinematic look, moody lighting, no people.",
tools=[
{
"type": "image_generation",
"size": "1024x1536",
"quality": "high",
}
],
)
# --------------------------------------------------------------
# Resize the image to 720x1280 and save it
# --------------------------------------------------------------
message_outputs = [output for output in response.output if output.type == "message"]
if message_outputs:
for msg in message_outputs:
for content in msg.content:
if hasattr(content, "text"):
print(f"API Response: {content.text}")
image_data = [
output.result
for output in response.output
if output.type == "image_generation_call"
]
if image_data:
image_base64 = image_data[0]
image_path = f"./references/{response.id}.png"
with open(image_path, "wb") as f:
f.write(base64.b64decode(image_base64))
resize_image(image_path)
print(f"Saved and resized image to 720x1280: {image_path}")
else:
print("No image was generated. Check the API response above.")
# --------------------------------------------------------------
# Use input reference image
# --------------------------------------------------------------
# The moderation here is quite strict.
# You might need to try different images/prompts if you get an error.
video = openai.videos.create(
model="sora-2",
prompt="Make this image come to life",
input_reference=Path(f"./references/{response.id}.png"),
size="720x1280",
seconds=4,
)
print("Video generation started:", video)
# --------------------------------------------------------------
# Get last video
# --------------------------------------------------------------
time.sleep(3)
last_video = openai.videos.list().data[0]
# --------------------------------------------------------------
# Download the video
# --------------------------------------------------------------
video = download_sora_video(video=last_video, output_folder="./output")
Related principles
- P2VisibilityEnsure that background work remains perceptibleWhen the system is operating asynchronously or outside the user’s immediate focus, it should provide persistent and proportionate signals that work is continuing.Open principle →
- P4TrustApply progressive disclosure to system agencyProvide the minimum information necessary by default, while enabling users to inspect additional detail when confidence, understanding, or intervention is required.Open principle →
- P5DelegationReplace implied magic with clear mental modelsThe product should help users understand what the system can do, what it is currently doing, what it cannot do, and what conditions govern its behaviour.Open principle →
- P7TrustEstablish trust through inspectabilityUsers should be able to examine how a result was produced when confidence, accountability, or decision quality is important.Open principle →
- P9OrchestrationRepresent delegated work as a system, not merely as a conversationWhere work involves multiple steps, agents, dependencies, or concurrent activities, it should be represented as a structured system rather than solely as a message stream.Open principle →