{"record":{"id":"3d8c6234d0032864","repo":"langchain-ai/langchain","slug":"this-output-parser-can-only-be-used-with-a-chat-ge","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_functions.py","lineNumber":45,"sourceCode":"\n    @override\n    def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:\n        \"\"\"Parse the result of an LLM call to a JSON object.\n\n        Args:\n            result: The result of the LLM call.\n            partial: Whether to parse partial JSON objects.\n\n        Returns:\n            The parsed JSON object.\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        try:\n            func_call = copy.deepcopy(message.additional_kwargs[\"function_call\"])\n        except KeyError as exc:\n            msg = f\"Could not parse function call: {exc}\"\n            raise OutputParserException(msg) from exc\n\n        if self.args_only:\n            return func_call[\"arguments\"]\n        return func_call\n\n\nclass JsonOutputFunctionsParser(BaseCumulativeTransformOutputParser[Any]):\n    \"\"\"Parse an output as the JSON object.\"\"\"\n\n    strict: bool = False\n    \"\"\"Whether to allow non-JSON-compliant strings.\n","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/output_parsers/openai_functions.py#L27-L63","documentation":"`JsonOpenAIFunctionCaller`/`OpenAIFunctionCallerOutputParser.parse_result` requires the first generation to be a `ChatGeneration` (a generation carrying a BaseMessage), because it reads `message.additional_kwargs[\"function_call\"]`. Passing it a plain `Generation` (from a completion-style LLM rather than a chat model) fails immediately with OutputParserException. It exists to fail fast rather than AttributeError deeper in.","triggerScenarios":"Piping a completion-style LLM (`OpenAI` legacy, any `LLM` subclass returning `Generation`) into a chain ending in this parser instead of a chat model; manually calling `parse_result([Generation(text=...)])`.","commonSituations":"Older tutorials built around `OpenAI` completion API and `create_openai_fn_chain`; migrating chains from completions to chat models piecemeal; unit tests constructing bare Generations.","solutions":["Use a chat model (`ChatOpenAI` or other `BaseChatModel`) in the chain so generations are `ChatGeneration`s.","If you must parse a completion-style function call, extract the JSON yourself from `result[0].text` with a JSON parser.","Prefer the modern tool-calling path (`llm.bind_tools`, `with_structured_output`) over the deprecated `function_call` kwargs API."],"exampleFix":"// before\nfrom langchain_openai import OpenAI\nchain = prompt | OpenAI() | openai_functions.OpenAIFunctionCallerOutputParser()\n\n// after\nfrom langchain_openai import ChatOpenAI\nchain = prompt | ChatOpenAI(model=\"gpt-4o\") | openai_functions.OpenAIFunctionCallerOutputParser()","handlingStrategy":"type-guard","validationCode":"from langchain_core.outputs import ChatGeneration\n\ndef is_chat_generation(result) -> bool:\n    return bool(result) and isinstance(result[0], ChatGeneration)\n\nif not is_chat_generation(result):\n    raise ValueError(\"use a chat model with this parser\")","typeGuard":"from langchain_core.outputs import ChatGeneration\n\ndef is_chat_gen(g) -> bool:\n    return isinstance(g, ChatGeneration)","tryCatchPattern":"from langchain_core.exceptions import OutputParserException\n\ntry:\n    parsed = parser.parse_result(result)\nexcept OutputParserException as e:\n    if \"chat generation\" in str(e):\n        # switch chain to a BaseChatModel, or parse completion text manually\n        ...","preventionTips":["Use ChatOpenAI/BaseChatModel with function-calling parsers","Migrate off the legacy OpenAI-functions API","Assert generation types in chain unit tests"],"tags":["output-parsers","openai","function-calling","legacy","chat-models"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}