langchain-ai/langchain · error · OutputParserException

Got invalid JSON object. Error: {e}

Error message

Got invalid JSON object. Error: {e}

What it means

Error "Got invalid JSON object. Error: {e}" thrown in langchain-ai/langchain.

Source

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

    Checks that it contains the expected keys.

    Args:
        text: The Markdown string.
        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. Fix the malformed JSON emitted by the model (unbalanced braces/brackets or trailing text); parse_and_check_json_markdown expects valid JSON inside markdown code blocks.
  2. Ensure the completion contains a fenced code block (```json ... ```) with a complete JSON value.

Example fix

text = '```json\n{"a": 1}\n```'  # complete, balanced JSON

When it happens

Trigger: Raised when parsing partial/malformed JSON output from a model fails even after best-effort repair, i.e. the text is not valid JSON.

Common situations: See trigger scenarios.

Understand the failure class


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