Skip to content

Commit 85ae8e4

Browse files
authored
detect when tool_calls is a list of JSON strings (#250)
1 parent 2106820 commit 85ae8e4

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

verifiers/scripts/tui.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,24 @@ def format_prompt_or_completion(prompt_or_completion) -> str:
142142
else:
143143
lines.append(f"[dim][b]{role}:[/b] {content}[/dim]")
144144
if "tool_calls" in msg and msg["tool_calls"]:
145-
for tool_call in msg["tool_calls"]:
146-
lines.append(
147-
f"[b]tool call:[/b] {tool_call['function']['name']}\n{tool_call['function']['arguments']}"
148-
)
145+
tool_calls_data = msg["tool_calls"]
146+
if isinstance(tool_calls_data, list) and len(tool_calls_data) > 0 and isinstance(tool_calls_data[0], str):
147+
import json
148+
parsed_tool_calls = []
149+
for tc_str in tool_calls_data:
150+
try:
151+
parsed_tool_calls.append(json.loads(tc_str))
152+
except (json.JSONDecodeError, TypeError):
153+
parsed_tool_calls.append(tc_str)
154+
tool_calls_data = parsed_tool_calls
155+
156+
for tool_call in tool_calls_data:
157+
if isinstance(tool_call, dict) and 'function' in tool_call:
158+
lines.append(
159+
f"[b]tool call:[/b] {tool_call['function']['name']}\n{tool_call['function']['arguments']}"
160+
)
161+
elif isinstance(tool_call, str):
162+
lines.append(f"[b]tool call:[/b] {tool_call}")
149163
return "\n\n".join(lines)
150164
return str(prompt_or_completion)
151165

0 commit comments

Comments
 (0)