datawhalechina/hello-agents · warning · HTTPException

llm 配置缺少必需字段: {', '.join(missing_fields)}

Error message

llm 配置缺少必需字段: {', '.join(missing_fields)}

What it means

Raised by PUT /config/CONFIG when the llm object is present but is missing one or more of model_id, api_key, base_url. The endpoint lists the absent fields in the message (e.g. 'llm 配置缺少必需字段: api_key, base_url'), so the exact gaps are named. HTTP 400; nothing is written to disk.

Source

Thrown at Co-creation-projects/tino-chen-HelloClaw/src/api/config.py:107

        ensure_config_json_exists()
        # 严格校验 JSON 格式
        try:
            config_data = json.loads(request.content)
        except json.JSONDecodeError as e:
            raise HTTPException(status_code=400, detail=f"无效的 JSON 格式: {str(e)}")

        # 校验必需字段
        if not isinstance(config_data, dict):
            raise HTTPException(status_code=400, detail="配置必须是 JSON 对象")

        if "llm" not in config_data:
            raise HTTPException(status_code=400, detail="缺少必需字段: llm")

        llm_config = config_data.get("llm", {})
        required_fields = ["model_id", "api_key", "base_url"]
        missing_fields = [f for f in required_fields if f not in llm_config]
        if missing_fields:
            raise HTTPException(status_code=400, detail=f"llm 配置缺少必需字段: {', '.join(missing_fields)}")

        config_path = get_config_json_path()
        with open(config_path, "w", encoding="utf-8") as f:
            f.write(request.content)
        return {"name": name, "status": "updated"}

    # 处理 .md 配置文件
    if name not in ws.list_configs():
        raise HTTPException(status_code=404, detail=f"配置文件 {name} 不存在")

    ws.save_config(name, request.content)
    return {"name": name, "status": "updated"}


def get_agent():
    """获取全局 Agent 实例"""
    from ..main import get_agent as _get_agent
    return _get_agent()

View on GitHub (pinned to 606a07d341)

Solutions

  1. Add every field named in the error message under llm: model_id, api_key, base_url
  2. Verify exact snake_case spellings — apiKey/baseUrl/model are not accepted
  3. For local backends set base_url to the local endpoint (e.g. http://localhost:11434/v1) and a non-empty placeholder api_key if required

Example fix

// before
{llm: {model_id: "gpt-4", apiKey: "sk-..."}}
// after
{llm: {model_id: "gpt-4", api_key: "sk-...", base_url: "https://api.openai.com/v1"}}
Defensive patterns

Strategy: validation

Validate before calling

const REQUIRED = ['model_id', 'api_key', 'base_url'];
const missing = REQUIRED.filter(k => !(k in parsed.llm));
if (missing.length) showError(`llm missing: ${missing.join(', ')}`);

Type guard

const isValidLlm = (v: unknown): v is { model_id: string; api_key: string; base_url: string } =>
  typeof v === 'object' && v !== null &&
  ['model_id','api_key','base_url'].every(k => k in v);

Prevention

When it happens

Trigger: PUT /api/config/CONFIG with llm = {"model_id": "gpt-4"} (missing api_key/base_url), or llm set to an empty object, or fields misspelled ("apiKey", "baseUrl", "model").

Common situations: Migrating from an older config schema that used different key names; user intentionally blanks the api_key for a local/Ollama model not realizing base_url is still mandatory; frontend form submits only dirty fields.

Related errors


AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14). Data as JSON: /api/errors/f5280e3ffbaf218e. Report an issue: GitHub.