ValueError: dictionary update sequence element #0 has length 236; 2 is required

用户在 ComfyUI 中使用 GGUFLoaderKJ 节点加载 GGUF 模型文件时触发此错误。该节点属于 comfyui-kjnodes 自定义节点,用于加载 GGUF 格式的大语言模型或扩散模型。

ValueError: dictionary update sequence element #0 has length 236; 2 is required

ValueError: dictionary update sequence element #0 has length 236; 2 is required

快速结论:该报错通常发生在 ComfyUI 的 GGUFLoaderKJ 节点加载 GGUF 格式模型时,由于 extra_model 数据结构不符合字典序列格式而导致合并失败。优先排查 KJNodes 版本,并可通过修改节点源代码中的合并逻辑解决问题。

问题场景

用户在 ComfyUI 中使用 GGUFLoaderKJ 节点加载 GGUF 模型文件时触发此错误。该节点属于 comfyui-kjnodes 自定义节点,用于加载 GGUF 格式的大语言模型或扩散模型。

报错原文

ValueError: dictionary update sequence element #0 has length 236; 2 is required

# 完整堆栈跟踪关键行:
File "...\ComfyUI\custom_nodes\comfyui-kjnodes\nodes\model_optimization_nodes.py", line 1650, in execute
    sd.update(extra_model)
    ~~~~~~~~~^^^^^^^^^^^^^

原因分析

根本原因是 extra_model 变量的数据结构不符合 dict.update() 方法的预期格式。该方法期望接收一个字典或包含键值对的可迭代对象,但实际传入的对象是长度为 236 的其他类型(可能是列表或张量集合),导致 Python 无法将其解析为 2 元素键值对序列。

可能原因:GGUF 加载器返回的模型数据结构与 KJNodes 节点期望的格式不一致,可能涉及 GGUF 格式版本变化或特定模型文件的结构差异。此问题在 2026 年 4 月的最新版 ComfyUI Desktop 和 KJNodes 中仍然存在。

环境排查

  • ComfyUI 版本:0.9.2(Issue 中报告版本)
  • Python 版本:3.13.9
  • PyTorch 版本:2.9.1+cu130
  • 显卡:NVIDIA GeForce RTX 4070(CUDA 可用)
  • KJNodes 版本:最新版(截至 2026 年 4 月)
  • GGUF 模型文件:确认模型文件是否完整、是否为标准 GGUF 格式

解决步骤

  1. 备份源文件:打开 ComfyUI\custom_nodes\comfyui-kjnodes\nodes\model_optimization_nodes.py,先备份原文件。
  2. 定位代码行:找到 extra_model = gguf_nodes.loader.gguf_sd_loader(extra_model_full_path) 这一行。
  3. 替换合并逻辑:从该行开始,直到 sd.update(extra_model) 为止,替换为以下逻辑(注意缩进对齐):
extra_model = gguf_nodes.loader.gguf_sd_loader(extra_model_full_path)

try:
    print("[GGUFLoaderKJ] extra_model type:", type(extra_model))
    merged = False

    def is_tensor_like(x):
        try:
            return hasattr(x, "nelement") or isinstance(x, torch.Tensor)
        except Exception:
            return False

    candidate_dicts = []
    if isinstance(extra_model, dict):
        candidate_dicts = [extra_model]
    elif isinstance(extra_model, (list, tuple)):
        for item in extra_model:
            if isinstance(item, dict):
                candidate_dicts.append(item)
            elif isinstance(item, (list, tuple)) and len(item) == 2 and isinstance(item[0], str):
                pass

    for d in candidate_dicts:
        values = list(d.values())
        if not values:
            continue
        sample = values[:50]
        tensor_like = sum(1 for v in sample if is_tensor_like(v))
        if tensor_like / len(sample) > 0.5:
            sd.update(d)
            merged = True
            break

    if not merged:
        if isinstance(extra_model, dict):
            for k, v in extra_model.items():
                if is_tensor_like(v):
                    sd[k] = v
                    merged = True
        elif isinstance(extra_model, (list, tuple)):
            for item in extra_model:
                if isinstance(item, (list, tuple)) and len(item) == 2 and isinstance(item[0], str) and is_tensor_like(item[1]):
                    sd[item[0]] = item[1]
                    merged = True

    if not merged:
        try:
            coerced = dict(extra_model)
            for k, v in coerced.items():
                if is_tensor_like(v):
                    sd[k] = v
                    merged = True
        except Exception:
            pass

    if not merged:
        print("[GGUFLoaderKJ] Could not find tensor-like mapping in extra_model. Preview:", repr(extra_model)[:800])
        raise ValueError("Cannot merge extra_model into state dict: no tensor-like mapping found")
finally:
    try:
        del extra_model
    except Exception:
        pass
  1. 保存文件:保存修改后的 model_optimization_nodes.py
  2. 重启 ComfyUI:完全关闭并重新启动 ComfyUI,重新加载工作流。

验证方法

重新运行触发错误的 GGUFLoaderKJ 节点工作流。如果节点正常加载模型并输出结果,且控制台未再出现 ValueError 错误,即表示问题已修复。可通过检查生成的模型输出或运行推理步骤进一步确认。

参考来源

kijai/ComfyUI-KJNodes #506

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

celebrityanime
celebrityanime
文章: 7645

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注