ZhuLinsen/daily_stock_analysis · error · ValueError

No API key found for vision model {model}

Error message

No API key found for vision model {model}

What it means

Raised by _call_litellm_vision in image_stock_extractor.py when no API key resolves for the selected vision model and no LiteLLM deployment provides usable params. The service first tries per-model keys (_get_api_keys_for_model), then LiteLLM deployments (including ones allowing an empty key, e.g. local/Ollama providers). Only when both key sources are empty does it refuse to call the model.

Source

Thrown at src/services/image_stock_extractor.py:304

        deployment = next(
            (
                item
                for item in deployments
                if str((item.get("litellm_params") or {}).get("api_key") or "").strip() == key
            ),
            None,
        )
        if deployment is None:
            deployment = next(
                (item for item in deployments if _deployment_allows_empty_api_key(item)),
                None,
            )
            if deployment is not None:
                key = None
        if deployment is not None:
            deployment_params = dict(deployment.get("litellm_params") or {})
    if key is None and not deployment_params:
        raise ValueError(f"No API key found for vision model {model}")
    wire_model = str(deployment_params.get("model") or model).strip()

    data_url = f"data:{mime_type};base64,{image_b64}"
    call_kwargs: dict = {
        "model": wire_model,
        "messages": [
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": EXTRACT_PROMPT},
                    {"type": "image_url", "image_url": {"url": data_url}},
                ],
            }
        ],
        "max_tokens": 1024,
        "timeout": VISION_API_TIMEOUT,
    }
    effective_api_key = str(deployment_params.get("api_key") or key or "").strip()

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Set the API key matching the vision model in .env (e.g. GEMINI_API_KEY for gemini/* models) and restart
  2. Check _get_api_keys_for_model/_resolve_vision_model to confirm which env var name your model maps to
  3. If using a local/proxy model, configure a LiteLLM deployment that allows an empty API key
  4. Verify with a minimal script that the key is loaded into config before calling the extractor

Example fix

# before: no vision key set, calling extractor raises
items, raw = extract_stock_codes_from_image(img_bytes, 'image/png')
# after: set key in environment/config first
os.environ['GEMINI_API_KEY'] = '...'
items, raw = extract_stock_codes_from_image(img_bytes, 'image/png')
Defensive patterns

Strategy: validation

Validate before calling

from src.services.image_stock_extractor import _resolve_vision_model, _get_api_keys_for_model
from src.config import get_config
model = _resolve_vision_model()
keys = _get_api_keys_for_model(model, get_config())
if not keys:
    raise SystemExit(f'Configure an API key for vision model {model} before importing images')

Try / catch

try:
    items, raw = extract_stock_codes_from_image(b, mime)
except ValueError as e:
    if 'No API key found for vision model' in str(e):
        # config problem — do not retry; prompt user to set key
        show_config_error(str(e))
    else:
        raise

Prevention

When it happens

Trigger: Calling extract_stock_codes_from_image() when the resolved vision model (from _resolve_vision_model(), e.g. gemini/*, gpt-4o*, claude*) has no matching key in config and litellm.get_model_list() contains no deployment whose model matches or allows an empty api_key.

Common situations: Vision-related env vars (e.g. GEMINI_API_KEY / OPENAI_API_KEY) missing or misspelled in .env; LiteLLM proxy not configured so deployments list is empty; model name in config not matching any key variable naming convention; running in CI without secrets.

Related errors


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/e58205852e56a516. Report an issue: GitHub.