FoundationAgents/MetaGPT · error · ValueError

Unsupported language: {language}

Error message

Unsupported language: {language}

What it means

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.

Source

Thrown at metagpt/utils/highlight.py:14

# 添加代码语法高亮显示
from pygments import highlight as highlight_
from pygments.formatters import HtmlFormatter, TerminalFormatter
from pygments.lexers import PythonLexer, SqlLexer


def highlight(code: str, language: str = "python", formatter: str = "terminal"):
    # 指定要高亮的语言
    if language.lower() == "python":
        lexer = PythonLexer()
    elif language.lower() == "sql":
        lexer = SqlLexer()
    else:
        raise ValueError(f"Unsupported language: {language}")

    # 指定输出格式
    if formatter.lower() == "terminal":
        formatter = TerminalFormatter()
    elif formatter.lower() == "html":
        formatter = HtmlFormatter()
    else:
        raise ValueError(f"Unsupported formatter: {formatter}")

    # 使用 Pygments 高亮代码片段
    return highlight_(code, lexer, formatter)

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Restrict calls to language='python' or language='sql'.
  2. For other languages, call pygments directly: pygments.highlight(code, get_lexer_by_name(language), TerminalFormatter()).
  3. Or extend the local wrapper with a mapping/get_lexer_by_name fallback before raising.

Example fix

# before
from metagpt.utils.highlight import highlight
highlight(js_code, language='javascript')  # ValueError

# after
from pygments import highlight as hl
from pygments.lexers import get_lexer_by_name
from pygments.formatters import TerminalFormatter
print(hl(js_code, get_lexer_by_name('javascript'), TerminalFormatter()))
Defensive patterns

Strategy: type-guard

Validate before calling

SUPPORTED_LANGS = {'python', 'sql'}
if language.lower() not in SUPPORTED_LANGS:
    language = 'python'  # or handle before calling

Type guard

def is_supported_language(language: str) -> bool:
    return isinstance(language, str) and language.lower() in {'python', 'sql'}

Try / catch

try:
    out = highlight(code, language=lang)
except ValueError:
    out = code  # plain text fallback for unsupported languages

Prevention

When it happens

Trigger: Calling highlight(code, language='javascript'), 'java', 'bash', or any string other than 'python'/'sql' (comparison is lowercased, so 'Python' is fine).

Common situations: 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.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/b5f7e6c1b67ad021. Report an issue: GitHub.