openai/codex · error · RuntimeError
AsyncCodex is not initialized yet. Prefer `async with AsyncC
Error message
AsyncCodex is not initialized yet. Prefer `async with AsyncCodex()`; initialization also happens on first awaited API use.
What it means
Error "AsyncCodex is not initialized yet. Prefer `async with AsyncCodex()`; initialization also happens on first awaited API use." thrown in openai/codex.
Source
Thrown at sdk/python/src/openai_codex/api.py:330
return
async with self._init_lock:
if self._initialized:
return
try:
await self._client.start()
payload = await self._client.initialize()
self._init = validate_initialize_metadata(payload)
self._initialized = True
except Exception:
await self._client.close()
self._init = None
self._initialized = False
raise
@property
def metadata(self) -> InitializeResponse:
if self._init is None:
raise RuntimeError(
"AsyncCodex is not initialized yet. Prefer `async with AsyncCodex()`; "
"initialization also happens on first awaited API use."
)
return self._init
async def close(self) -> None:
await self._client.close()
self._init = None
self._initialized = False
async def login_api_key(self, api_key: str) -> None:
"""Authenticate Codex with an API key."""
await self._ensure_initialized()
await self._client.account_login_start(
LoginAccountParams(
root=ApiKeyLoginAccountParams(
api_key=api_key,
type="apiKey",View on GitHub (pinned to 339751715c)
Solutions
- Use async with AsyncCodex() or await an API call so initialization completes before this operation.
When it happens
Trigger: Thrown at sdk/python/src/openai_codex/api.py:330 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/a53a131703a39be0.
Report an issue: GitHub.