Skip to content

Turn

Base classes

Name Children Inherits
CodexBaseModel
codexed.models.base
Base model for all Codex API models.

⋔ Inheritance diagram

graph TD
  1940435775["misc.Turn"]
  3116041069["base.CodexBaseModel"]
  3879467795["main.BaseModel"]
  2751680898["builtins.object"]
  3116041069 --> 1940435775
  3879467795 --> 3116041069
  2751680898 --> 3879467795

🛈 DocStrings

Bases: CodexBaseModel

Turn data structure.

Source code in src/codexed/models/misc.py
class Turn(CodexBaseModel):
    """Turn data structure."""

    id: str

    completed_at: int | None = None
    """
    Unix timestamp (in seconds) when the turn completed.
    """
    duration_ms: int | None = None
    """
    Duration between turn start and completion in milliseconds, if known.
    """
    error: TurnError | None = None
    """
    Only populated when the Turn's status is failed.
    """
    items: list[ThreadItem]
    """
    Only populated on a `thread/resume` or `thread/fork` response.

    For all other responses and notifications returning a Turn,
    the items field will be an empty list.
    """
    started_at: int | None = None
    """
    Unix timestamp (in seconds) when the turn started.
    """
    status: TurnStatus

    @property
    def final_response(self) -> str | None:
        """Extract the final assistant response text from this turn.

        Looks for the last ``ThreadItemAgentMessage`` with ``phase="final_answer"``.
        Falls back to the last agent message with no phase (for models that don't
        emit phase metadata). Returns None if the turn has no agent messages.

        For structured output turns, this returns the raw JSON string.
        """
        last_unphased: str | None = None
        for item in reversed(self.items):
            match item:
                case ThreadItemAgentMessage(phase="final_answer", text=text):
                    return text
                case ThreadItemAgentMessage(phase=None, text=text) if last_unphased is None:
                    last_unphased = text
        return last_unphased

    items_view: TurnItemsView | None = None
    """How completely the turn's items are included in this payload."""
    root_turn_id: str | None = None

completed_at class-attribute instance-attribute

completed_at: int | None = None

Unix timestamp (in seconds) when the turn completed.

duration_ms class-attribute instance-attribute

duration_ms: int | None = None

Duration between turn start and completion in milliseconds, if known.

error class-attribute instance-attribute

error: TurnError | None = None

Only populated when the Turn's status is failed.

final_response property

final_response: str | None

Extract the final assistant response text from this turn.

Looks for the last ThreadItemAgentMessage with phase="final_answer". Falls back to the last agent message with no phase (for models that don't emit phase metadata). Returns None if the turn has no agent messages.

For structured output turns, this returns the raw JSON string.

items instance-attribute

items: list[ThreadItem]

Only populated on a thread/resume or thread/fork response.

For all other responses and notifications returning a Turn, the items field will be an empty list.

items_view class-attribute instance-attribute

items_view: TurnItemsView | None = None

How completely the turn's items are included in this payload.

started_at class-attribute instance-attribute

started_at: int | None = None

Unix timestamp (in seconds) when the turn started.

:fa-brands-github: Show source on GitHub