Skip to content

history 接口 500:工具输出 JSON 的 type 为对象时 enrich_media_block_preview 抛 TypeError: unhashable type: 'dict' #1869

Open

Description

@Lv000001

现象

执行自动化(cron)任务后,在 Dashboard 打开该线程的执行记录,GET /api/agents/{agent_id}/threads/{thread_id}/history 返回:

{"error":{"code":"INTERNAL_ERROR","message":"服务器内部错误。","details":{}}}

前端提示「历史记录加载失败,已显示的消息仍然保留。请重试。」。只要线程历史里存在一条问题工具输出,该接口必然 500,页面无法加载(不限于自动化线程,普通聊天线程遇到同类数据同样会挂)。

报错 traceback(关键帧)

File "src/octop/api/routers/chat/history.py", line 372, in get_thread_history
    messages, has_more = _load_projected_thread_messages(
File "src/octop/api/routers/chat/serialize.py", line 479, in _load_projected_thread_messages
    return _enrich_history_tool_media(out, agent_id=agent_id), has_more
File "src/octop/api/routers/chat/serialize.py", line 575, in _enrich_history_tool_media
    new_output = enrich_tool_output_string_sync(output, agent_id=media_agent)
File "src/octop/infra/gateway/media/tool_media.py", line 534, in enrich_tool_output_string_sync
    enriched = enrich_tool_message_content(output, agent_id=agent_id)
File "src/octop/infra/gateway/media/tool_media.py", line 367, in enrich_tool_message_content
    return _map_content_blocks(
File "src/octop/infra/gateway/media/tool_media.py", line 313, in _map_content_blocks
    return json.dumps(transform(parsed), ensure_ascii=False)
File "src/octop/infra/gateway/media/tool_media.py", line 369, in <lambda>
    lambda block: enrich_media_block_preview(block, agent_id=agent_id),
File "src/octop/infra/gateway/media/tool_media.py", line 346, in enrich_media_block_preview
    if not isinstance(block, dict) or block.get("type") not in _MEDIA_BLOCK_TYPES:
TypeError: unhashable type: 'dict'

(行号对应当前 main 分支的 tool_media.py。)

根因

_MEDIA_BLOCK_TYPES 是 frozenset。某些工具返回的 JSON 里,块的 type 字段不是字符串而是对象,例如:

[{"type": {"kind": "image"}}, {"text": "plain"}]

block.get("type") in _MEDIA_BLOCK_TYPES 对不可哈希的 dict 求值直接抛 TypeError: unhashable type: 'dict'。_enrich_history_tool_media 这一层没有兜底,异常一路冒泡到 FastAPI 全局异常处理器,变成 INTERNAL_ERROR。

同样的裸成员判断在 tool_media.py 里共 4 处(iter_media_blocks、enrich_media_block_preview、_enrich_block_with_backend、block_to_content_part),其中 _enrich_block_with_backend 位于聊天流式工具结果增强路径——同一份脏数据在直播过程中也可能触发同类崩溃。

复现

构造一条 thread_messages 投影行,其 tool 消息 content/output 为上述 JSON,然后请求该线程 history;或最小复现:

from octop.infra.gateway.media.tool_media import enrich_media_block_preview

enrich_media_block_preview({"type": {"kind": "image"}}, agent_id="x")
# TypeError: unhashable type: 'dict'

修复建议

  1. 统一用帮助函数先校验 isinstance(block.get("type"), str) 再做 frozenset 成员判断(上述 4 处替换)。
  2. _enrich_history_tool_media 中对 enrich_tool_output_string_sync 加 try/except:预览 URL 重写是纯展示功能,失败时保留原始输出并打日志降级,不让增强逻辑把整个 history 接口打挂。

环境与版本

  • 部署:Docker + PostgreSQL 控制面,Python 3.12
  • traceback 中的行号与当前 main 分支逐一吻合;已核对 main 的 tool_media.py,4 处裸成员判断(199/346/495/835)与本文描述完全一致,问题在 main 上可复现

Activity

  1. JasonBuildAI commented on Oct 10, 2026

    @JasonBuildAI

    复现并修好了,说明一下我理解的范围:issue 里的报错是 _MEDIA_BLOCK_TYPES(frozenset)的裸成员判断遇到非字符串 type 时抛 TypeError: unhashable type: 'dict',这类块本应被当作非媒体块跳过。develop 上共 4 处同样的判断,除了 history 增强(enrich_media_block_preview、iter_media_blocks),_enrich_block_with_backend 和 block_to_content_part 分别在实时工具结果增强和 IM 媒体投递路径上,所以同一份脏数据也可能打断流式回复而不是只影响历史页。改动是加一个只接受字符串类型的辅助函数并替换这 4 处,另补了 history 请求路径(_load_projected_thread_messages)的回归测试。PR 见 #1871;我没有加 issue 里建议的 try/except 兜底,理由放在 PR 描述里,如果更希望有兜底我再补。

  2. added 2 commits that reference this issue on Oct 10, 2026
    696161d
    32322ea
  3. added 2 commits that reference this issue on Oct 24, 2026
    16fd535
    1c54f22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions