File tree Expand file tree Collapse file tree 3 files changed +40
-1
lines changed Expand file tree Collapse file tree 3 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -178,6 +178,15 @@ def openai_v1_chat_completions(guard_name: str):
178
178
decoded_guard_name = unquote_plus (guard_name )
179
179
guard_struct = guard_client .get_guard (decoded_guard_name )
180
180
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
+
181
190
if not isinstance (guard_struct , Guard ):
182
191
guard : Guard = Guard .from_dict (guard_struct .to_dict ())
183
192
stream = payload .get ("stream" , False )
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ def decorator(*args, **kwargs):
17
17
return str (validation_error ), 400
18
18
except HttpError as http_error :
19
19
logger .error (http_error )
20
- traceback .print_exception (http_error )
20
+ traceback .print_exception (type ( http_error ), http_error , http_error . __traceback__ )
21
21
return http_error .to_dict (), http_error .status
22
22
except HTTPException as http_exception :
23
23
logger .error (http_exception )
Original file line number Diff line number Diff line change @@ -614,6 +614,36 @@ def test_validate__call_throws_validation_error(mocker):
614
614
615
615
del os .environ ["PGHOST" ]
616
616
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
+
617
647
def test_openai_v1_chat_completions__call (mocker ):
618
648
from guardrails_api .blueprints .guards import openai_v1_chat_completions
619
649
os .environ ["PGHOST" ] = "localhost"
You can’t perform that action at this time.
0 commit comments