{"record":{"id":"33bf4a8a4abc6e10","repo":"langchain-ai/deepagents","slug":"failed-to-initialize-codex-model-provider-mode","errorCode":null,"errorMessage":"Failed to initialize Codex model '{provider}:{model_name}': {exc}","messagePattern":"Failed to initialize Codex model '(.+?):(.+?)': (.+?)","errorType":"exception","errorClass":"ModelConfigError","httpStatus":null,"severity":"error","filePath":"libs/code/deepagents_code/config.py","lineNumber":5915,"sourceCode":"            msg = (\n                \"Not signed in to ChatGPT. Run `/auth` and select \"\n                \"openai_codex to sign in with your ChatGPT account.\"\n            )\n            raise MissingCredentialsError(msg, provider=provider, env_var=None) from exc\n        except _codex.CodexAuthExpiredError as exc:\n            # A token exists but its refresh token is dead. Route through the\n            # same `MissingCredentialsError` recovery path as a missing token\n            # (which the retry flow re-attempts after `/auth`) instead of the\n            # generic `ModelConfigError` below, which would not offer sign-in.\n            msg = (\n                \"ChatGPT session expired. Run `/auth` and select openai_codex \"\n                \"to sign in again.\"\n            )\n            raise MissingCredentialsError(msg, provider=provider, env_var=None) from exc\n        except Exception as exc:\n            spec = f\"{provider}:{model_name}\"\n            msg = f\"Failed to initialize Codex model '{spec}': {exc}\"\n            raise ModelConfigError(msg) from exc\n    elif class_path:\n        model = _create_model_from_class(class_path, model_name, provider, kwargs)\n    else:\n        model = _create_model_via_init(model_name, provider, kwargs)\n\n    resolved_provider = provider or getattr(model, \"_model_provider\", provider)\n    from deepagents_code.cost_tracking import _set_configured_model_metadata\n\n    _set_configured_model_metadata(model, model_name, resolved_provider)\n\n    # Apply profile overrides from config.toml (e.g., max_input_tokens)\n    if provider:\n        config_profile_overrides = config.get_profile_overrides(\n            provider, model_name=model_name\n        )\n        if config_profile_overrides:\n            _apply_profile_overrides(\n                model,","sourceCodeStart":5897,"sourceCodeEnd":5933,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/code/deepagents_code/config.py#L5897-L5933","documentation":"Catch-all translation: any exception from `_codex.build_chat_model` that is not a missing token (FileNotFoundError) or expired auth (CodexAuthExpiredError) is wrapped in ModelConfigError as `Failed to initialize Codex model '<provider>:<model>'`. The original exception is chained, so the root cause is in the traceback.","triggerScenarios":"Constructing the Codex chat model when build_chat_model fails for other reasons: malformed or corrupted auth file (present but unparseable JSON), missing `codex` CLI binary the integration shells out to, network failure during token refresh that isn't classified as auth-expired, or an unsupported model name.","commonSituations":"Hand-editing or truncating the Codex auth file; upgrading/downgrading the codex CLI out of sync with dcode; a partially written auth file after a killed sign-in; firewall/proxy blocking the token endpoint without producing the specific expired-auth error.","solutions":["Read the chained traceback (`from exc`) for the root cause and fix that directly.","Delete the Codex auth file and re-run `/auth` to regenerate a clean one.","Ensure the `codex` CLI is installed and current (`codex --version`, upgrade as needed).","Check network/proxy access to ChatGPT auth endpoints."],"exampleFix":"// before\n$ dcode -m openai_codex:gpt-5-codex\n// ModelConfigError: Failed to initialize Codex model ... (JSONDecodeError)\n// after\n$ rm ~/.codex/auth.json   # remove corrupted auth file\n# inside dcode: /auth -> openai_codex -> sign in","handlingStrategy":"try-catch","validationCode":"import json\nfrom pathlib import Path\n\nauth_path = Path.home() / \".codex\" / \"auth.json\"\nif auth_path.exists():\n    try:\n        json.loads(auth_path.read_text())\n    except json.JSONDecodeError:\n        auth_path.unlink()  # corrupted; force a clean /auth\n        print(\"Codex auth file corrupted; run /auth to sign in again.\")","typeGuard":null,"tryCatchPattern":"try:\n    model = build_model(\"openai_codex\", model_name)\nexcept ModelConfigError as exc:\n    logger.error(\"Codex init failed: %s\", exc.__cause__)  # inspect root cause\n    reset_codex_auth()      # delete auth file + ensure codex CLI present\n    run_auth_flow(\"openai_codex\")\n    model = build_model(\"openai_codex\", model_name)","preventionTips":["Never hand-edit the Codex auth file; regenerate it via `/auth`.","Keep the `codex` CLI installed and current alongside dcode.","Check network/proxy reachability to ChatGPT auth endpoints in restricted environments.","Always log `exc.__cause__` when wrapping model-construction errors so root causes aren't lost."],"tags":["model-config","codex","initialization","corrupted-state"],"backgroundTag":"model-init-failed","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}