Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize funciton arguments which are 'invalid' JSON #108

Merged
merged 1 commit into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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