{"record":{"id":"b5f7e6c1b67ad021","repo":"FoundationAgents/MetaGPT","slug":"unsupported-language-language","errorCode":null,"errorMessage":"Unsupported language: {language}","messagePattern":"Unsupported language: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/highlight.py","lineNumber":14,"sourceCode":"# 添加代码语法高亮显示\nfrom pygments import highlight as highlight_\nfrom pygments.formatters import HtmlFormatter, TerminalFormatter\nfrom pygments.lexers import PythonLexer, SqlLexer\n\n\ndef highlight(code: str, language: str = \"python\", formatter: str = \"terminal\"):\n    # 指定要高亮的语言\n    if language.lower() == \"python\":\n        lexer = PythonLexer()\n    elif language.lower() == \"sql\":\n        lexer = SqlLexer()\n    else:\n        raise ValueError(f\"Unsupported language: {language}\")\n\n    # 指定输出格式\n    if formatter.lower() == \"terminal\":\n        formatter = TerminalFormatter()\n    elif formatter.lower() == \"html\":\n        formatter = HtmlFormatter()\n    else:\n        raise ValueError(f\"Unsupported formatter: {formatter}\")\n\n    # 使用 Pygments 高亮代码片段\n    return highlight_(code, lexer, formatter)\n","sourceCodeStart":1,"sourceCodeEnd":26,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/highlight.py#L1-L26","documentation":"Raised by metagpt.utils.highlight.highlight(): the function only maps two languages to pygments lexers — 'python' (PythonLexer) and 'sql' (SqlLexer) — and raises ValueError for anything else, even though pygments itself supports many more languages.","triggerScenarios":"Calling highlight(code, language='javascript'), 'java', 'bash', or any string other than 'python'/'sql' (comparison is lowercased, so 'Python' is fine).","commonSituations":"Reusing this helper to pretty-print arbitrary code blocks (e.g. tool output, generated code in other languages) instead of just Python/SQL; version upgrades where call sites started passing new languages.","solutions":["Restrict calls to language='python' or language='sql'.","For other languages, call pygments directly: pygments.highlight(code, get_lexer_by_name(language), TerminalFormatter()).","Or extend the local wrapper with a mapping/get_lexer_by_name fallback before raising."],"exampleFix":"# before\nfrom metagpt.utils.highlight import highlight\nhighlight(js_code, language='javascript')  # ValueError\n\n# after\nfrom pygments import highlight as hl\nfrom pygments.lexers import get_lexer_by_name\nfrom pygments.formatters import TerminalFormatter\nprint(hl(js_code, get_lexer_by_name('javascript'), TerminalFormatter()))","handlingStrategy":"type-guard","validationCode":"SUPPORTED_LANGS = {'python', 'sql'}\nif language.lower() not in SUPPORTED_LANGS:\n    language = 'python'  # or handle before calling","typeGuard":"def is_supported_language(language: str) -> bool:\n    return isinstance(language, str) and language.lower() in {'python', 'sql'}","tryCatchPattern":"try:\n    out = highlight(code, language=lang)\nexcept ValueError:\n    out = code  # plain text fallback for unsupported languages","preventionTips":["Whitelist languages to {'python','sql'} at call sites of this helper.","For arbitrary languages call pygments get_lexer_by_name directly.","Lowercase the language string before validating."],"tags":["pygments","highlighting","validation"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}