jd-opensource/joyagent-jdgenie · error · ValueError

dict_list 中的每个元素必须是字典

Error message

dict_list 中的每个元素必须是字典, {dict_list}

What it means

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.

Solutions

  1. Log the offending dict_list and fix the upstream producer so each element is an object ({...}) not an array
  2. Pre-filter: [d for d in dict_list if isinstance(d, dict)] before calling, or coerce records with dict(d)
  3. Validate the LLM/parsed JSON structure with jsonschema before passing it into schema conversion
  4. Trace where all_table_schema_list2model_code_schema gets its data and add type checks at that boundary

Example fix

// before
schema_list = llm_json_output  # may contain lists
sorted_list = sort_dict_list_by_keys(schema_list, ["name", "type"])
// after
schema_list = [item if isinstance(item, dict) else dict(zip(["name", "type"], item)) for item in llm_json_output if item]
sorted_list = sort_dict_list_by_keys(schema_list, ["name", "type"])
Defensive patterns

Strategy: type-guard

Validate before calling

def ensure_list_of_dicts(items):
    bad = [i for i in items if not isinstance(i, dict)]
    if bad:
        raise TypeError(f'non-dict schema items: {bad!r}')
    return items
# sort_dict_list_by_keys(ensure_list_of_dicts(schema_list), order)

Type guard

def is_list_of_dicts(v):
    return isinstance(v, list) and all(isinstance(i, dict) for i in v)

Try / catch

try:
    ordered = sort_dict_list_by_keys(schema_list, order)
except ValueError as e:
    logger.error('bad schema list: %s', e)
    schema_list = [d for d in schema_list if isinstance(d, dict)]
    ordered = sort_dict_list_by_keys(schema_list, order)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of jd-opensource/joyagent-jdgenie@2417e0b8b6 (2026-09-08). Data as JSON: /api/errors/8b15104d0ee85ad1. Report an issue: GitHub.

Appendix: source

Thrown at genie-tool/genie_tool/tool/table_rag/utils.py:81

    try:
        float(s)
    except:
        return False
    return True

def sort_dict_list_by_keys(dict_list, desired_order, include_extra_keys=True):
    """
    将字典列表中的每个字典按键的指定顺序重新排序。

    :param dict_list: list[dict] - 要排序的字典列表
    :param desired_order: list[str] - 希望的键顺序
    :param include_extra_keys: bool - 是否包含不在 desired_order 中的键(放在最后)
    :return: list[dict] - 重新排序后的字典列表
    """
    result = []
    for d in dict_list:
        if not isinstance(d, dict):
            raise ValueError(f"dict_list 中的每个元素必须是字典, {dict_list}")
        
        # 按 desired_order 提取存在的键
        sorted_dict = {key: d[key] for key in desired_order if key in d}
        
        # 如果需要,把原字典中其他未在 desired_order 出现的键加在后面
        if include_extra_keys:
            for key in d:
                if key not in desired_order:
                    sorted_dict[key] = d[key]
        
        result.append(sorted_dict)
    
    return result

def softmax(x):
    return np.exp(x) / np.sum(np.exp(x))

def get_rerank(query, doc_list, request_id, url, timeout=0.5):

View on GitHub (pinned to 2417e0b8b6)