HKUDS/DeepTutor · error · LLMAuthenticationError

Cohere API key is missing from the active LLM profile.

Error message

Cohere API key is missing from the active LLM profile.

What it means

Cohere backend guard in _cohere_complete: Cohere's generate endpoint requires an Authorization bearer token, so a falsy api_key is rejected up front with LLMAuthenticationError(provider='cohere'). No request is made. Note the default base is api.cohere.ai/v1 in this code path.

Source

Thrown at deeptutor/services/llm/cloud_provider.py:826

                            text = None
                        if isinstance(text, str) and text:
                            yield text
                except json.JSONDecodeError:
                    continue


async def _cohere_complete(
    model: str,
    prompt: str,
    system_prompt: str,
    api_key: str | None,
    base_url: str | None,
    max_tokens: int | None = None,
    temperature: float | None = None,
) -> str:
    """Cohere API completion."""
    if not api_key:
        raise LLMAuthenticationError(
            "Cohere API key is missing from the active LLM profile.",
            provider="cohere",
        )

    # Build URL using unified utility
    effective_base = base_url or "https://api.cohere.ai/v1"
    url = f"{effective_base}/chat"

    # Build headers using unified utility
    headers = build_auth_headers(api_key, binding="cohere")

    max_tokens_value = max_tokens if max_tokens is not None else 4096
    temperature_value = temperature if temperature is not None else 0.7
    data: dict[str, object] = {
        "model": model,
        "message": f"{system_prompt}\n\n{prompt}",
        "max_tokens": max_tokens_value,
        "temperature": temperature_value,

View on GitHub (pinned to 3e82f13042)

Solutions

  1. Set the Cohere API key in the active profile / COHERE_API_KEY env var.
  2. Verify with a quick assert on the resolved key before calling.
  3. If testing without a key, choose the local/Ollama provider instead.
  4. Check the env var spelling against Cohere's current docs.

Example fix

// before
resp = await complete(prompt=p, binding="cohere", model="command-r", api_key=os.environ.get("COHERE_API_TOKEN"))

# after
resp = await complete(prompt=p, binding="cohere", model="command-r", api_key=os.environ["COHERE_API_KEY"])
Defensive patterns

Strategy: validation

Validate before calling

api_key = (os.getenv("COHERE_API_KEY") or "").strip()
if not api_key:
    raise RuntimeError("COHERE_API_KEY is required for the cohere binding")
resp = await complete(prompt=p, binding="cohere", model=m, api_key=api_key)

Type guard

def has_cohere_key(api_key: str | None) -> bool:
    return isinstance(api_key, str) and bool(api_key.strip())

Try / catch

try:
    resp = await complete(prompt=p, binding="cohere", model=m, api_key=k)
except LLMAuthenticationError as e:
    if e.provider == "cohere":
        prompt_user_for_key("cohere")
    raise

Prevention

When it happens

Trigger: Calling complete with a cohere binding while the profile's api_key is empty; COHERE_API_KEY unset; key entry blanked during settings migration.

Common situations: Adding Cohere as a provider without completing key setup; env var typo (COHERE_API_KEY vs COHEREAI_API_KEY); CI lacking the secret.

Understand the failure class

Background: "API key is required" / "API key not found" / "No API key was set": the missing-api-key error family across 16 libraries — this error's family across 16 libraries.

Related errors


AI-assisted analysis of HKUDS/DeepTutor@3e82f13042 (2026-08-27). Data as JSON: /api/errors/3ffa1d68505104ea. Report an issue: GitHub.