Home / Class/ JSONAgentOutputParser Class — langchain Architecture

JSONAgentOutputParser Class — langchain Architecture

Architecture documentation for the JSONAgentOutputParser class in json.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  2c262212_f593_03a5_620b_c7cfcfc48461["JSONAgentOutputParser"]
  d9685a4b_26b0_bca9_f857_03bb2ffc9dd4["AgentOutputParser"]
  2c262212_f593_03a5_620b_c7cfcfc48461 -->|extends| d9685a4b_26b0_bca9_f857_03bb2ffc9dd4
  d6fe6653_f36c_cb5d_8853_46c26a36510e["json.py"]
  2c262212_f593_03a5_620b_c7cfcfc48461 -->|defined in| d6fe6653_f36c_cb5d_8853_46c26a36510e
  24f94156_8615_c223_d455_e60077503839["parse()"]
  2c262212_f593_03a5_620b_c7cfcfc48461 -->|method| 24f94156_8615_c223_d455_e60077503839
  e55b2fa4_66cb_977d_8350_133c13c7a80a["_type()"]
  2c262212_f593_03a5_620b_c7cfcfc48461 -->|method| e55b2fa4_66cb_977d_8350_133c13c7a80a

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/output_parsers/json.py lines 15–57

class JSONAgentOutputParser(AgentOutputParser):
    """Parses tool invocations and final answers in JSON format.

    Expects output to be in one of two formats.

    If the output signals that an action should be taken,
    should be in the below format. This will result in an AgentAction
    being returned.

    ```
    {"action": "search", "action_input": "2+2"}
    ```

    If the output signals that a final answer should be given,
    should be in the below format. This will result in an AgentFinish
    being returned.

    ```
    {"action": "Final Answer", "action_input": "4"}
    ```
    """

    @override
    def parse(self, text: str) -> AgentAction | AgentFinish:
        try:
            response = parse_json_markdown(text)
            if isinstance(response, list):
                # gpt turbo frequently ignores the directive to emit a single action
                logger.warning("Got multiple action responses: %s", response)
                response = response[0]
            if response["action"] == "Final Answer":
                return AgentFinish({"output": response["action_input"]}, text)
            action_input = response.get("action_input", {})
            if action_input is None:
                action_input = {}
            return AgentAction(response["action"], action_input, text)
        except Exception as e:
            msg = f"Could not parse LLM output: {text}"
            raise OutputParserException(msg) from e

    @property
    def _type(self) -> str:
        return "json-agent"

Frequently Asked Questions

What is the JSONAgentOutputParser class?
JSONAgentOutputParser is a class in the langchain codebase, defined in libs/langchain/langchain_classic/agents/output_parsers/json.py.
Where is JSONAgentOutputParser defined?
JSONAgentOutputParser is defined in libs/langchain/langchain_classic/agents/output_parsers/json.py at line 15.
What does JSONAgentOutputParser extend?
JSONAgentOutputParser extends AgentOutputParser.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free