langchain-ai/langchain · error · OutputParserException

Expected JSON object (dict), but got: {type(json_obj).__name

Error message

Expected JSON object (dict), but got: {type(json_obj).__name__}. 

What it means

Error "Expected JSON object (dict), but got: {type(json_obj).__name__}. " thrown in langchain-ai/langchain.

Source

Thrown at libs/core/langchain_core/utils/json.py:221

        expected_keys: The expected keys in the JSON string.

    Returns:
        The parsed JSON object as a Python dictionary.

    Raises:
        OutputParserException: If the JSON string is invalid or does not contain
            the expected keys.
    """
    try:
        json_obj = parse_json_markdown(text)
    except json.JSONDecodeError as e:
        msg = f"Got invalid JSON object. Error: {e}"
        raise OutputParserException(msg) from e
    if not isinstance(json_obj, dict):
        error_message = (
            f"Expected JSON object (dict), but got: {type(json_obj).__name__}. "
        )
        raise OutputParserException(error_message, llm_output=text)

    for key in expected_keys:
        if key not in json_obj:
            msg = (
                f"Got invalid return object. Expected key `{key}` "
                f"to be present, but got {json_obj}"
            )
            raise OutputParserException(msg)
    return json_obj

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Return a JSON object (dict) at the top level, not a JSON array or scalar.
  2. If the model returned a list, wrap it in an object, e.g. {"items": [...]}.

Example fix

{"items": [1, 2, 3]}  # instead of [1, 2, 3]

When it happens

Trigger: Raised when parsed JSON output is not a JSON object (dict) as required, e.g. it is a list or scalar.

Common situations: See trigger scenarios.


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