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

add handling to have informative 404 when a guard doesnt exist on openai #67

Merged
merged 3 commits into from
Aug 19, 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
9 changes: 9 additions & 0 deletions guardrails_api/blueprints/guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ def openai_v1_chat_completions(guard_name: str):
decoded_guard_name = unquote_plus(guard_name)
guard_struct = guard_client.get_guard(decoded_guard_name)
guard = guard_struct
if guard_struct is None:
raise HttpError(
404,
"NotFound",
"A Guard with the name {guard_name} does not exist!".format(
guard_name=decoded_guard_name
),
)

if not isinstance(guard_struct, Guard):
guard: Guard = Guard.from_dict(guard_struct.to_dict())
stream = payload.get("stream", False)
Expand Down
2 changes: 1 addition & 1 deletion guardrails_api/utils/handle_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def decorator(*args, **kwargs):
return str(validation_error), 400
except HttpError as http_error:
logger.error(http_error)
traceback.print_exception(http_error)
traceback.print_exception(type(http_error), http_error, http_error.__traceback__)
return http_error.to_dict(), http_error.status
except HTTPException as http_exception:
logger.error(http_exception)
Expand Down
30 changes: 30 additions & 0 deletions tests/blueprints/test_guards.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,36 @@ def test_validate__call_throws_validation_error(mocker):

del os.environ["PGHOST"]

def test_openai_v1_chat_completions__raises_404(mocker):
from guardrails_api.blueprints.guards import openai_v1_chat_completions
os.environ["PGHOST"] = "localhost"
mock_guard = None

mock_request = MockRequest(
"POST",
json={
"messages": [{"role":"user", "content":"Hello world!"}],
},
headers={"x-openai-api-key": "mock-key"},
)

mocker.patch("flask.Blueprint", new=MockBlueprint)
mocker.patch("guardrails_api.blueprints.guards.request", mock_request)
mock_get_guard = mocker.patch(
"guardrails_api.blueprints.guards.guard_client.get_guard",
return_value=mock_guard,
)
mocker.patch("guardrails_api.blueprints.guards.CacheClient.set")

response = openai_v1_chat_completions("My%20Guard's%20Name")
assert response[1] == 404
assert response[0]["message"] == 'NotFound'


mock_get_guard.assert_called_once_with("My Guard's Name")

del os.environ["PGHOST"]

def test_openai_v1_chat_completions__call(mocker):
from guardrails_api.blueprints.guards import openai_v1_chat_completions
os.environ["PGHOST"] = "localhost"
Expand Down