Skip to content

fix(wechat): wechat_channel.py sends invalid "tool_call" role to LLM API #758

Description

@ysq1989

Bug Description

wechat_channel.py line 249 directly uses m.role from the database when building LLM history. Tool call logs are stored
with role="tool_call" in ChatMessage, but this is an internal role — it should be converted to standard OpenAI format
(assistant + tool_calls followed by tool result) before sending to the LLM API.

This causes HTTP 400 with providers that strictly validate role (e.g. Agnes AI):

messages[2].role: unknown variant tool_call, expected one of system, user, assistant, tool, latest_reminder

Root Cause

wechat_channel.py line 249 (buggy)

history = [{"role": m.role, "content": m.content} for m in reversed(history_r.scalars().all())]

All other channels correctly use convert_chat_messages_to_llm_format(): feishu.py, dingtalk.py, discord_bot.py,
slack.py, teams.py, gateway.py, websocket.py. WeChat is the only channel that skips this conversion.

Fix

from app.services.llm.utils import convert_chat_messages_to_llm_format
history = convert_chat_messages_to_llm_format(reversed(history_r.scalars().all()))

Steps to Reproduce

  1. Configure an agent with WeChat channel
  2. Send a message that triggers a tool call
  3. After tool call completes, send another message
  4. LLM call fails with HTTP 400

Environment

  • Clawith v1.10.3 (main branch)
  • File: backend/app/services/wechat_channel.py, line 249

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions