Skip to content

Commit d90a8be

Browse files
committed
Parse tool call responses with an arguments property
1 parent f844bd2 commit d90a8be

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

os_computer_use/llm_provider.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ def create_function_schema(self, definitions):
5757

5858
return functions
5959

60+
# Represent a tool call as an object
61+
def create_tool_call(self, name, parameters):
62+
return {
63+
"type": "function",
64+
"name": name,
65+
"parameters": parameters,
66+
}
67+
6068
# Wrap a content block in a text or an image object
6169
def wrap_block(self, block):
6270
if isinstance(block, bytes):
@@ -125,22 +133,24 @@ def call(self, messages, functions=None):
125133
if functions:
126134
tool_calls = message.tool_calls or []
127135
combined_tool_calls = [
128-
{
129-
"type": "function",
130-
"name": tool_call.function.name,
131-
"parameters": parse_json(tool_call.function.arguments),
132-
}
136+
self.create_tool_call(
137+
tool_call.function.name, parse_json(tool_call.function.arguments)
138+
)
133139
for tool_call in tool_calls
134140
if parse_json(tool_call.function.arguments) is not None
135141
]
136142

137-
# Sometimes, Llama function calls are not parsed by the inference provider. This code parses them manually.
143+
# Sometimes, function calls are returned unparsed by the inference provider. This code parses them manually.
138144
if message.content and not tool_calls:
139145
tool_call_matches = re.search(r"\{.*\}", message.content)
140146
if tool_call_matches:
141147
tool_call = parse_json(tool_call_matches.group(0))
142-
if tool_call.get("name") and tool_call.get("parameters"):
143-
combined_tool_calls.append(tool_call)
148+
# Some models use "arguments" as the key instead of "parameters"
149+
parameters = tool_call.get("parameters", tool_call.get("arguments"))
150+
if tool_call.get("name") and parameters:
151+
combined_tool_calls.append(
152+
self.create_tool_call(tool_call.get("name"), parameters)
153+
)
144154
return None, combined_tool_calls
145155

146156
return message.content, combined_tool_calls
@@ -194,7 +204,7 @@ def call(self, messages, functions=None):
194204
# Return response text and tool calls separately
195205
if functions:
196206
tool_calls = [
197-
{"type": "function", "name": block.name, "parameters": block.input}
207+
self.create_tool_call(block.name, block.input)
198208
for block in completion.content
199209
if block.type == "tool_use"
200210
]

0 commit comments

Comments
 (0)