Use environments and artifacts
To give an agent a file to work on, create a session with an OpenAI-hosted environment. The environment gives the agent a workspace where it can read and edit files and run commands.
To get a file back, ask the agent to write it under /workspace/outputs. When the turn (one round of work) completes, OpenAI publishes a downloadable copy called an artifact. That copy stays the same even if the agent edits the working file later.
Workspace files and published copies
Use the environment ID for files in the live workspace. You can supply initial files in environment.files when you create the session. To upload a file later, wait until the environment reports connected. If the supplied files are all the agent needs, you can disable network access.
Use the session ID to tell the agent what to do with the files and to find the artifacts it publishes. A later turn can overwrite a workspace file. Each published copy has its own artifact ID and identifies the turn that produced it.
Example: Report whether a payment export passed its test
Herringbone uses two IDs from the session creation response to get the log to the agent.
- With
environment.id(env_a), Herringbone uploads the log to/workspace/test-results.txtonce the workspace is ready. - With the session
id(sess_a), Herringbone asks the agent to read that path and write a summary to/workspace/outputs/result.json.
The agent reads the log and writes “All three payments appeared once in the export” in the report’s summary field. Once the turn completes, Herringbone can retrieve the agent’s reply and the published report, even after the workspace expires.
The log lists the payment IDs on each page and the IDs the job exported. Here, environments is OpenAI().beta.agents.environments. Once the environment reports connected, Herringbone encodes the log as a base64 string and uploads it to /workspace/test-results.txt.
def upload_log(environments, environment_id, log):
deadline = time.monotonic() + 120
while True:
status = environments.retrieve(environment_id).status
if status == "connected":
break
if status not in {"pending", "ready"}:
raise RuntimeError(f"Workspace is {status}")
if time.monotonic() >= deadline:
raise TimeoutError("Workspace did not connect in time.")
time.sleep(2)
return environments.files.create(
environment_id,
path="/workspace/test-results.txt",
type="inline",
data=base64.b64encode(log).decode("ascii"),
)Here, sessions is OpenAI().beta.agents.sessions. Herringbone sends the uploaded file’s path and the destination for the report in a session message. The completion event supplies the turn ID for retrieving the published copy.
def request_report(sessions, session_id, log_path, report_path):
task = f"""Read Customer A's nightly export test log at {log_path}.
Write a JSON object to {report_path}. In its summary string, state
how many test payments appeared exactly once and whether any were
missing or duplicated. Include the full report path in your reply."""
message = {
"type": "agent.session.input.message",
"input": [{"role": "user", "content": [{"type": "input_text", "text": task}]}],
}
with sessions.events.stream(session_id) as events:
sessions.events.create(
session_id, events=[message], idempotency_key=str(uuid4())
)
for event in events:
if event.type == "agent.session.turn.completed":
return event.turn.id
if event.type in {
"agent.session.turn.failed",
"agent.session.turn.cancelled",
"agent.session.failed",
"error",
}:
raise RuntimeError(event.type)
raise RuntimeError("The stream closed before the turn finished.")Set OPENAI_API_KEY and copy the program into publish-and-retrieve-report.py. With the Python package manager uv, run uv run --with openai==3.57.0 python publish-and-retrieve-report.py.
The program creates a temporary session with an OpenAI-hosted workspace and uploads a sample test log. It prints the assistant’s reply and the report’s summary, then deletes the session and its artifacts.
import base64
import json
import time
from uuid import uuid4
from openai import OpenAI
def upload_log(environments, environment_id, log):
deadline = time.monotonic() + 120
while True:
status = environments.retrieve(environment_id).status
if status == "connected":
break
if status not in {"pending", "ready"}:
raise RuntimeError(f"Workspace is {status}")
if time.monotonic() >= deadline:
raise TimeoutError("Workspace did not connect in time.")
time.sleep(2)
return environments.files.create(
environment_id,
path="/workspace/test-results.txt",
type="inline",
data=base64.b64encode(log).decode("ascii"),
)
def request_report(sessions, session_id, log_path, report_path):
task = f"""Read Customer A's nightly export test log at {log_path}.
Write a JSON object to {report_path}. In its summary string, state
how many test payments appeared exactly once and whether any were
missing or duplicated. Include the full report path in your reply."""
message = {
"type": "agent.session.input.message",
"input": [{"role": "user", "content": [{"type": "input_text", "text": task}]}],
}
with sessions.events.stream(session_id) as events:
sessions.events.create(
session_id, events=[message], idempotency_key=str(uuid4())
)
for event in events:
if event.type == "agent.session.turn.completed":
return event.turn.id
if event.type in {
"agent.session.turn.failed",
"agent.session.turn.cancelled",
"agent.session.failed",
"error",
}:
raise RuntimeError(event.type)
raise RuntimeError("The stream closed before the turn finished.")
def read_reply(sessions, session_id, turn_id):
turn = sessions.turns.retrieve(turn_id, session_id=session_id)
if turn.status != "completed":
raise RuntimeError(f"Turn is {turn.status}")
items = sessions.turns.items.list(turn_id, session_id=session_id, order="desc")
for item in items:
if item.type == "message" and item.role == "assistant":
if item.phase != "commentary" and item.output_text:
return item.output_text
raise ValueError("No final reply in this turn.")
def read_report(sessions, session_id, turn_id, report_path):
for artifact in sessions.artifacts.list(session_id):
if artifact.path == report_path and artifact.turn_id == turn_id:
file = sessions.artifacts.content(artifact.id, session_id=session_id)
return json.loads(file.read())["summary"]
raise FileNotFoundError(f"No {report_path} for turn {turn_id}")
def main():
agents = OpenAI().beta.agents
session = agents.sessions.create(
agent={
"model": "gpt-6.2-astra",
"instructions": """You are a forward-deployed engineer for Herringbone.
Base each finding on the customer's test log. Never claim a fix is live
on the strength of a test alone.""",
},
environment={"type": "openai_hosted", "network": {"access": "disabled"}},
)
try:
# Sample log from a test of the change proposed for Customer A.
log = b"""Expected on page 1: pay_1 pay_2
Expected on page 2: pay_3
Exported: pay_1 pay_2 pay_3"""
uploaded = upload_log(agents.environments, session.environment.id, log)
path = "/workspace/outputs/result.json"
turn_id = request_report(agents.sessions, session.id, uploaded.path, path)
print(read_reply(agents.sessions, session.id, turn_id))
print(read_report(agents.sessions, session.id, turn_id, path))
finally:
agents.sessions.delete(session.id)
if __name__ == "__main__":
main()