jeecgboot/JeecgBoot · error · ValueError

未知操作: ${operation}

Error message

未知操作: ${operation}

What it means

`execute_text_process` is an LLM-tool handler that takes an `operation` argument. Only five values are valid: `length`, `upper`, `lower`, `reverse`, `count_words`. Any other string raises `ValueError(f"未知操作: {operation}")`, which the surrounding try/except catches and returns as `{status: "error", error: str(e)}` to the caller.

Source

Thrown at jeecg-boot/jeecg-boot-module/jeecg-boot-module-airag/doc/RAG/main.py:236

            if operation == "length":
                result = len(text)
                result_str = f"文本长度: {result} 个字符"
            elif operation == "upper":
                result = text.upper()
                result_str = f"大写: {result}"
            elif operation == "lower":
                result = text.lower()
                result_str = f"小写: {result}"
            elif operation == "reverse":
                result = text[::-1]
                result_str = f"反转: {result}"
            elif operation == "count_words":
                words = len(text.split())
                result = words
                result_str = f"单词数: {words}"
            else:
                raise ValueError(f"未知操作: {operation}")

            return {
                "status": "success",
                "operation": operation,
                "original_text": text,
                "result": result,
                "result_str": result_str,
                "text_length": len(text)
            }

        except Exception as e:
            return {
                "status": "error",
                "error": str(e),
                "operation": args.get("operation", "")
            }

    def execute_format_data(self, args: Dict[str, Any]) -> Dict[str, Any]:

View on GitHub (pinned to 96fb33f5ec)

Solutions

  1. Pass one of the supported values: `length`, `upper`, `lower`, `reverse`, `count_words`.
  2. To support a new op, add an `elif operation == '...'` branch in `execute_text_process` AND document it in the tool schema.
  3. Constrain the tool-call JSON schema with an `enum` of valid operations so the LLM can only pick supported values.
  4. Normalize the input (lowercase + alias map) before the if-chain so casing/aliases don't cause a miss.

Example fix

# before
else:
    raise ValueError(f"未知操作: {operation}")

# after — normalize + alias + enumerate valid ops in the message
VALID_OPS = {"length", "upper", "lower", "reverse", "count_words"}
ALIASES = {"word_count": "count_words", "countwords": "count_words"}
operation = ALIASES.get(str(operation).strip().lower(), str(operation).strip().lower())
if operation not in VALID_OPS:
    raise ValueError(f"未知操作: {operation} (支持: {', '.join(sorted(VALID_OPS))})")
Defensive patterns

Strategy: validation

Validate before calling

# validate the operation before dispatching to execute_text_process
VALID_TEXT_OPS = {"length", "upper", "lower", "reverse", "count_words"}
operation = args.get("operation", "length")
if operation not in VALID_TEXT_OPS:
    return {"status": "error", "error": f"未知操作: {operation} (支持: {', '.join(sorted(VALID_TEXT_OPS))})"}

Type guard

def is_valid_text_operation(op: object) -> bool:
    return isinstance(op, str) and op in {"length", "upper", "lower", "reverse", "count_words"}

Try / catch

# the handler already wraps execution; keep it and add up-front validation
try:
    if not is_valid_text_operation(operation):
        raise ValueError(f"未知操作: {operation}")
    # ... dispatch
except Exception as e:
    return {"status": "error", "error": str(e)}

Prevention

When it happens

Trigger: The agent/LLM emits a tool call with an unsupported operation (e.g. `trim`, `substring`, `capitalize`, or a typo like `word_count`/`countWords`). Also fires when the tool schema does not enforce an enum and free text reaches the handler, or after version drift adds/renames operations.

Common situations: LLM hallucinated an operation name; the tool description doesn't list the valid values; a new op was added on the backend but the client prompt uses an outdated list; locale/camelCase naming mismatch (`countWords` vs `count_words`).

Related errors


AI-assisted analysis of jeecgboot/JeecgBoot@96fb33f5ec (2026-08-14). Data as JSON: /api/errors/713729d7769eb555. Report an issue: GitHub.