{"record":{"id":"6ada2a86031072b7","repo":"FoundationAgents/MetaGPT","slug":"unsupported-formatter-formatter","errorCode":null,"errorMessage":"Unsupported formatter: {formatter}","messagePattern":"Unsupported formatter: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/highlight.py","lineNumber":22,"sourceCode":"from 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":4,"sourceCodeEnd":26,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/highlight.py#L4-L26","documentation":"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.","triggerScenarios":"Calling highlight(code, language='python', formatter='ansi'), 'console', 'latex' or any value besides 'terminal'/'html' (case-insensitive).","commonSituations":"Call sites assuming pygments formatter names (e.g. 'terminal256', 'terminal16m') are accepted; copy-paste from pygments docs into code that uses this simplified wrapper.","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."],"exampleFix":"# before\nhighlight(code, formatter='terminal256')  # ValueError\n\n# after\nfrom pygments.formatters import Terminal256Formatter\nfrom pygments import highlight as hl\nprint(hl(code, PythonLexer(), Terminal256Formatter()))","handlingStrategy":"type-guard","validationCode":"SUPPORTED_FORMATTERS = {'terminal', 'html'}\nif formatter.lower() not in SUPPORTED_FORMATTERS:\n    formatter = 'terminal'","typeGuard":"def is_supported_formatter(formatter: str) -> bool:\n    return isinstance(formatter, str) and formatter.lower() in {'terminal', 'html'}","tryCatchPattern":"try:\n    out = highlight(code, formatter=fmt)\nexcept ValueError:\n    out = highlight(code, formatter='terminal')","preventionTips":["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."],"tags":["pygments","highlighting","validation"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}