Skip to content

Commit

Permalink
Sanitize funciton arguments which are 'invalid' JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
codingjoe committed Jun 17, 2024
1 parent cb1947d commit c953f4f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
5 changes: 4 additions & 1 deletion sam/bot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import ast
import datetime
import json
import logging
Expand Down Expand Up @@ -90,7 +91,9 @@ async def call_tools(run: openai.types.beta.threads.Run, **context) -> None:
f"Tool {tool_call.function.name} not found, cancelling run {run.id}"
) from e
try:
kwargs = json.loads(tool_call.function.arguments)
kwargs = json.loads(
json.dumps(ast.literal_eval(tool_call.function.arguments))
)
except json.JSONDecodeError as e:
await client.beta.threads.runs.cancel(
run_id=run.id, thread_id=run.thread_id
Expand Down
6 changes: 3 additions & 3 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ async def test_call_tools__io_error_fn_name(client):


@pytest.mark.asyncio
async def test_call_tools__io_error_fn_kwargs(client):
async def test_call_tools__type_error_fn_kwargs(client):
run = mock.MagicMock()
tool_call = mock.MagicMock()
tool_call.function.name = "web_search"
tool_call.function.arguments = "{'notJSON'}"
run.required_action.submit_tool_outputs.tool_calls = [tool_call]
with pytest.raises(OSError) as e:
with pytest.raises(TypeError) as e:
await bot.call_tools(run)
assert "Invalid arguments" in str(e.value)
assert "Object of type set is not JSON serializable" in str(e.value)


@pytest.mark.asyncio
Expand Down

0 comments on commit c953f4f

Please sign in to comment.