Home / Function/ parse() — langchain Function Reference

parse() — langchain Function Reference

Architecture documentation for the parse() function in react_json_single_input.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  82fe8ecf_d248_3f94_9ae6_6b64c9d27d9d["parse()"]
  9710ac05_9156_8ac4_3f96_8c6d01f3f675["ReActJsonSingleInputOutputParser"]
  82fe8ecf_d248_3f94_9ae6_6b64c9d27d9d -->|defined in| 9710ac05_9156_8ac4_3f96_8c6d01f3f675
  8a6ae8dc_8433_ec1a_2372_2641f4d84ab5["parse()"]
  82fe8ecf_d248_3f94_9ae6_6b64c9d27d9d -->|calls| 8a6ae8dc_8433_ec1a_2372_2641f4d84ab5
  style 82fe8ecf_d248_3f94_9ae6_6b64c9d27d9d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/output_parsers/react_json_single_input.py lines 54–82

    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)

Subdomains

Calls

Frequently Asked Questions

What does parse() do?
parse() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/output_parsers/react_json_single_input.py.
Where is parse() defined?
parse() is defined in libs/langchain/langchain_classic/agents/output_parsers/react_json_single_input.py at line 54.
What does parse() call?
parse() calls 1 function(s): parse.

Analyze Your Own Codebase

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

Try Supermodel Free