{"record":{"id":"5a65bc3ac236bd34","repo":"langchain-ai/langchain","slug":"unrecognized-format-format-r-supported-formats","errorCode":null,"errorMessage":"Unrecognized format={format!r}. Supported formats are 'prefix' and 'xml'.","messagePattern":"Unrecognized format=(.+?)\\. Supported formats are 'prefix' and 'xml'\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/messages/utils.py","lineNumber":414,"sourceCode":"                content=\"I'll search for that.\",\n                tool_calls=[\n                    {\"id\": \"call_123\", \"name\": \"search\", \"args\": {\"query\": \"weather\"}}\n                ],\n            ),\n        ]\n        get_buffer_string(messages, format=\"xml\")\n        # -> '<message type=\"ai\">\\\\n'\n        # -> '  <content>I\\\\'ll search for that.</content>\\\\n'\n        # -> '  <tool_call id=\"call_123\" name=\"search\">'\n        # -> '{\"query\": \"weather\"}</tool_call>\\\\n'\n        # -> '</message>'\n        ```\n    \"\"\"\n    if format not in {\"prefix\", \"xml\"}:\n        msg = (\n            f\"Unrecognized format={format!r}. Supported formats are 'prefix' and 'xml'.\"\n        )\n        raise ValueError(msg)\n\n    string_messages = []\n    for m in messages:\n        if isinstance(m, HumanMessage):\n            role = human_prefix\n        elif isinstance(m, AIMessage):\n            role = ai_prefix\n        elif isinstance(m, SystemMessage):\n            role = system_prefix\n        elif isinstance(m, FunctionMessage):\n            role = function_prefix\n        elif isinstance(m, ToolMessage):\n            role = tool_prefix\n        elif isinstance(m, ChatMessage):\n            role = m.role\n        else:\n            msg = f\"Got unsupported message type: {m}\"\n            raise ValueError(msg)  # noqa: TRY004","sourceCodeStart":396,"sourceCodeEnd":432,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/messages/utils.py#L396-L432","documentation":"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.","triggerScenarios":"Calling `get_buffer_string(messages, format='markdown')` or any string other than 'prefix'/'xml'; passing a variable that is None or misspelled.","commonSituations":"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.","solutions":["Use `format='prefix'` (default, `Human:`/`AI:` lines) or `format='xml'`","If the format comes from config, validate it against `{'prefix', 'xml'}` before calling and fail fast with your own error"],"exampleFix":"# before\nget_buffer_string(msgs, format='html')\n\n# after\nget_buffer_string(msgs, format='xml')","handlingStrategy":"validation","validationCode":"def valid_format(fmt: str) -> bool:\n    return fmt in {'prefix', 'xml'}\n\nassert valid_format(fmt), f'format must be prefix or xml, got {fmt!r}'","typeGuard":"def is_known_format(fmt: object) -> bool:\n    return isinstance(fmt, str) and fmt in {'prefix', 'xml'}","tryCatchPattern":null,"preventionTips":["Treat format as an enum, not free text, in config schemas","Default the value explicitly instead of relying on uninitialized variables"],"tags":["prompt-formatting","argument-validation"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}