#!/usr/bin/env python3 """apply_patch: the model emits a structured create_file operation; we validate the path and report (dry run, nothing written). STATUS: LIVE_VERIFIED 2026-09-18 (gpt-5.4-nano; apply_patch_call.operation = {type:create_file, path:'hello.txt', diff:'+OK\\n'}). Run: .venv/bin/python examples/openai/tools/apply-patch/apply_patch.py """ import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[4]; sys.path.insert(0, str(ROOT)) from scripts.live import load_env, log_request; load_env() from openai import OpenAI client = OpenAI(); MODEL = "gpt-5.4-nano" r = client.responses.create(model=MODEL, input="Create a new file hello.txt containing the single line OK using apply_patch.", tools=[{"type": "apply_patch"}], tool_choice={"type": "apply_patch"}, max_output_tokens=128, reasoning={"effort": "low"}) log_request("openai", "POST", "/v1/responses", 200, 0.0002, "apply_patch.py create_file") call = next(i for i in r.output if i.type == "apply_patch_call") op = call.operation print("apply_patch_call:", call.call_id, op.type, op.path, repr(getattr(op, "diff", None))) ok = ".." not in op.path and not op.path.startswith("/") output_item = {"type": "apply_patch_call_output", "call_id": call.call_id, "status": "completed" if ok else "failed", "output": "dry run: not applied" if ok else "path rejected"} print("would send:", output_item)