Skip to content

Result

ConversationState

Bases: dict[str, Any]

Conversation history through a completed or interrupted turn, ready to resume.

Source code in src/openai_agents/codex/result.py
class ConversationState(dict[str, Any]):
    """Conversation history through a completed or interrupted turn, ready to resume."""

    @property
    def thread_id(self) -> str:
        return cast(str, self["thread_id"])

    @property
    def session_id(self) -> str:
        return cast(str, self.get("session_id", self.thread_id))

    @property
    def codex_revision(self) -> str | None:
        """Codex commit that produced this snapshot, or None for older snapshots."""
        return self.get("codex_revision")

    def to_json(self) -> str:
        """Serialize the snapshot for storage or transfer."""
        return json.dumps(self, allow_nan=False)

    @classmethod
    def from_json(cls, data: str | bytes) -> "ConversationState":
        """Load a snapshot. The engine validates the history when resuming."""
        state = json.loads(data)
        if (
            not isinstance(state, dict)
            or not isinstance(state.get("thread_id"), str)
            or ("session_id" in state and not isinstance(state["session_id"], str))
            or type(state.get("version")) is not int
            or not isinstance(state.get("items"), list)
            or ("codex_revision" in state and not isinstance(state["codex_revision"], str))
        ):
            raise ValueError("Invalid conversation state")
        return cls(state)

codex_revision property

codex_revision: str | None

Codex commit that produced this snapshot, or None for older snapshots.

to_json

to_json() -> str

Serialize the snapshot for storage or transfer.

Source code in src/openai_agents/codex/result.py
def to_json(self) -> str:
    """Serialize the snapshot for storage or transfer."""
    return json.dumps(self, allow_nan=False)

from_json classmethod

from_json(data: str | bytes) -> ConversationState

Load a snapshot. The engine validates the history when resuming.

Source code in src/openai_agents/codex/result.py
@classmethod
def from_json(cls, data: str | bytes) -> "ConversationState":
    """Load a snapshot. The engine validates the history when resuming."""
    state = json.loads(data)
    if (
        not isinstance(state, dict)
        or not isinstance(state.get("thread_id"), str)
        or ("session_id" in state and not isinstance(state["session_id"], str))
        or type(state.get("version")) is not int
        or not isinstance(state.get("items"), list)
        or ("codex_revision" in state and not isinstance(state["codex_revision"], str))
    ):
        raise ValueError("Invalid conversation state")
    return cls(state)

RunResult dataclass

Output of one completed turn.

Source code in src/openai_agents/codex/result.py
@dataclass(kw_only=True)
class RunResult:
    """Output of one completed turn."""

    api_type: ClassVar[str] = "agent.session.turn.completed"

    turn_id: str
    final_output: str | None
    """The final assistant message, if the turn produced one."""

    new_items: list[RunItem]
    """Completed items from this turn, in stream order."""

    usage: Usage

    state: ConversationState = field(repr=False)
    """Snapshot through the end of this turn, ready to pass to another run."""

    type: Literal["result"] = field(default="result", init=False)

    @property
    def session_id(self) -> str:
        return self.state.session_id

    @property
    def output_text(self) -> str | None:
        return self.final_output

    @property
    def items(self) -> list[RunItem]:
        return self.new_items

final_output instance-attribute

final_output: str | None

The final assistant message, if the turn produced one.

new_items instance-attribute

new_items: list[RunItem]

Completed items from this turn, in stream order.

state class-attribute instance-attribute

state: ConversationState = field(repr=False)

Snapshot through the end of this turn, ready to pass to another run.