openai-agents-python/examples/codex/conversation.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

44 lines
1.5 KiB
Python

"""Continue and restore a native conversation after its work settles.
Run `python -m examples.codex.conversation` from the repository root with
the matching SDK and OPENAI_API_KEY. The answer before and after restore
should recall atlas. All three turns make live requests and can incur charges.
"""
import asyncio
from contextlib import suppress
from agents import Agent, Turn
from agents.observation import OutputTextDelta
async def print_text(turn: Turn[str]) -> None:
async for event in turn.events():
if isinstance(event, OutputTextDelta):
print(event.delta, end="", flush=True)
async def main() -> None:
agent = Agent(instructions="Answer briefly and remember this conversation.")
async with agent.session() as session:
first = await session.submit("Remember the label atlas.")
await first.get_result()
second = await session.submit("What label did I give you?")
observer = asyncio.create_task(print_text(second))
try:
result = await second.get_result()
finally:
observer.cancel()
with suppress(asyncio.CancelledError):
await observer
print("\nFinal answer:", result.final_output)
state = await session.save_state()
async with agent.session(state=state) as restored:
third = await restored.submit("Repeat that label once more.")
result = await third.get_result()
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())