json.py — langchain Source File
Architecture documentation for json.py, a python file in the langchain codebase. 6 imports, 3 dependents.
Entity Profile
Dependency Diagram
graph LR 0a919d90_e60a_9150_e4f8_7158b3c7818c["json.py"] 2a7f66a7_8738_3d47_375b_70fcaa6ac169["logging"] 0a919d90_e60a_9150_e4f8_7158b3c7818c --> 2a7f66a7_8738_3d47_375b_70fcaa6ac169 80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b["langchain_core.agents"] 0a919d90_e60a_9150_e4f8_7158b3c7818c --> 80d582c5_7cc3_ac96_2742_3dbe1cbd4e2b 75137834_4ba7_dc43_7ec5_182c05eceedf["langchain_core.exceptions"] 0a919d90_e60a_9150_e4f8_7158b3c7818c --> 75137834_4ba7_dc43_7ec5_182c05eceedf 78c5ca66_f675_3ca1_fdb7_d5a994dcf4bf["langchain_core.utils.json"] 0a919d90_e60a_9150_e4f8_7158b3c7818c --> 78c5ca66_f675_3ca1_fdb7_d5a994dcf4bf 91721f45_4909_e489_8c1f_084f8bd87145["typing_extensions"] 0a919d90_e60a_9150_e4f8_7158b3c7818c --> 91721f45_4909_e489_8c1f_084f8bd87145 e160f068_75de_4342_6673_9969b919de85["langchain_classic.agents.agent"] 0a919d90_e60a_9150_e4f8_7158b3c7818c --> e160f068_75de_4342_6673_9969b919de85 40e4b606_56fd_f886_5186_58cae009f177["openai_functions.py"] 40e4b606_56fd_f886_5186_58cae009f177 --> 0a919d90_e60a_9150_e4f8_7158b3c7818c d5e40c71_999a_aa6f_3a6e_f12a6e6fdff6["react_json_single_input.py"] d5e40c71_999a_aa6f_3a6e_f12a6e6fdff6 --> 0a919d90_e60a_9150_e4f8_7158b3c7818c 8fc68e0d_37aa_0267_4dec_1aba8ce06a76["tools.py"] 8fc68e0d_37aa_0267_4dec_1aba8ce06a76 --> 0a919d90_e60a_9150_e4f8_7158b3c7818c style 0a919d90_e60a_9150_e4f8_7158b3c7818c fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
from __future__ import annotations
import logging
from langchain_core.agents import AgentAction, AgentFinish
from langchain_core.exceptions import OutputParserException
from langchain_core.utils.json import parse_json_markdown
from typing_extensions import override
from langchain_classic.agents.agent import AgentOutputParser
logger = logging.getLogger(__name__)
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"
Domain
Subdomains
Classes
Dependencies
- langchain_classic.agents.agent
- langchain_core.agents
- langchain_core.exceptions
- langchain_core.utils.json
- logging
- typing_extensions
Imported By
Source
Frequently Asked Questions
What does json.py do?
json.py is a source file in the langchain codebase, written in python. It belongs to the AgentOrchestration domain, ActionLogic subdomain.
What does json.py depend on?
json.py imports 6 module(s): langchain_classic.agents.agent, langchain_core.agents, langchain_core.exceptions, langchain_core.utils.json, logging, typing_extensions.
What files import json.py?
json.py is imported by 3 file(s): openai_functions.py, react_json_single_input.py, tools.py.
Where is json.py in the architecture?
json.py is located at libs/langchain/langchain_classic/agents/output_parsers/json.py (domain: AgentOrchestration, subdomain: ActionLogic, directory: libs/langchain/langchain_classic/agents/output_parsers).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free