langchain-ai/langchain · error · ValueError

Unrecognized format={format!r}. Supported formats are 'prefi

Error message

Unrecognized format={format!r}. Supported formats are 'prefix' and 'xml'.

What it means

Raised by `get_buffer_string` when its `format` parameter is not one of the two supported rendering modes: 'prefix' (`Human: ...` lines) or 'xml' (`<message type="...">...</message>`). It guards the format dispatch at the top of the function.

Source

Thrown at libs/core/langchain_core/messages/utils.py:414

                content="I'll search for that.",
                tool_calls=[
                    {"id": "call_123", "name": "search", "args": {"query": "weather"}}
                ],
            ),
        ]
        get_buffer_string(messages, format="xml")
        # -> '<message type="ai">\\n'
        # -> '  <content>I\\'ll search for that.</content>\\n'
        # -> '  <tool_call id="call_123" name="search">'
        # -> '{"query": "weather"}</tool_call>\\n'
        # -> '</message>'
        ```
    """
    if format not in {"prefix", "xml"}:
        msg = (
            f"Unrecognized format={format!r}. Supported formats are 'prefix' and 'xml'."
        )
        raise ValueError(msg)

    string_messages = []
    for m in messages:
        if isinstance(m, HumanMessage):
            role = human_prefix
        elif isinstance(m, AIMessage):
            role = ai_prefix
        elif isinstance(m, SystemMessage):
            role = system_prefix
        elif isinstance(m, FunctionMessage):
            role = function_prefix
        elif isinstance(m, ToolMessage):
            role = tool_prefix
        elif isinstance(m, ChatMessage):
            role = m.role
        else:
            msg = f"Got unsupported message type: {m}"
            raise ValueError(msg)  # noqa: TRY004

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Use `format='prefix'` (default, `Human:`/`AI:` lines) or `format='xml'`
  2. If the format comes from config, validate it against `{'prefix', 'xml'}` before calling and fail fast with your own error

Example fix

# before
get_buffer_string(msgs, format='html')

# after
get_buffer_string(msgs, format='xml')
Defensive patterns

Strategy: validation

Validate before calling

def valid_format(fmt: str) -> bool:
    return fmt in {'prefix', 'xml'}

assert valid_format(fmt), f'format must be prefix or xml, got {fmt!r}'

Type guard

def is_known_format(fmt: object) -> bool:
    return isinstance(fmt, str) and fmt in {'prefix', 'xml'}

Prevention

When it happens

Trigger: Calling `get_buffer_string(messages, format='markdown')` or any string other than 'prefix'/'xml'; passing a variable that is None or misspelled.

Common situations: Copy-pasted example code with a format name from an older/newer version; dynamically chosen format strings that fall back to an empty default; LLM-generated calls to the API inventing format values.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/5a65bc3ac236bd34. Report an issue: GitHub.