Skip to content

Commit e4fcafd

Browse files
authored
Merge pull request #67 from guardrails-ai/dtam/fix_openai_missing_guard_handling
add handling to have informative 404 when a guard doesnt exist on openai
2 parents af02055 + ac8d8c0 commit e4fcafd

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

guardrails_api/blueprints/guards.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ def openai_v1_chat_completions(guard_name: str):
178178
decoded_guard_name = unquote_plus(guard_name)
179179
guard_struct = guard_client.get_guard(decoded_guard_name)
180180
guard = guard_struct
181+
if guard_struct is None:
182+
raise HttpError(
183+
404,
184+
"NotFound",
185+
"A Guard with the name {guard_name} does not exist!".format(
186+
guard_name=decoded_guard_name
187+
),
188+
)
189+
181190
if not isinstance(guard_struct, Guard):
182191
guard: Guard = Guard.from_dict(guard_struct.to_dict())
183192
stream = payload.get("stream", False)

guardrails_api/utils/handle_error.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def decorator(*args, **kwargs):
1717
return str(validation_error), 400
1818
except HttpError as http_error:
1919
logger.error(http_error)
20-
traceback.print_exception(http_error)
20+
traceback.print_exception(type(http_error), http_error, http_error.__traceback__)
2121
return http_error.to_dict(), http_error.status
2222
except HTTPException as http_exception:
2323
logger.error(http_exception)

tests/blueprints/test_guards.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,36 @@ def test_validate__call_throws_validation_error(mocker):
614614

615615
del os.environ["PGHOST"]
616616

617+
def test_openai_v1_chat_completions__raises_404(mocker):
618+
from guardrails_api.blueprints.guards import openai_v1_chat_completions
619+
os.environ["PGHOST"] = "localhost"
620+
mock_guard = None
621+
622+
mock_request = MockRequest(
623+
"POST",
624+
json={
625+
"messages": [{"role":"user", "content":"Hello world!"}],
626+
},
627+
headers={"x-openai-api-key": "mock-key"},
628+
)
629+
630+
mocker.patch("flask.Blueprint", new=MockBlueprint)
631+
mocker.patch("guardrails_api.blueprints.guards.request", mock_request)
632+
mock_get_guard = mocker.patch(
633+
"guardrails_api.blueprints.guards.guard_client.get_guard",
634+
return_value=mock_guard,
635+
)
636+
mocker.patch("guardrails_api.blueprints.guards.CacheClient.set")
637+
638+
response = openai_v1_chat_completions("My%20Guard's%20Name")
639+
assert response[1] == 404
640+
assert response[0]["message"] == 'NotFound'
641+
642+
643+
mock_get_guard.assert_called_once_with("My Guard's Name")
644+
645+
del os.environ["PGHOST"]
646+
617647
def test_openai_v1_chat_completions__call(mocker):
618648
from guardrails_api.blueprints.guards import openai_v1_chat_completions
619649
os.environ["PGHOST"] = "localhost"

0 commit comments

Comments
 (0)