langchain-ai/langchain · error · OutputParserException

Expected exactly one result, but got {len(result)}

Error message

Expected exactly one result, but got {len(result)}

What it means

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.

Source

Thrown at libs/core/langchain_core/output_parsers/openai_functions.py:95

    def _diff(self, prev: Any | None, next: Any) -> Any:
        return jsonpatch.make_patch(prev, next).patch

    def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:
        """Parse the result of an LLM call to a JSON object.

        Args:
            result: The result of the LLM call.
            partial: Whether to parse partial JSON objects.

        Returns:
            The parsed JSON object.

        Raises:
            OutputParserException: If the output is not valid JSON.
        """
        if len(result) != 1:
            msg = f"Expected exactly one result, but got {len(result)}"
            raise OutputParserException(msg)
        generation = result[0]
        if not isinstance(generation, ChatGeneration):
            msg = "This output parser can only be used with a chat generation."
            raise OutputParserException(msg)
        message = generation.message
        try:
            function_call = message.additional_kwargs["function_call"]
        except KeyError as exc:
            if partial:
                return None
            msg = f"Could not parse function call: {exc}"
            raise OutputParserException(msg) from exc
        try:
            if partial:
                try:
                    if self.args_only:
                        return parse_partial_json(
                            function_call["arguments"], strict=self.strict

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Set n=1 (or omit n) on the model invocation so exactly one generation is returned
  2. Check len(result) before calling the parser and handle 0 or multiple generations explicitly
  3. If you need multiple outputs, iterate generations and parse each individually instead of relying on this parser

Example fix

# before
llm = ChatOpenAI(model="gpt-4o", n=3).bind(function=schema)
result = llm.invoke("...").generations
parsed = parser.parse_result(result)

# after
llm = ChatOpenAI(model="gpt-4o").bind(function=schema)  # n defaults to 1
result = llm.invoke("...").generations
parsed = parser.parse_result(result)
Defensive patterns

Strategy: validation

Validate before calling

if len(result) != 1:
    raise ValueError(f"Expected 1 generation, got {len(result)}; check n parameter")
parsed = parser.parse_result(result)

Try / catch

from langchain_core.exceptions import OutputParserException
try:
    parsed = parser.parse_result(result)
except OutputParserException as e:
    if "Expected exactly one result" in str(e):
        # handle n>1 or empty generation list
        ...

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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