openai/codex · error · RuntimeError

turn failed with status

Error message

turn failed with status {turn.status.value}

What it means

_raise_for_failed_turn re-raises with this message (including the turn's status value) when the turn's own error message is absent — it wraps the failed turn status into the exception so callers see why the turn ended.

Solutions

  1. Inspect the turn status and its error details, then retry the turn after addressing the reported failure.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at sdk/python/src/openai_codex/_run.py:65 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of openai/codex@339751715c (2026-08-25). Data as JSON: /api/errors/b06220246e3aaa88. Report an issue: GitHub.

Appendix: source

Thrown at sdk/python/src/openai_codex/_run.py:65

    for item in reversed(items):
        agent_message = _agent_message_item_from_thread_item(item)
        if agent_message is None:
            continue
        if agent_message.phase == MessagePhase.final_answer:
            return agent_message.text
        if agent_message.phase is None and last_unknown_phase_response is None:
            last_unknown_phase_response = agent_message.text

    return last_unknown_phase_response


def _raise_for_failed_turn(turn: Turn) -> None:
    if turn.status != TurnStatus.failed:
        return
    if turn.error is not None and turn.error.message:
        raise RuntimeError(turn.error.message)
    raise RuntimeError(f"turn failed with status {turn.status.value}")


def _collect_turn_result(stream: Iterator[Notification], *, turn_id: str) -> TurnResult:
    completed: TurnCompletedNotification | None = None
    items: list[ThreadItem] = []
    usage: ThreadTokenUsage | None = None

    for event in stream:
        payload = event.payload
        if isinstance(payload, ItemCompletedNotification) and payload.turn_id == turn_id:
            items.append(payload.item)
            continue
        if isinstance(payload, ThreadTokenUsageUpdatedNotification) and payload.turn_id == turn_id:
            usage = payload.token_usage
            continue
        if isinstance(payload, TurnCompletedNotification) and payload.turn.id == turn_id:
            completed = payload

View on GitHub (pinned to 339751715c)