parse_result() — langchain Function Reference
Architecture documentation for the parse_result() function in openai_tools.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 8e9fd1fe_5de0_04e1_cb35_1696d2e6ac68["parse_result()"] 91a80294_edac_8c82_8cf9_34ecc13994c1["JsonOutputKeyToolsParser"] 8e9fd1fe_5de0_04e1_cb35_1696d2e6ac68 -->|defined in| 91a80294_edac_8c82_8cf9_34ecc13994c1 6362cf2f_ac6d_027b_84a9_3cafe28b4ba5["parse_result()"] 6362cf2f_ac6d_027b_84a9_3cafe28b4ba5 -->|calls| 8e9fd1fe_5de0_04e1_cb35_1696d2e6ac68 6bbf0e48_c095_5f4e_dfb8_4947a3959e12["parse_tool_calls()"] 8e9fd1fe_5de0_04e1_cb35_1696d2e6ac68 -->|calls| 6bbf0e48_c095_5f4e_dfb8_4947a3959e12 6362cf2f_ac6d_027b_84a9_3cafe28b4ba5["parse_result()"] 8e9fd1fe_5de0_04e1_cb35_1696d2e6ac68 -->|calls| 6362cf2f_ac6d_027b_84a9_3cafe28b4ba5 style 8e9fd1fe_5de0_04e1_cb35_1696d2e6ac68 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/output_parsers/openai_tools.py lines 231–295
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
Subdomains
Called By
Source
Frequently Asked Questions
What does parse_result() do?
parse_result() is a function in the langchain codebase, defined in libs/core/langchain_core/output_parsers/openai_tools.py.
Where is parse_result() defined?
parse_result() is defined in libs/core/langchain_core/output_parsers/openai_tools.py at line 231.
What does parse_result() call?
parse_result() calls 2 function(s): parse_result, parse_tool_calls.
What calls parse_result()?
parse_result() is called by 1 function(s): parse_result.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free