Skip to content

Commit 00aaaa1

Browse files
fix: handle malformed JSON in OpenAI tool call arguments gracefully
1 parent 0818d34 commit 00aaaa1

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/askui/models/openai/messages_api.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,23 @@ def _parse_tool_calls(
223223
for tool_call in message.tool_calls:
224224
if not isinstance(tool_call, ChatCompletionMessageToolCall):
225225
continue
226+
try:
227+
arguments = json.loads(tool_call.function.arguments)
228+
except json.JSONDecodeError:
229+
logger.warning(
230+
"Malformed JSON in tool call arguments, passing raw string",
231+
extra={
232+
"tool_call_id": tool_call.id,
233+
"function": tool_call.function.name,
234+
"arguments": tool_call.function.arguments,
235+
},
236+
)
237+
arguments = {"raw_arguments": tool_call.function.arguments}
226238
content_blocks.append(
227239
ToolUseBlockParam(
228240
id=tool_call.id,
229241
name=tool_call.function.name,
230-
input=json.loads(tool_call.function.arguments),
242+
input=arguments,
231243
)
232244
)
233245

0 commit comments

Comments
 (0)