datawhalechina/hello-agents · error · AgentException
不支持的任务类型: {task_type}
Error message
不支持的任务类型: {task_type} What it means
Dispatch error in CoachAgent.run: task_type is matched against the literal set {'explain','polish','mimic','suggest'}; anything else reaches the else-branch raise. Combined with the required-fields check, valid coach tasks must carry task_type in that exact lowercase set — 'Explain', 'summarize', or 'rewrite' all fail here.
Source
Thrown at Co-creation-projects/Apricity-InnocoreAI/agents/coach.py:52
try:
user_id = input_data["user_id"]
task_type = input_data["task_type"] # explain, polish, mimic, suggest
content = input_data["content"]
context = input_data.get("context", {})
result = None
if task_type == "explain":
result = await self._handle_explain_task(user_id, content, context)
elif task_type == "polish":
result = await self._handle_polish_task(user_id, content, context)
elif task_type == "mimic":
result = await self._handle_mimic_task(user_id, content, context)
elif task_type == "suggest":
result = await self._handle_suggest_task(user_id, content, context)
else:
raise AgentException(f"不支持的任务类型: {task_type}")
self.set_state("completed")
return {
"status": "success",
"task_type": task_type,
"user_id": user_id,
"result": result,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
self.set_state("error")
raise AgentException(f"Coach Agent执行失败: {str(e)}")
def get_required_fields(self) -> List[str]:
"""获取必需的输入字段"""
return ["user_id", "task_type", "content"]View on GitHub (pinned to 606a07d341)
Solutions
- Use one of: explain, polish, mimic, suggest (exact lowercase) for task_type.
- Validate/normalize task_type at the API boundary (strip + lower + membership check) before coach.run.
- Centralize the enum (Literal['explain','polish','mimic','suggest'] or an Enum) and drive both the dispatch and the API schema from it.
- When adding a new capability, add both the handler method and the dispatch branch in the same commit.
Example fix
# before
if task_type == "explain": ...
elif task_type == "polish": ...
else:
raise AgentException(f"不支持的任务类型: {task_type}")
# after — enum-driven dispatch, impossible to desync
from enum import Enum
class CoachTask(str, Enum):
EXPLAIN = "explain"; POLISH = "polish"; MIMIC = "mimic"; SUGGEST = "suggest"
try:
handler = getattr(self, f"_handle_{CoachTask(task_type).value}_task")
except ValueError:
raise AgentException(f"不支持的任务类型: {task_type}; 可选: {[t.value for t in CoachTask]}")
result = await handler(user_id, content, context) Defensive patterns
Strategy: validation
Validate before calling
SUPPORTED = {"explain", "polish", "mimic", "suggest"}
task_type = (input_data.get("task_type") or "").strip().lower()
if task_type not in SUPPORTED:
raise ValueError(f"task_type must be one of {sorted(SUPPORTED)}") Type guard
from typing import Literal
CoachTaskType = Literal["explain", "polish", "mimic", "suggest"]
def is_coach_task(v: str) -> bool:
return v in {"explain", "polish", "mimic", "suggest"} Try / catch
try:
result = await coach.run(input_data)
except AgentException as e:
if "不支持的任务类型" in str(e):
return 422, {"error": str(e), "supported": [...]} # client fixable
raise Prevention
- Single source of truth (Enum/Literal) for task types shared by API schema, UI dropdown, and dispatch.
- Normalize case/whitespace on input before dispatch.
- Return the supported list in the error so callers self-correct.
When it happens
Trigger: Calling coach.run({'task_type':'summarize',...}) (unsupported), passing uppercase 'EXPLAIN', a typo like 'polishing', or an LLM orchestrator choosing task types outside the advertised enum.
Common situations: Frontend dropdown options not synced with backend enum; adding a new coaching capability in the prompt but not in the if/elif chain; free-text task_type from users passed through unvalidated.
Related errors
- 工具 '{tool_name}' 不存在
- 缺少必需字段: {field}
- 不支持的任务类型: {task['type']}
- 论文不存在: {paper_id}
- Unsupported latency_mode: {latency_mode}
AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14).
Data as JSON: /api/errors/4eb53d74192efb84.
Report an issue: GitHub.