JsonOutputKeyToolsParser Class — langchain Architecture
Architecture documentation for the JsonOutputKeyToolsParser class in openai_tools.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD c2828f9b_eb5b_3cad_9c8f_e14020a48d77["JsonOutputKeyToolsParser"] 866f9561_d4a8_d272_8901_6d748e78c2d8["JsonOutputToolsParser"] c2828f9b_eb5b_3cad_9c8f_e14020a48d77 -->|extends| 866f9561_d4a8_d272_8901_6d748e78c2d8 fb3554e0_291b_93d2_d325_51461432ed8a["ChatGeneration"] c2828f9b_eb5b_3cad_9c8f_e14020a48d77 -->|extends| fb3554e0_291b_93d2_d325_51461432ed8a fcfa55b0_4a86_fa31_a156_3c38c76a0a9b["AIMessage"] c2828f9b_eb5b_3cad_9c8f_e14020a48d77 -->|extends| fcfa55b0_4a86_fa31_a156_3c38c76a0a9b 7d2ea6eb_ed7a_3052_c920_cb5cdc943964["openai_tools.py"] c2828f9b_eb5b_3cad_9c8f_e14020a48d77 -->|defined in| 7d2ea6eb_ed7a_3052_c920_cb5cdc943964 1e79f1df_086c_29b2_208b_2899be253da6["parse_result()"] c2828f9b_eb5b_3cad_9c8f_e14020a48d77 -->|method| 1e79f1df_086c_29b2_208b_2899be253da6
Relationship Graph
Source Code
libs/core/langchain_core/output_parsers/openai_tools.py lines 225–295
class JsonOutputKeyToolsParser(JsonOutputToolsParser):
"""Parse tools from OpenAI response."""
key_name: str
"""The type of tools to return."""
def parse_result(self, result: list[Generation], *, partial: bool = False) -> Any:
"""Parse the result of an LLM call to a list of tool calls.
Args:
result: The result of the LLM call.
partial: Whether to parse partial JSON.
If `True`, the output will be a JSON object containing
all the keys that have been returned so far.
If `False`, the output will be the full JSON object.
Raises:
OutputParserException: If the generation is not a chat generation.
Returns:
The parsed tool calls.
"""
generation = result[0]
if not isinstance(generation, ChatGeneration):
msg = "This output parser can only be used with a chat generation."
raise OutputParserException(msg)
message = generation.message
if isinstance(message, AIMessage) and message.tool_calls:
parsed_tool_calls = [dict(tc) for tc in message.tool_calls]
for tool_call in parsed_tool_calls:
if not self.return_id:
_ = tool_call.pop("id")
else:
try:
# This exists purely for backward compatibility / cached messages
# All new messages should use `message.tool_calls`
raw_tool_calls = copy.deepcopy(message.additional_kwargs["tool_calls"])
except KeyError:
if self.first_tool_only:
return None
return []
parsed_tool_calls = parse_tool_calls(
raw_tool_calls,
partial=partial,
strict=self.strict,
return_id=self.return_id,
)
# For backwards compatibility
for tc in parsed_tool_calls:
tc["type"] = tc.pop("name")
if self.first_tool_only:
parsed_result = list(
filter(lambda x: x["type"] == self.key_name, parsed_tool_calls)
)
single_result = (
parsed_result[0]
if parsed_result and parsed_result[0]["type"] == self.key_name
else None
)
if self.return_id:
return single_result
if single_result:
return single_result["args"]
return None
return (
[res for res in parsed_tool_calls if res["type"] == self.key_name]
if self.return_id
else [
res["args"] for res in parsed_tool_calls if res["type"] == self.key_name
]
)
Domain
Source
Frequently Asked Questions
What is the JsonOutputKeyToolsParser class?
JsonOutputKeyToolsParser is a class in the langchain codebase, defined in libs/core/langchain_core/output_parsers/openai_tools.py.
Where is JsonOutputKeyToolsParser defined?
JsonOutputKeyToolsParser is defined in libs/core/langchain_core/output_parsers/openai_tools.py at line 225.
What does JsonOutputKeyToolsParser extend?
JsonOutputKeyToolsParser extends JsonOutputToolsParser, ChatGeneration, AIMessage.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free