openai-agents-python/examples/sandbox/tutorials/dataroom_metric_extract/main.py
Steve Coffey 2d665c9a67
Sandbox Agents (#2889)
### Sandbox Agents

This release adds **Sandbox Agents**, a beta SDK surface for running
agents with a persistent, isolated workspace. Sandbox agents keep the
normal `Agent` and `Runner` flow, but add workspace manifests,
sandbox-native capabilities, sandbox clients, snapshots, and resume
support so agents can work over real files, run commands, edit
repositories, generate artifacts, and continue work across runs.

Key pieces:

- `SandboxAgent`: an `Agent` with sandbox defaults such as
`default_manifest`, sandbox instructions, capabilities, and `run_as`.
- `Manifest`: a fresh-workspace contract for files, directories, local
files, local directories, Git repos, environment, users, groups, and
mounts.
- `SandboxRunConfig`: per-run sandbox wiring for client creation, live
session injection, serialized session resume, manifest overrides,
snapshots, and materialization concurrency limits.
- Built-in capabilities for shell access, filesystem editing and image
inspection, skills, memory, and compaction.
- Workspace snapshots and serialized sandbox session state for
reconnecting to existing work or seeding a fresh sandbox from saved
contents.

### Sandbox clients and hosted providers

Sandbox agents now support local, containerized, and hosted execution
backends:

- `UnixLocalSandboxClient` for fast local development.
- `DockerSandboxClient` for container isolation and image parity.
- Hosted sandbox clients for Blaxel, Cloudflare, Daytona, E2B, Modal,
Runloop, and Vercel through optional extras.

The release also adds provider-specific examples and mount strategies
for common storage backends, including S3, Cloudflare R2, Google Cloud
Storage, Azure Blob Storage, and S3 Files where supported by the
selected backend.

### Sandbox memory

Adds a sandbox memory capability that lets future sandbox-agent runs
learn from prior runs. Memory stores extracted lessons in the sandbox
workspace, injects a concise summary into later runs, and uses
progressive disclosure so agents can search deeper rollout summaries
only when useful.

Memory supports:

- Read-only or generate-only modes.
- Live updates when the agent discovers stale memory.
- Multi-turn grouping through `conversation_id`, SDK `Session`,
`RunConfig.group_id`, or generated run IDs.
- Separate memory layouts for isolating memory across agents or
workflows.
- S3-backed examples for persisted memory across runs.

### Workspace mounts, snapshots, and resume

This release adds a full workspace entry and mount model for sandbox
sessions:

- Local files and directories.
- Synthetic files and directories.
- Git repository entries.
- Remote storage mounts for S3, R2, GCS, Azure Blob Storage, and S3
Files.
- Provider-specific mount strategies across Docker, Modal, Cloudflare,
Blaxel, Daytona, E2B, and Runloop.
- Portable snapshots with path normalization, symlink preservation,
mount-safe snapshotting, and remote snapshot support.
- Resume paths through runner-managed `RunState`, explicit
`SandboxSessionState`, or saved snapshots.

### Examples and tutorials

Adds a large `examples/sandbox/` suite covering:

- Local Unix and Docker sandbox runners.
- Docker mount smoke tests for S3, GCS, Azure Blob Storage, and S3
Files.
- Sandbox coding tasks with skills.
- Sandbox agents as tools and handoff patterns.
- Memory examples, including multi-agent/multi-turn memory and S3-backed
memory.
- Tax-prep and healthcare-support workflows.
- Dataroom QA and metric extraction tutorials.
- Repository code review tutorial.
- Vision website clone tutorial.
- Provider examples for Blaxel, Cloudflare, Daytona, E2B, Modal,
Runloop, Temporal, and Vercel.

### Runtime, tracing, and model plumbing

The release includes the runtime plumbing needed to make sandbox agents
work naturally inside the existing SDK:

- Runner-managed sandbox preparation, capability binding, session
lifecycle, state serialization, and resume behavior.
- Sandbox-aware `RunState` serialization.
- Unified sandbox tracing with SDK spans.
- Token usage on tracing spans.
- Runner-managed prompt cache key defaults.
- OpenAI agent registration and harness ID configuration.
- Safer redaction of sensitive MCP tool outputs when sensitive tracing
is disabled.
- Additional OpenAI client/model utilities and Chat Completions
coverage.


## Documentation & Other Changes

- docs: add Asqav to external tracing processors list.
- docs: update translated document pages.

Co-authored-by: Abdulrahman Alfozan <alfozan@openai.com>
Co-authored-by: Aditya Singh <60082699+adityasingh2400@users.noreply.github.com>
Co-authored-by: Andi Liu <andi@openai.com>
Co-authored-by: Aron <263346377+aron-cf@users.noreply.github.com>
Co-authored-by: ashwinnathan-openai <ashwinnathan@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: cploujoux <cploujoux@blaxel.ai>
Co-authored-by: elainegan-openai <168589666+elainegan-openai@users.noreply.github.com>
Co-authored-by: Elias Freider <freider@users.noreply.github.com>
Co-authored-by: Erik Dunteman <erik@erikds-macbook-air.local>
Co-authored-by: Jason Liu <jasonliu@openai.com>
Co-authored-by: Jason Steving <32336750+jasonsteving99@users.noreply.github.com>
Co-authored-by: Kazuhiro Sera <seratch@openai.com>
Co-authored-by: Lovre Pešut <lovre.pesut@gmail.com>
Co-authored-by: Lucas Wang <lucas_wang@lucas-futures.com>
Co-authored-by: Matt Brockman <matt.brockman@e2b.dev>
Co-authored-by: Mish Ushakov <mishushakov@users.noreply.github.com>
Co-authored-by: Naresh <ghostwriternr@gmail.com>
Co-authored-by: nicholasclark-openai <nicholasclark@openai.com>
Co-authored-by: qiyaoq-oai <qiyaoq@openai.com>
Co-authored-by: Scott Trinh <scott@scotttrinh.com>
Co-authored-by: tode-rl <tony@runloop.ai>
Co-authored-by: Wendy Jiao <wendyjiao@openai.com>
2026-04-15 10:00:40 -07:00

274 lines
9.4 KiB
Python

"""
Extract structured financial metrics from a synthetic 10-K dataroom and write a
JSONL or CSV artifact.
"""
import argparse
import asyncio
import csv
import json
import sys
from collections.abc import Sequence
from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING, Literal, cast
from openai.types.shared.reasoning import Reasoning
from pydantic import BaseModel
from agents import ModelSettings, Runner, RunResultStreaming, TResponseInputItem
from agents.run import RunConfig
from agents.sandbox import Manifest, SandboxAgent, SandboxRunConfig
from agents.sandbox.capabilities import Shell
from agents.sandbox.entries import File, LocalDir
if __package__ is None or __package__ == "":
sys.path.insert(0, str(Path(__file__).resolve().parent))
sys.path.insert(0, str(Path(__file__).resolve().parents[4]))
if TYPE_CHECKING or __package__:
from .schemas import FinancialMetric, FinancialMetricBatch
else:
from schemas import FinancialMetric, FinancialMetricBatch
from examples.sandbox.tutorials.misc import (
DEFAULT_SANDBOX_IMAGE,
console,
create_sandbox_client_and_session,
load_env_defaults,
print_event,
run_interactive_loop,
)
DEMO_DIR = Path(__file__).resolve().parent
DATAROOM_DATA_DIR = DEMO_DIR.parent / "data" / "dataroom"
DEFAULT_QUESTION = (
"Extract revenue, gross margin, operating income, cash flow, balance-sheet, segment, "
"and geography metrics from the 10-K packet into one row per metric-period-source. "
"For each table, include every explicit line item in the source, even when it is "
"similar to a line item in another source."
)
AGENTS_MD = dedent(
"""\
# AGENTS.md
Extract structured financial metrics from the synthetic 10-K packet under `data/`.
## Output (one row per metric-value occurrence)
Required fields: `source_file`, `filing_section`, `metric_name`, `fiscal_period`, `value`,
`unit` (`USD millions` or `percent`).
Optional field: `segment` (segment/geography if explicitly stated, else null).
## Rules
- Review all `.txt` and `.pdf` under `data/` (these PDFs contain searchable text).
- Use shell tools (`rg`, `sed`) for discovery/inspection; do not run Python from the sandbox shell.
- Do not read `data/setup.py`.
- Emit a separate row for each metric-period pair in each source file (do not dedupe across files).
- For tables, include every explicit table line item in that source. For example, the
statements-of-operations PDF has separate Net revenue, Gross profit, and Operating income rows.
- Only extract explicit source line items / table rows. Do not invent rollups or “cleaned up” metrics.
- Do not treat Gross profit and Gross margin as duplicates; they are distinct source metrics.
- Preserve labels as written (e.g., `Revenue` vs `Net revenue`).
## Completeness checklist
Before final output, verify the batch has exactly 41 rows from these source-level line items:
- `data/10-k-mdna-overview.txt`: Revenue, Gross margin, and Operating income for FY2025 and FY2024.
- `data/10-k-mdna-liquidity.txt`: Net cash provided by operating activities, Capital expenditures,
and Free cash flow for FY2025 and FY2024.
- `data/10-k-note-segments.txt`: Platform segment revenue and Services segment revenue for FY2025
and FY2024, with the matching segment names.
- `data/10-k-note-geography.txt`: Americas revenue, EMEA revenue, and APAC revenue for FY2025, with
the matching geography names as segments.
- `data/10-k-note-balance-sheet.txt`: Cash and cash equivalents and Deferred revenue for 2025-12-31
and 2024-12-31.
- `data/10-k-statements-of-operations.pdf`: Net revenue, Gross profit, and Operating income for
FY2025 and FY2024.
- `data/10-k-balance-sheets.pdf`: Cash and cash equivalents, Accounts receivable, and Deferred revenue
for 2025-12-31 and 2024-12-31.
- `data/10-k-statements-of-cash-flows.pdf`: Net cash provided by operating activities, Capital
expenditures, and Free cash flow for FY2025 and FY2024.
Return the structured rows directly in your final output.
"""
)
async def print_streamed_result(result: RunResultStreaming) -> BaseModel:
async for event in result.stream_events():
print_event(event)
if result.final_output is None:
raise RuntimeError("10-K Metric Extractor returned no structured metric output.")
print_event(str(result.final_output).strip())
return cast(BaseModel, result.final_output)
def write_jsonl(path: Path, metrics: Sequence[BaseModel]) -> None:
path.write_text(
"\n".join(metric.model_dump_json() for metric in metrics) + "\n",
encoding="utf-8",
)
def write_csv(path: Path, metrics: list[FinancialMetric]) -> None:
with path.open("w", encoding="utf-8", newline="") as output_file:
writer = csv.DictWriter(
output_file,
fieldnames=[
"source_file",
"filing_section",
"metric_name",
"fiscal_period",
"value",
"unit",
"segment",
],
)
writer.writeheader()
for metric in metrics:
writer.writerow(json.loads(metric.model_dump_json()))
def write_final_artifact(
output_dir: Path,
output_format: Literal["jsonl", "csv"],
metrics: list[FinancialMetric],
) -> Path:
output_path = output_dir / f"financial_metrics.{output_format}"
if output_format == "jsonl":
write_jsonl(output_path, metrics)
else:
write_csv(output_path, metrics)
return output_path
async def main(
model: str,
question: str,
output_format: Literal["jsonl", "csv"],
use_docker: bool,
image: str,
no_interactive: bool,
) -> None:
if not (DATAROOM_DATA_DIR / "10-k-mdna-overview.txt").exists():
raise SystemExit(
"Run `uv run python examples/sandbox/tutorials/data/dataroom/setup.py` "
"before starting this demo."
)
manifest = Manifest(
entries={
"AGENTS.md": File(content=AGENTS_MD.encode("utf-8")),
"data": LocalDir(src=DATAROOM_DATA_DIR),
}
)
agent = SandboxAgent(
name="10-K Metric Extractor",
model=model,
instructions=AGENTS_MD,
capabilities=[Shell()],
model_settings=ModelSettings(
reasoning=Reasoning(effort="high"),
tool_choice="required",
),
output_type=FinancialMetricBatch,
)
client, sandbox = await create_sandbox_client_and_session(
manifest=manifest,
use_docker=use_docker,
image=image,
)
try:
async with sandbox:
extracted_metrics: FinancialMetricBatch | None = None
async def run_turn(
conversation: list[TResponseInputItem],
) -> list[TResponseInputItem]:
nonlocal extracted_metrics
result = Runner.run_streamed(
agent,
conversation,
max_turns=25,
run_config=RunConfig(
sandbox=SandboxRunConfig(session=sandbox),
tracing_disabled=True,
workflow_name="Dataroom extraction example",
),
)
extracted_metrics = cast(FinancialMetricBatch, await print_streamed_result(result))
return result.to_input_list()
conversation: list[TResponseInputItem] = [{"role": "user", "content": question}]
conversation = await run_turn(conversation)
await run_interactive_loop(
conversation=conversation,
no_interactive=no_interactive,
run_turn=run_turn,
)
finally:
await client.delete(sandbox)
if extracted_metrics is None:
raise RuntimeError("10-K Metric Extractor returned no structured metric output.")
output_dir = DEMO_DIR / "output"
output_dir.mkdir(exist_ok=True)
artifact_path = write_final_artifact(output_dir, output_format, extracted_metrics.metrics)
console.print(
f"[green]Wrote {len(extracted_metrics.metrics)} metric row(s) to {artifact_path}[/green]"
)
if __name__ == "__main__":
load_env_defaults(DEMO_DIR / ".env")
parser = argparse.ArgumentParser()
parser.add_argument(
"--model",
default="gpt-5.4-mini",
help="Model name to use.",
)
parser.add_argument(
"--question",
default=DEFAULT_QUESTION,
help="Prompt to send to the agent.",
)
parser.add_argument(
"--output-format",
choices=("jsonl", "csv"),
default="csv",
help="Artifact format.",
)
parser.add_argument(
"--docker",
action="store_true",
help="Run this example in Docker instead of Unix-local.",
)
parser.add_argument(
"--image",
default=DEFAULT_SANDBOX_IMAGE,
help="Docker image to use when --docker is set.",
)
parser.add_argument(
"--no-interactive",
action="store_true",
help="Run the scripted turn and skip follow-up terminal input.",
)
args = parser.parse_args()
asyncio.run(
main(
args.model,
args.question,
args.output_format,
args.docker,
args.image,
args.no_interactive,
)
)