Skip to content

Commit 0873c05

Browse files
Merge pull request #2640 from blublinsky/jsonfy
try to jsonify tools string parameters
2 parents 0d0c433 + 046d3c3 commit 0873c05

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

ols/src/tools/tools.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Functions/Tools definition."""
22

33
import asyncio
4+
import json
45
import logging
56

67
from langchain_core.messages import ToolMessage
@@ -40,7 +41,7 @@ async def execute_tool_call(
4041
"""Execute a tool call and return the output and status."""
4142
try:
4243
tool = get_tool_by_name(tool_name, all_mcp_tools)
43-
tool_output = await tool.arun(tool_args) # type: ignore [attr-defined]
44+
tool_output = await tool.arun(_jsonify(tool_args)) # type: ignore [attr-defined]
4445
status = "success"
4546
logger.debug(
4647
"Tool: %s | Args: %s | Output: %s", tool_name, tool_args, tool_output
@@ -100,3 +101,26 @@ async def execute_tool_calls(
100101
tool_messages = await asyncio.gather(*tasks)
101102

102103
return tool_messages
104+
105+
106+
def _jsonify(args: dict) -> dict:
107+
"""Convert to JSON."""
108+
res = {}
109+
for key, value in args.items():
110+
if isinstance(value, str) and _maybe_json(value):
111+
# If a value looks like json
112+
try:
113+
# convert to json
114+
res[key] = json.loads(value)
115+
except json.JSONDecodeError:
116+
# conversion fails, use a string
117+
res[key] = value
118+
else:
119+
res[key] = value
120+
return res
121+
122+
123+
def _maybe_json(value: str) -> bool:
124+
"""Check if a string looks like JSON."""
125+
stripped = value.strip()
126+
return stripped.startswith(("[", "{"))

0 commit comments

Comments
 (0)