{"record":{"id":"5b45a5732e11b7ca","repo":"langchain-ai/langchain","slug":"this-output-parser-can-only-be-used-with-a-chat-ge-5b45a5","errorCode":null,"errorMessage":"This output parser can only be used with a chat generation.","messagePattern":"This output parser can only be used with a chat generation\\.","errorType":"exception","errorClass":"OutputParserException","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/output_parsers/openai_tools.py","lineNumber":186,"sourceCode":"        Args:\n            result: The result of the LLM call.\n            partial: Whether to parse partial JSON.\n\n                If `True`, the output will be a JSON object containing\n                all the keys that have been returned so far.\n\n                If `False`, the output will be the full JSON object.\n\n        Returns:\n            The parsed tool calls.\n\n        Raises:\n            OutputParserException: If the output is not valid JSON.\n        \"\"\"\n        generation = result[0]\n        if not isinstance(generation, ChatGeneration):\n            msg = \"This output parser can only be used with a chat generation.\"\n            raise OutputParserException(msg)\n        message = generation.message\n        if isinstance(message, AIMessage) and message.tool_calls:\n            tool_calls = [dict(tc) for tc in message.tool_calls]\n            for tool_call in tool_calls:\n                if not self.return_id:\n                    _ = tool_call.pop(\"id\")\n        else:\n            try:\n                raw_tool_calls = copy.deepcopy(message.additional_kwargs[\"tool_calls\"])\n            except KeyError:\n                return []\n            tool_calls = parse_tool_calls(\n                raw_tool_calls,\n                partial=partial,\n                strict=self.strict,\n                return_id=self.return_id,\n            )\n        # for backwards compatibility","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/output_parsers/openai_tools.py#L168-L204","documentation":"Raised by JsonOutputToolsParser.parse_result when result[0] is not a ChatGeneration. The parser needs the AIMessage (its tool_calls or additional_kwargs['tool_calls']) to extract tool calls; a plain text Generation carries none of that.","triggerScenarios":"Using JsonOutputToolsParser downstream of a completion-style LLM or a custom LLM returning base Generation objects; passing a manually built [Generation(text=...)] into parse_result.","commonSituations":"Custom LLM subclasses not wrapping output in ChatGeneration(AIMessage(...)); test doubles emitting the wrong Generation type; mixing legacy completion chains with chat-only parsers.","solutions":["Use a chat model (e.g. ChatOpenAI, ChatAnthropic) whose generations are ChatGeneration instances","In custom LLM implementations, return ChatGeneration(message=AIMessage(...)) from _generate","For completion models, use a text-based parser instead of a tool-calls parser"],"exampleFix":"# before\nresult = [Generation(text=\"tool output text\")]\nparsed = parser.parse_result(result)\n\n# after\nfrom langchain_core.messages import AIMessage\nfrom langchain_core.outputs import ChatGeneration\nresult = [ChatGeneration(message=AIMessage(content=\"\", tool_calls=[{\"name\": \"f\", \"args\": {}, \"id\": \"1\"}]))]\nparsed = parser.parse_result(result)","handlingStrategy":"type-guard","validationCode":"from langchain_core.outputs import ChatGeneration\nassert isinstance(result[0], ChatGeneration), \"JsonOutputToolsParser requires chat output\"","typeGuard":"from langchain_core.outputs import ChatGeneration\n\ndef is_chat_generation(g: object) -> bool:\n    return isinstance(g, ChatGeneration)","tryCatchPattern":"try:\n    parsed = parser.parse_result(result)\nexcept OutputParserException as e:\n    if \"chat generation\" in str(e):\n        ...  # swap in a chat model or text parser","preventionTips":["Bind tool parsers only to BaseChatModel runnables","Make test doubles emit ChatGeneration(AIMessage)"],"tags":["tool-calling","output-parsing","type-mismatch"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}