apache/seatunnel · error · ValueError

OPENAI_API_KEY environment variable is required for…

Error message

OPENAI_API_KEY environment variable is required for AI_PROVIDER=openai

What it means

OpenAIProvider reads OPENAI_API_KEY (and optional OPENAI_BASE_URL) and refuses to construct the client without a key, raising ValueError. The key is required to authenticate Chat Completions requests.

Solutions

  1. export OPENAI_API_KEY=sk-... (or your provider-compatible key) before running the CLI
  2. Ensure the secret reaches the process (CI config, shell profile, dotenv loading)
  3. Double-check the variable name OPENAI_API_KEY and that it contains a non-empty value

Example fix

// before
export AI_PROVIDER=openai
// after
export AI_PROVIDER=openai
export OPENAI_API_KEY=sk-...
Defensive patterns

Strategy: validation

Validate before calling

import os, sys
if not os.environ.get('OPENAI_API_KEY'):
    sys.exit('OPENAI_API_KEY is not set')

Try / catch

try:
    provider = OpenAIProvider()
except ValueError as e:
    print(e)  # missing OPENAI_API_KEY
    sys.exit(2)

Prevention

When it happens

Trigger: AI_PROVIDER=openai with the openai package installed, but OPENAI_API_KEY is unset or empty at provider __init__ time.

Common situations: Key exported in a different shell/session; CI environment lacking the secret; using a compatible API but forgetting the key entirely; .env not sourced.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of apache/seatunnel@cf67b549a7 (2026-09-10). Data as JSON: /api/errors/44dee37b99444afe. Report an issue: GitHub.

Appendix: source

Thrown at seatunnel-cli/seatunnel_cli/llm_provider.py:713

# ─── OpenAI Provider ───

class OpenAIProvider(LLMProvider):
    """OpenAI Chat Completions API provider."""

    def __init__(self):
        try:
            import openai
        except ImportError:
            raise ImportError(
                "openai package required for AI_PROVIDER=openai. "
                "Install it: pip install openai"
            )

        api_key = os.environ.get("OPENAI_API_KEY")
        base_url = os.environ.get("OPENAI_BASE_URL")  # Supports compatible APIs
        if not api_key:
            raise ValueError("OPENAI_API_KEY environment variable is required for AI_PROVIDER=openai")

        client_kwargs = {"api_key": api_key}
        if base_url:
            client_kwargs["base_url"] = base_url
        self._client = openai.OpenAI(**client_kwargs)
        self._model_id = os.environ.get("OPENAI_MODEL", "gpt-4o")
        self._fast_model_id = os.environ.get("OPENAI_SMALL_FAST_MODEL", "gpt-4o-mini")
        self._echo_reasoning_content = _env_bool("OPENAI_ECHO_REASONING_CONTENT", True)

    @property
    def provider_name(self) -> str:
        return "openai"

    @property
    def model_id(self) -> str:
        return self._model_id

    @property

View on GitHub (pinned to cf67b549a7)