langchain-ai/langchain · error · OutputParserException

This output parser can only be used with a chat generation.

Error message

This output parser can only be used with a chat generation.

What it means

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.

Source

Thrown at libs/core/langchain_core/output_parsers/openai_tools.py:186

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

                If `True`, the output will be a JSON object containing
                all the keys that have been returned so far.

                If `False`, the output will be the full JSON object.

        Returns:
            The parsed tool calls.

        Raises:
            OutputParserException: If the output is not valid JSON.
        """
        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
        if isinstance(message, AIMessage) and message.tool_calls:
            tool_calls = [dict(tc) for tc in message.tool_calls]
            for tool_call in tool_calls:
                if not self.return_id:
                    _ = tool_call.pop("id")
        else:
            try:
                raw_tool_calls = copy.deepcopy(message.additional_kwargs["tool_calls"])
            except KeyError:
                return []
            tool_calls = parse_tool_calls(
                raw_tool_calls,
                partial=partial,
                strict=self.strict,
                return_id=self.return_id,
            )
        # for backwards compatibility

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Use a chat model (e.g. ChatOpenAI, ChatAnthropic) whose generations are ChatGeneration instances
  2. In custom LLM implementations, return ChatGeneration(message=AIMessage(...)) from _generate
  3. For completion models, use a text-based parser instead of a tool-calls parser

Example fix

# before
result = [Generation(text="tool output text")]
parsed = parser.parse_result(result)

# after
from langchain_core.messages import AIMessage
from langchain_core.outputs import ChatGeneration
result = [ChatGeneration(message=AIMessage(content="", tool_calls=[{"name": "f", "args": {}, "id": "1"}]))]
parsed = parser.parse_result(result)
Defensive patterns

Strategy: type-guard

Validate before calling

from langchain_core.outputs import ChatGeneration
assert isinstance(result[0], ChatGeneration), "JsonOutputToolsParser requires chat output"

Type guard

from langchain_core.outputs import ChatGeneration

def is_chat_generation(g: object) -> bool:
    return isinstance(g, ChatGeneration)

Try / catch

try:
    parsed = parser.parse_result(result)
except OutputParserException as e:
    if "chat generation" in str(e):
        ...  # swap in a chat model or text parser

Prevention

When it happens

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

Common situations: Custom LLM subclasses not wrapping output in ChatGeneration(AIMessage(...)); test doubles emitting the wrong Generation type; mixing legacy completion chains with chat-only parsers.

Related errors


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