{"record":{"id":"8b15104d0ee85ad1","repo":"jd-opensource/joyagent-jdgenie","slug":"dict-list-dict-list","errorCode":null,"errorMessage":"dict_list 中的每个元素必须是字典, {dict_list}","messagePattern":"dict_list 中的每个元素必须是字典, (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"genie-tool/genie_tool/tool/table_rag/utils.py","lineNumber":81,"sourceCode":"    try:\n        float(s)\n    except:\n        return False\n    return True\n\ndef sort_dict_list_by_keys(dict_list, desired_order, include_extra_keys=True):\n    \"\"\"\n    将字典列表中的每个字典按键的指定顺序重新排序。\n\n    :param dict_list: list[dict] - 要排序的字典列表\n    :param desired_order: list[str] - 希望的键顺序\n    :param include_extra_keys: bool - 是否包含不在 desired_order 中的键（放在最后）\n    :return: list[dict] - 重新排序后的字典列表\n    \"\"\"\n    result = []\n    for d in dict_list:\n        if not isinstance(d, dict):\n            raise ValueError(f\"dict_list 中的每个元素必须是字典, {dict_list}\")\n        \n        # 按 desired_order 提取存在的键\n        sorted_dict = {key: d[key] for key in desired_order if key in d}\n        \n        # 如果需要，把原字典中其他未在 desired_order 出现的键加在后面\n        if include_extra_keys:\n            for key in d:\n                if key not in desired_order:\n                    sorted_dict[key] = d[key]\n        \n        result.append(sorted_dict)\n    \n    return result\n\ndef softmax(x):\n    return np.exp(x) / np.sum(np.exp(x))\n\ndef get_rerank(query, doc_list, request_id, url, timeout=0.5):","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/jd-opensource/joyagent-jdgenie/blob/2417e0b8b636d941ad5fb14c59b20dddfef5375d/genie-tool/genie_tool/tool/table_rag/utils.py#L63-L99","documentation":"sort_dict_list_by_keys in table_rag/utils.py raises ValueError when any element of dict_list is not a dict while re-ordering dictionary lists. It enforces the precondition that the input is a List[dict] before applying the desired key order.","triggerScenarios":"Calling sort_dict_list_by_keys with a list containing non-dict items (e.g. a list of lists from a malformed LLM answer, JSON array of arrays, or a string element) — typically via all_table_schema_list2model_code_schema fed with unvalidated schema data.","commonSituations":"LLM returns schema info as arrays instead of objects; upstream JSON has nested arrays where objects were expected; a None slips into the list; caller passes a dict-of-lists instead of list-of-dicts.","solutions":["Log the offending dict_list and fix the upstream producer so each element is an object ({...}) not an array","Pre-filter: [d for d in dict_list if isinstance(d, dict)] before calling, or coerce records with dict(d)","Validate the LLM/parsed JSON structure with jsonschema before passing it into schema conversion","Trace where all_table_schema_list2model_code_schema gets its data and add type checks at that boundary"],"exampleFix":"// before\nschema_list = llm_json_output  # may contain lists\nsorted_list = sort_dict_list_by_keys(schema_list, [\"name\", \"type\"])\n// after\nschema_list = [item if isinstance(item, dict) else dict(zip([\"name\", \"type\"], item)) for item in llm_json_output if item]\nsorted_list = sort_dict_list_by_keys(schema_list, [\"name\", \"type\"])","handlingStrategy":"type-guard","validationCode":"def ensure_list_of_dicts(items):\n    bad = [i for i in items if not isinstance(i, dict)]\n    if bad:\n        raise TypeError(f'non-dict schema items: {bad!r}')\n    return items\n# sort_dict_list_by_keys(ensure_list_of_dicts(schema_list), order)","typeGuard":"def is_list_of_dicts(v):\n    return isinstance(v, list) and all(isinstance(i, dict) for i in v)","tryCatchPattern":"try:\n    ordered = sort_dict_list_by_keys(schema_list, order)\nexcept ValueError as e:\n    logger.error('bad schema list: %s', e)\n    schema_list = [d for d in schema_list if isinstance(d, dict)]\n    ordered = sort_dict_list_by_keys(schema_list, order)","preventionTips":["Validate LLM schema output with jsonschema before conversion","Coerce JSON arrays of arrays to dicts at the parse boundary","Add unit tests covering malformed schema inputs","Fail fast at ingestion, not deep in sorting"],"tags":["validation","type-error","rag"],"backgroundTag":"type-mismatch","analyzedSha":"2417e0b8b636d941ad5fb14c59b20dddfef5375d","analyzedAt":"2026-09-08T11:28:19.414Z","contentChangedAt":"2026-09-08T11:28:19.414Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}