Sub topic 9
Sandbox containers, input files, and generated files
This chapter assumes you have used the code_interpreter tool from the previous chapter and can read the items in a Responses API output list. Here you will see how the sandbox is stored, how to give it files, and how to get files back.
What a container is
When the model runs code, it runs inside a container. This is a provider-managed sandbox with its own file system and a Python runtime. Your request sets it up with the container field of the tool. With {"type": "auto"}, the provider creates a container for the request, or reuses the one from the earlier turn of the same conversation.
State lives in the container, not in the model. Variables and files written in one code call are available to the next call in the same container. Containers also expire after a period of inactivity, and their contents are then lost. Check your provider's documentation for the current lifetime and billing.
Giving the sandbox input files
Upload the file with the Files API, then list its ID in the container definition.
from openai import OpenAI
client = OpenAI()
with open("sales.csv", "rb") as f:
uploaded = client.files.create(file=f, purpose="user_data")
response = client.responses.create(
model="gpt-4.1",
tools=[
{
"type": "code_interpreter",
"container": {"type": "auto", "file_ids": [uploaded.id]},
}
],
instructions="Use code to analyze the attached file. Save any chart as a PNG.",
input="Plot monthly revenue from sales.csv.",
)
print(response.output_text)The file is copied into the container, so the model's code can open it like any local file.
Finding generated files
When code writes a file and the answer mentions it, the message text carries a container_file_citation annotation. It holds the container_id, the file_id, and the filename. Collect these from the annotations.
for item in response.output:
if item.type == "message":
for part in item.content:
for ann in getattr(part, "annotations", []) or []:
if ann.type == "container_file_citation":
data = client.containers.files.content.retrieve(
ann.file_id, container_id=ann.container_id
)
with open(ann.filename, "wb") as out:
out.write(data.read())
print("saved", ann.filename)Common errors
- Expired container. Downloading after the container has expired fails. Download files soon after the response arrives, and store anything you need to keep.
- No file annotation. The model must write the file and refer to it in its answer. If a file is missing, say in
instructionsthat results must be saved to a file. - Assuming persistence. A new, unrelated request gets a fresh container unless you pass the earlier container's ID.
Exercise
A response says it saved chart.png, but your download call fails an hour later. What is the most likely cause, and how do you avoid it?
Show answer
The container expired, so its files were deleted. Download generated files right after the response arrives (using the containerid and fileid from the containerfilecitation annotation) and store them in your own storage.