parse_ai_message_to_tool_action() — langchain Function Reference
Architecture documentation for the parse_ai_message_to_tool_action() function in tools.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 1cbe46c3_ba5c_1ed6_5566_b4b637a71ed8["parse_ai_message_to_tool_action()"] 8fc68e0d_37aa_0267_4dec_1aba8ce06a76["tools.py"] 1cbe46c3_ba5c_1ed6_5566_b4b637a71ed8 -->|defined in| 8fc68e0d_37aa_0267_4dec_1aba8ce06a76 75132407_8d88_d3b7_0f55_98ad6a8ee121["parse_result()"] 75132407_8d88_d3b7_0f55_98ad6a8ee121 -->|calls| 1cbe46c3_ba5c_1ed6_5566_b4b637a71ed8 style 1cbe46c3_ba5c_1ed6_5566_b4b637a71ed8 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/langchain/langchain_classic/agents/output_parsers/tools.py lines 24–84
def parse_ai_message_to_tool_action(
message: BaseMessage,
) -> list[AgentAction] | AgentFinish:
"""Parse an AI message potentially containing tool_calls."""
if not isinstance(message, AIMessage):
msg = f"Expected an AI message got {type(message)}"
raise TypeError(msg)
actions: list = []
if message.tool_calls:
tool_calls = message.tool_calls
else:
if not message.additional_kwargs.get("tool_calls"):
return AgentFinish(
return_values={"output": message.content},
log=str(message.content),
)
# Best-effort parsing
tool_calls = []
for tool_call in message.additional_kwargs["tool_calls"]:
function = tool_call["function"]
function_name = function["name"]
try:
args = json.loads(function["arguments"] or "{}")
tool_calls.append(
ToolCall(
type="tool_call",
name=function_name,
args=args,
id=tool_call["id"],
),
)
except JSONDecodeError as e:
msg = (
f"Could not parse tool input: {function} because "
f"the `arguments` is not valid JSON."
)
raise OutputParserException(msg) from e
for tool_call in tool_calls:
# A hack here:
# The code that encodes tool input into Open AI uses a special variable
# name called `__arg1` to handle old style tools that do not expose a
# schema and expect a single string argument as an input.
# We unpack the argument here if it exists.
# Open AI does not support passing in a JSON array as an argument.
function_name = tool_call["name"]
_tool_input = tool_call["args"]
tool_input = _tool_input.get("__arg1", _tool_input)
content_msg = f"responded: {message.content}\n" if message.content else "\n"
log = f"\nInvoking: `{function_name}` with `{tool_input}`\n{content_msg}\n"
actions.append(
ToolAgentAction(
tool=function_name,
tool_input=tool_input,
log=log,
message_log=[message],
tool_call_id=tool_call["id"],
),
)
return actions
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does parse_ai_message_to_tool_action() do?
parse_ai_message_to_tool_action() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/output_parsers/tools.py.
Where is parse_ai_message_to_tool_action() defined?
parse_ai_message_to_tool_action() is defined in libs/langchain/langchain_classic/agents/output_parsers/tools.py at line 24.
What calls parse_ai_message_to_tool_action()?
parse_ai_message_to_tool_action() 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