shareAI-lab/learn-claude-code · error · GoalError

MODEL_ID is required in the environment or .env

Error message

MODEL_ID is required in the environment or .env

What it means

make_live_session loaded .env (load_dotenv(override=True)) and read MODEL_ID from the environment, but it was empty/unset (s17_goal_loop/code.py:822). MODEL_ID names the main agent model for the Anthropic client, and a live session cannot be constructed without it, so it fails fast.

Source

Thrown at s17_goal_loop/code.py:822

            ]
            return "\n".join(matches[:200]) if matches else "(no matches)"

        raise GoalError(f"unknown tool '{name}'")


def make_live_session(workdir: Path) -> AgentSession:
    try:
        from anthropic import Anthropic
        from dotenv import load_dotenv
    except ImportError as error:
        raise GoalError(
            "Install dependencies first: pip install -r requirements.txt"
        ) from error

    load_dotenv(override=True)
    model = os.getenv("MODEL_ID")
    if not model:
        raise GoalError("MODEL_ID is required in the environment or .env")
    evaluator_model = (
        os.getenv("GOAL_EVALUATOR_MODEL_ID")
        or os.getenv("ANTHROPIC_DEFAULT_HAIKU_MODEL")
        or model
    )
    if os.getenv("ANTHROPIC_BASE_URL"):
        os.environ.pop("ANTHROPIC_AUTH_TOKEN", None)
    client = Anthropic(base_url=os.getenv("ANTHROPIC_BASE_URL"))
    evaluator = PromptGoalEvaluator(client=client, model=evaluator_model)
    block_cap = int(
        os.getenv(
            "CLAUDE_CODE_STOP_HOOK_BLOCK_CAP",
            str(DEFAULT_STOP_HOOK_BLOCK_CAP),
        )
    )
    goal = GoalController(evaluator=evaluator, block_cap=block_cap)
    max_turns_value = int(os.getenv("MAX_TURNS", "0"))
    return AgentSession(

View on GitHub (pinned to 985456f4ad)

Solutions

  1. Create .env in the project root with MODEL_ID set (e.g. MODEL_ID=claude-sonnet-4-5), or export MODEL_ID in the shell
  2. Check for typos and empty values: 'grep -c "^MODEL_ID=" .env' must find a non-empty line
  3. If .env lives elsewhere, load it explicitly or run the process from the directory containing it
  4. In CI, inject MODEL_ID as a secret/environment variable instead of a file

Example fix

# before
# .env
ANTHROPIC_API_KEY=sk-...

# after
# .env
ANTHROPIC_API_KEY=sk-...
MODEL_ID=claude-sonnet-4-5
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path
if not (os.getenv("MODEL_ID") or "").strip():
    raise SystemExit("MODEL_ID is missing: set it in .env or the environment")
session = make_live_session(workdir)

Type guard

def has_model_id() -> bool:
    return bool((os.getenv("MODEL_ID") or "").strip())

Try / catch

try:
    session = make_live_session(workdir)
except GoalError as error:
    if "MODEL_ID" in str(error):
        raise SystemExit("copy .env.example to .env and set MODEL_ID") from error
    raise

Prevention

When it happens

Trigger: Running the module with no MODEL_ID in the environment and no .env file (or a .env missing the MODEL_ID line); a typo like MODELID= or MODEL_ID= (empty value); .env located in a different directory than the process cwd so load_dotenv never finds it.

Common situations: Fresh setup where .env.example was copied incompletely; CI containers lacking the env var; running from a different working directory so python-dotenv does not discover .env; overriding via shell where the variable was unset by a wrapper script.

Related errors


AI-assisted analysis of shareAI-lab/learn-claude-code@985456f4ad (2026-08-14). Data as JSON: /api/errors/f7a15658704783db. Report an issue: GitHub.