openai-agents-python/examples/codex/completion.py
Mark Stuart ba3fbbf400
docs: organize SDK guides around developer tasks (#5920)
* docs: organize SDK guides and clarify examples

* fix: correct native SDK type declarations

* test: update native SDK contract fixtures

* fix: normalize persisted agent profile transport settings

* test: reconcile inherited model settings API snapshot

* test: preserve executor coverage on Windows

---------

Co-authored-by: markstuart-oai <323302876+markstuart-oai@users.noreply.github.com>
2026-11-12 21:00:58 -08:00

58 lines
1.6 KiB
Python

"""Require a saved report before accepting the agent's final answer.
Run `python -m examples.codex.completion` from the repository root with
the matching SDK and OPENAI_API_KEY. It prints the answer and saved report,
then removes the temporary report. Live requests can incur charges.
"""
from dataclasses import dataclass
from pathlib import Path
from tempfile import TemporaryDirectory
from agents import (
Agent,
CompletionContext,
ContinueTurn,
RunBudget,
ToolContext,
function_tool,
)
@dataclass
class Report:
path: Path
@function_tool
def save_report(ctx: ToolContext[Report], text: str) -> str:
"""Save the completed report."""
ctx.context.path.write_text(text, encoding="utf-8")
return "Report saved."
def require_report(request: CompletionContext[Report]) -> ContinueTurn | None:
if not request.context.path.is_file():
return ContinueTurn("Save the report with save_report before finishing.")
return None
def main() -> None:
agent = Agent(
instructions="Write a short project report and save it with save_report.",
tools=[save_report],
before_complete=require_report,
)
with TemporaryDirectory() as directory:
report = Report(Path(directory) / "report.md")
result = agent.run_sync(
"The prototype is complete. The deployment date is still undecided.",
context=report,
budget=RunBudget(max_requests=5),
)
print(result.final_output)
print(report.path.read_text(encoding="utf-8"))
if __name__ == "__main__":
main()