langchain-ai/langchain · error · OutputParserException

Got invalid return object. Expected key `{key}` to be presen

Error message

Got invalid return object. Expected key `{key}` to be present, but got {json_obj}

What it means

Error "Got invalid return object. Expected key `{key}` to be present, but got {json_obj}" thrown in langchain-ai/langchain.

Source

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

    """
    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. Ensure the parsed JSON object contains the expected key exactly as named (check spelling and nesting).
  2. Adjust the expected_keys argument to match the keys the model actually returns.

Example fix

parse_and_check_json_markdown(text, expected_keys=['action', 'action_input'])

When it happens

Trigger: Raised when parsed JSON output lacks a required key that the caller explicitly demanded (e.g. an 'action'/'action_input' key).

Common situations: See trigger scenarios.


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