FoundationAgents/MetaGPT · error · ValueError
Unsupported formatter: {formatter}
Error message
Unsupported formatter: {formatter} What it means
Raised by metagpt.utils.highlight.highlight(): the output formatter argument only accepts 'terminal' (TerminalFormatter) or 'html' (HtmlFormatter); any other string raises ValueError after the lexer has already been selected.
Source
Thrown at metagpt/utils/highlight.py:22
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
- Use formatter='terminal' or formatter='html'.
- For other formatters (256-color, IRC, images), construct the pygments formatter object yourself and call pygments.highlight directly.
- Check formatter value against the two allowed strings before calling.
Example fix
# before highlight(code, formatter='terminal256') # ValueError # after from pygments.formatters import Terminal256Formatter from pygments import highlight as hl print(hl(code, PythonLexer(), Terminal256Formatter()))
Defensive patterns
Strategy: type-guard
Validate before calling
SUPPORTED_FORMATTERS = {'terminal', 'html'}
if formatter.lower() not in SUPPORTED_FORMATTERS:
formatter = 'terminal' Type guard
def is_supported_formatter(formatter: str) -> bool:
return isinstance(formatter, str) and formatter.lower() in {'terminal', 'html'} Try / catch
try:
out = highlight(code, formatter=fmt)
except ValueError:
out = highlight(code, formatter='terminal') Prevention
- Only pass 'terminal' or 'html' to this wrapper.
- Need 256-color or other output? Build the pygments formatter yourself.
- Note the check is case-insensitive but exact on the two names.
When it happens
Trigger: Calling highlight(code, language='python', formatter='ansi'), 'console', 'latex' or any value besides 'terminal'/'html' (case-insensitive).
Common situations: Call sites assuming pygments formatter names (e.g. 'terminal256', 'terminal16m') are accepted; copy-paste from pygments docs into code that uses this simplified wrapper.
Related errors
- Unsupported language: {language}
- Only support for python, markdown, but got {language}
- Only support for language: python, markdown, but got {langua
- The invoice format is not zip, pdf, png, or jpg
- Query cannot be empty or contain only whitespace.
AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14).
Data as JSON: /api/errors/6ada2a86031072b7.
Report an issue: GitHub.