{"record":{"id":"847ead5fa264e7e5","repo":"langchain-ai/langchain","slug":"expected-exactly-one-result-but-got-len-result","errorCode":null,"errorMessage":"Expected exactly one result, but got {len(result)}","messagePattern":"Expected exactly one result, but got (.+?)","errorType":"exception","errorClass":"OutputParserException","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/output_parsers/openai_functions.py","lineNumber":95,"sourceCode":"    def _diff(self, prev: Any | None, next: Any) -> Any:\n        return jsonpatch.make_patch(prev, next).patch\n\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        if len(result) != 1:\n            msg = f\"Expected exactly one result, but got {len(result)}\"\n            raise OutputParserException(msg)\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            function_call = message.additional_kwargs[\"function_call\"]\n        except KeyError as exc:\n            if partial:\n                return None\n            msg = f\"Could not parse function call: {exc}\"\n            raise OutputParserException(msg) from exc\n        try:\n            if partial:\n                try:\n                    if self.args_only:\n                        return parse_partial_json(\n                            function_call[\"arguments\"], strict=self.strict","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/output_parsers/openai_functions.py#L77-L113","documentation":"Raised by JsonOutputFunctionsParser.parse_result when the list of Generation objects returned from an LLM call does not contain exactly one element. The legacy OpenAI function-call parsing pipeline expects a single generation to extract the 'function_call' from, so 0 results (empty list) or >1 results (e.g. n>1 completions) are rejected before any parsing happens.","triggerScenarios":"Calling a chain/model with the JsonOutputFunctionsParser bound while the underlying model is configured with n>1 (multiple generations returned), or receiving an empty generations list (e.g. filtered or failed completions), then invoking parse_result on that result list.","commonSituations":"Setting n=2 or higher on an OpenAI ChatCompletion request but using a single-output function parser; custom LLM wrappers that return empty generation lists on error; streaming code that aggregates generations incorrectly.","solutions":["Set n=1 (or omit n) on the model invocation so exactly one generation is returned","Check len(result) before calling the parser and handle 0 or multiple generations explicitly","If you need multiple outputs, iterate generations and parse each individually instead of relying on this parser"],"exampleFix":"# before\nllm = ChatOpenAI(model=\"gpt-4o\", n=3).bind(function=schema)\nresult = llm.invoke(\"...\").generations\nparsed = parser.parse_result(result)\n\n# after\nllm = ChatOpenAI(model=\"gpt-4o\").bind(function=schema)  # n defaults to 1\nresult = llm.invoke(\"...\").generations\nparsed = parser.parse_result(result)","handlingStrategy":"validation","validationCode":"if len(result) != 1:\n    raise ValueError(f\"Expected 1 generation, got {len(result)}; check n parameter\")\nparsed = parser.parse_result(result)","typeGuard":null,"tryCatchPattern":"from langchain_core.exceptions import OutputParserException\ntry:\n    parsed = parser.parse_result(result)\nexcept OutputParserException as e:\n    if \"Expected exactly one result\" in str(e):\n        # handle n>1 or empty generation list\n        ...","preventionTips":["Keep n=1 on model calls feeding single-output function parsers","Assert generation count before parsing in custom pipelines"],"tags":["openai-functions","output-parsing","configuration"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}