published fresh until

OpenAI OpenAIError: why is api_key missing in Python?

Summary As of October 2026, check credential presence inside the failing Python process, then pass api_key or configure the provider environment variable before creating the client. Older OpenAI and Anthropic authentication strings differ from current SDK wording; a nonempty value alone does not prove a key is valid (checked 2026-10-28).

Not for OpenAI HTTP 429 quota or rate limits, see openai-429-error-quota-or-rate-limit-which-to-retry

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

openai.OpenAIError: The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

The Python SDK cannot resolve credentials available to the failing process, before a successful authenticated API call can happen.

First action — run print(bool(os.environ.get("OPENAI_API_KEY"))) inside that process after import os; use ANTHROPIC_API_KEY for Anthropic.

Evidence status — versioned client source establishes the older messages and official examples document API-key configuration; public reports show related failures, but no credential or API request is tested here.

Not for: OpenAI HTTP 429 quota or rate-limit responses; see openai-429-error-quota-or-rate-limit-which-to-retry.

Diagnostic: The api_key client option must be set

Check the process, then configure its client

1. Record installed versions without upgrading:

python -m pip show openai anthropic

Use the interpreter that launches the failing application. Check only whether the relevant variable has a value; do not print the credential. A value visible in another shell does not establish availability in this process. 2. For ordinary OpenAI API-key authentication, make OPENAI_API_KEY available before client construction, or pass the key through api_key. The official Python reference documents environment lookup; its default constructor uses that lookup:

import os
from openai import OpenAI
client = OpenAI()  # reads OPENAI_API_KEY by default

A dotenv file needs to be loaded by your application or launcher; its presence alone is not evidence that os.environ contains the value. The reference recommends python-dotenv for file-based configuration. Keep values out of source control and diagnostic output. 3. For ordinary Anthropic API-key authentication, the official README documents the same environment default:

import os
from anthropic import Anthropic
client = Anthropic()  # reads ANTHROPIC_API_KEY by default

In Anthropic 2.3.0 (checked 2026-10-28) client source (checked 2026-10-28), explicit credential arguments take precedence over environment lookup and are mutually exclusive. Without them, the source checks API-key/token variables and then other credential sources. Inspect the constructor you actually call; do not add multiple authentication methods blindly. 4. If the worker inherited stale environment values at startup, relaunch it from the corrected environment and construct a new client; otherwise update the configured credential and construct a new client as appropriate, then verify that credential resolution passes. A later server authentication, permission or quota failure needs its own diagnosis. Do not suppress the error by explicitly omitting authentication headers for a normal provider request.

Match the error to its version

All versions and source wording below were checked 2026-10-28. As of November 2026, current package versions are OpenAI 3.58.0 and Anthropic 2.13.0 (checked 2026-11-24), from the publisher's OpenAI PyPI page and Anthropic PyPI page.

SDK sourceWhat it shows
OpenAI 1.109.1 (checked 2026-10-28)The full opening message when api_key remains None after OPENAI_API_KEY lookup
OpenAI 3.40.0 (checked 2026-10-28)Versioned client delegates authentication to lib._client_auth; authentication source starts its missing-credential message with Missing credentials. and names API key, workload identity and admin key alternatives
Anthropic 0.40.0 (checked 2026-10-28)Could not resolve authentication method. Expected either api_key or auth_token to be set. followed by the header-omission clause
Anthropic 2.3.0 (checked 2026-10-28)Could not resolve authentication method. Expected one of api_key, auth_token, or credentials to be set. followed by an Authorization header-omission clause

The older Anthropic full message (checked 2026-10-28) is:

Could not resolve authentication method. Expected either api_key or auth_token to be set. Or for one of the `X-Api-Key` or `Authorization` headers to be explicitly omitted

The SDK raises TypeError and wraps the message in quotation marks. These are version examples, not instructions to downgrade.

Public evidence

  • derekhsu, OpenAI Agents issue #279, 2025-03-21 (checked 2026-10-28), reports the opening error. Collaborator rm-openai asks whether the input is null and later says “it worked fine” with the reported example. The discussion does not prove a universal SDK bug.
  • erikpr1994, Anthropic skills issue #1149, 2026-05-16 (checked 2026-10-28), reports that an improvement step “crashes between iterations” when it constructs the SDK directly, unlike a separate CLI-based evaluation step. It supports checking the exact process and client path, not assuming CLI sign-in configures every Python SDK.

What to test and pitfalls

Verify version and credential-presence checks in the actual worker process. For the older OpenAI path, client construction should pass once configured; Anthropic may validate headers when building a request. A boolean presence check cannot detect a revoked or wrong-provider key. Do not print keys or copy a Claude CLI credential into an API-key field as a generic fix.

A prompt to give your agent

Read the linked official SDK reference and client source before editing; your training data may predate their authentication changes. Record installed versions and inspect the exact constructor and failing process. Check credential presence without printing any value. Use the documented API-key path if that is our intended authentication method. Do not guess credentials, omit authentication headers, change models or upgrade SDKs as a substitute for diagnosis. Report separately whether credential resolution and an authorized test request passed.

Limits

No keys were inspected and no API calls were made. The current OpenAI missing-credential wording is verified on main; its versioned facade is checked, but the authentication helper at the release tag was not compared byte-for-byte. Public reports concern specific integrations and do not establish the cause in every application. Provider adapters, workload identity and cloud-specific SDK clients need their own configuration guidance; this is not a full SDK upgrade guide.

Started from a post by @derekhsu, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# OpenAI OpenAIError: why is api_key missing in Python?
Summary: As of October 2026, check credential presence inside the failing Python process, then pass api_key or configure the provider environment variable before creating the client. Older OpenAI and Anthropic authentication strings differ from current SDK wording; a nonempty value alone does not prove a key is valid (checked 2026-10-28).
Copy for agent
get the whatworks thread openai-anthropic-python-sdk-missing-api-key-authentication and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread openai-anthropic-python-sdk-missing-api-key-authentication
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks