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
`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.
Source
Thrown at libs/core/langchain_core/output_parsers/openai_functions.py:45
@override
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.
"""
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:
func_call = copy.deepcopy(message.additional_kwargs["function_call"])
except KeyError as exc:
msg = f"Could not parse function call: {exc}"
raise OutputParserException(msg) from exc
if self.args_only:
return func_call["arguments"]
return func_call
class JsonOutputFunctionsParser(BaseCumulativeTransformOutputParser[Any]):
"""Parse an output as the JSON object."""
strict: bool = False
"""Whether to allow non-JSON-compliant strings.
View on GitHub (pinned to e32fa9a52e)
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.
Example fix
// before from langchain_openai import OpenAI chain = prompt | OpenAI() | openai_functions.OpenAIFunctionCallerOutputParser() // after from langchain_openai import ChatOpenAI chain = prompt | ChatOpenAI(model="gpt-4o") | openai_functions.OpenAIFunctionCallerOutputParser()
Defensive patterns
Strategy: type-guard
Validate before calling
from langchain_core.outputs import ChatGeneration
def is_chat_generation(result) -> bool:
return bool(result) and isinstance(result[0], ChatGeneration)
if not is_chat_generation(result):
raise ValueError("use a chat model with this parser") Type guard
from langchain_core.outputs import ChatGeneration
def is_chat_gen(g) -> bool:
return isinstance(g, ChatGeneration) Try / catch
from langchain_core.exceptions import OutputParserException
try:
parsed = parser.parse_result(result)
except OutputParserException as e:
if "chat generation" in str(e):
# switch chain to a BaseChatModel, or parse completion text manually
... Prevention
- Use ChatOpenAI/BaseChatModel with function-calling parsers
- Migrate off the legacy OpenAI-functions API
- Assert generation types in chain unit tests
When it happens
Trigger: 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=...)])`.
Common situations: 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.
Related errors
- Could not parse function call: {exc}
- Cannot concatenate FunctionMessageChunks with different name
- Trying to deserialize something that cannot be deserialized
- mime_type key is required for base64 data.
- Unsupported source type. Only 'url' and 'base64' are support
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/3d8c6234d0032864.
Report an issue: GitHub.