Home / Class/ ReActJsonSingleInputOutputParser Class — langchain Architecture

ReActJsonSingleInputOutputParser Class — langchain Architecture

Architecture documentation for the ReActJsonSingleInputOutputParser class in react_json_single_input.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  b407092d_625c_efe1_7c6b_84903f820254["ReActJsonSingleInputOutputParser"]
  d9685a4b_26b0_bca9_f857_03bb2ffc9dd4["AgentOutputParser"]
  b407092d_625c_efe1_7c6b_84903f820254 -->|extends| d9685a4b_26b0_bca9_f857_03bb2ffc9dd4
  072586ec_28fa_a8e2_c30e_f5c4c2ff9a31["react_json_single_input.py"]
  b407092d_625c_efe1_7c6b_84903f820254 -->|defined in| 072586ec_28fa_a8e2_c30e_f5c4c2ff9a31
  3bcdd4df_aaef_9e74_e49c_e69aa603a3dd["get_format_instructions()"]
  b407092d_625c_efe1_7c6b_84903f820254 -->|method| 3bcdd4df_aaef_9e74_e49c_e69aa603a3dd
  90abe7a2_f3e7_e935_36d0_34c01b173ead["parse()"]
  b407092d_625c_efe1_7c6b_84903f820254 -->|method| 90abe7a2_f3e7_e935_36d0_34c01b173ead
  da089cba_cb60_9a45_41b2_681a0d65d41b["_type()"]
  b407092d_625c_efe1_7c6b_84903f820254 -->|method| da089cba_cb60_9a45_41b2_681a0d65d41b

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/output_parsers/react_json_single_input.py lines 15–86

class ReActJsonSingleInputOutputParser(AgentOutputParser):
    """Parses ReAct-style LLM calls that have a single tool input 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.

    ```
    Thought: agent thought here
    Action:
    ```
    {
        "action": "search",
        "action_input": "what is the temperature in SF"
    }
    ```
    ```

    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.

    ```
    Thought: agent thought here
    Final Answer: The temperature is 100 degrees
    ```

    """

    pattern: Pattern = re.compile(r"^.*?`{3}(?:json)?\n?(.*?)`{3}.*?$", re.DOTALL)
    """Regex pattern to parse the output."""

    @override
    def get_format_instructions(self) -> str:
        return FORMAT_INSTRUCTIONS

    @override
    def parse(self, text: str) -> AgentAction | AgentFinish:
        includes_answer = FINAL_ANSWER_ACTION in text
        try:
            found = self.pattern.search(text)
            if not found:
                # Fast fail to parse Final Answer.
                msg = "action not found"
                raise ValueError(msg)
            action = found.group(1)
            response = json.loads(action.strip())
            includes_action = "action" in response
            if includes_answer and includes_action:
                msg = (
                    "Parsing LLM output produced a final answer "
                    f"and a parse-able action: {text}"
                )
                raise OutputParserException(msg)
            return AgentAction(
                response["action"],
                response.get("action_input", {}),
                text,
            )

        except Exception as e:
            if not includes_answer:
                msg = f"Could not parse LLM output: {text}"
                raise OutputParserException(msg) from e
            output = text.rsplit(FINAL_ANSWER_ACTION, maxsplit=1)[-1].strip()
            return AgentFinish({"output": output}, text)

    @property
    def _type(self) -> str:
        return "react-json-single-input"

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free