{"record":{"id":"c8c4e5654a5e8efc","repo":"datawhalechina/hello-agents","slug":"missing-keys","errorCode":null,"errorMessage":"配置缺少必需的键: {missing_keys}","messagePattern":"配置缺少必需的键: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/helpers.py","lineNumber":36,"sourceCode":"    \"\"\"\n    if timestamp is None:\n        timestamp = datetime.now()\n    return timestamp.strftime(format_str)\n\ndef validate_config(config: Dict[str, Any], required_keys: list) -> bool:\n    \"\"\"\n    验证配置是否包含必需的键\n    \n    Args:\n        config: 配置字典\n        required_keys: 必需的键列表\n        \n    Returns:\n        是否验证通过\n    \"\"\"\n    missing_keys = [key for key in required_keys if key not in config]\n    if missing_keys:\n        raise ValueError(f\"配置缺少必需的键: {missing_keys}\")\n    return True\n\ndef safe_import(module_name: str, class_name: Optional[str] = None) -> Any:\n    \"\"\"\n    安全导入模块或类\n    \n    Args:\n        module_name: 模块名\n        class_name: 类名（可选）\n        \n    Returns:\n        导入的模块或类\n    \"\"\"\n    try:\n        module = importlib.import_module(module_name)\n        if class_name:\n            return getattr(module, class_name)\n        return module","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/YYHDBL-HelloCodeAgentCli/utils/helpers.py#L18-L54","documentation":"ValueError from utils.helpers.validate_config when one or more required keys are absent from the config dict; the missing key names are included in the message. It is a fail-fast startup check used to reject incomplete configuration before downstream code dereferences the keys.","triggerScenarios":"Calling validate_config(config, ['api_key', 'base_url']) with a dict missing either key; keys present but under different names/casing ('apiKey' vs 'api_key'); config loaded from a JSON/YAML file that omitted sections.","commonSituations":"Config schema evolves and older files lack new keys; nested keys expected flat (config['llm']['api_key'] vs config['api_key']); environment-dependent configs where one env's file is incomplete.","solutions":["Add the listed missing keys to the config dict/file with valid values.","Check spelling and nesting — flatten or access nested values before validating.","Log the full expected key list next to the error during onboarding so users know the schema."],"exampleFix":"# before\nvalidate_config(cfg, ['api_key', 'base_url'])\n# cfg = {'apiKey': '...'}  -> ValueError: missing ['api_key', 'base_url']\n\n# after\ncfg = {'api_key': '...', 'base_url': 'https://...'}\nvalidate_config(cfg, ['api_key', 'base_url'])","handlingStrategy":"validation","validationCode":"def config_has_keys(config: dict, required: list) -> bool:\n    return all(k in config for k in required)\n\nif not config_has_keys(cfg, REQUIRED_KEYS):\n    missing = [k for k in REQUIRED_KEYS if k not in cfg]\n    raise ValueError(f'fill in config keys: {missing}')","typeGuard":"def is_complete_config(config: dict, required: list) -> bool:\n    return isinstance(config, dict) and all(k in config and config[k] is not None for k in required)","tryCatchPattern":"try:\n    validate_config(config, REQUIRED_KEYS)\nexcept ValueError as e:\n    raise SystemExit(f'startup aborted — fix config: {e}')","preventionTips":["Ship a config template listing every required key.","Validate once at startup, not per request.","Normalize key names (case, nesting) before validation."],"tags":["config","validation","startup"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}