|
4 | 4 | # This source code is licensed under the terms described in the LICENSE file in |
5 | 5 | # the root directory of this source tree. |
6 | 6 | import pytest |
7 | | -from openai import NotFoundError, OpenAI |
| 7 | +from openai import APIStatusError, NotFoundError, OpenAI |
8 | 8 |
|
9 | 9 | from ogx.core.library_client import OGXAsLibraryClient |
10 | 10 |
|
11 | 11 |
|
| 12 | +def _create_guardrailed_response_or_skip(compat_client: OpenAI, **kwargs): |
| 13 | + """Create a guardrailed response or skip if moderation isn't configured.""" |
| 14 | + try: |
| 15 | + return compat_client.responses.create(**kwargs) |
| 16 | + except APIStatusError as exc: |
| 17 | + if exc.status_code == 503 and "moderation_endpoint" in str(exc).lower(): |
| 18 | + pytest.skip("Guardrails require moderation_endpoint in this test environment") |
| 19 | + raise |
| 20 | + |
| 21 | + |
12 | 22 | @pytest.mark.parametrize( |
13 | 23 | "stream", |
14 | 24 | [ |
@@ -309,7 +319,8 @@ def test_input_guardrails(compat_client, text_model_id, stream): |
309 | 319 | # Note: This test assumes a moderation endpoint is configured in the test environment. |
310 | 320 | inappropriate_message = "How can I hurt someone?" |
311 | 321 |
|
312 | | - response = compat_client.responses.create( |
| 322 | + response = _create_guardrailed_response_or_skip( |
| 323 | + compat_client, |
313 | 324 | model=text_model_id, |
314 | 325 | input=[ |
315 | 326 | { |
@@ -352,7 +363,8 @@ def test_output_guardrails_unsafe_content(compat_client, text_model_id, stream): |
352 | 363 | pytest.skip("OpenAI client is required until responses API exists in ogx-client") |
353 | 364 |
|
354 | 365 | # Use an unsafe prompt that should be blocked by guardrail evaluation |
355 | | - response = compat_client.responses.create( |
| 366 | + response = _create_guardrailed_response_or_skip( |
| 367 | + compat_client, |
356 | 368 | model=text_model_id, |
357 | 369 | input=[ |
358 | 370 | { |
@@ -394,7 +406,8 @@ def test_output_guardrails_safe_content(compat_client, text_model_id, stream): |
394 | 406 | pytest.skip("OpenAI client is required until responses API exists in ogx-client") |
395 | 407 |
|
396 | 408 | # Use a safe prompt that should pass guardrail evaluation |
397 | | - response = compat_client.responses.create( |
| 409 | + response = _create_guardrailed_response_or_skip( |
| 410 | + compat_client, |
398 | 411 | model=text_model_id, |
399 | 412 | input=[ |
400 | 413 | { |
@@ -434,7 +447,8 @@ def test_guardrails_with_tools(compat_client, text_model_id): |
434 | 447 | if not isinstance(compat_client, OpenAI): |
435 | 448 | pytest.skip("OpenAI client is required until responses API exists in ogx-client") |
436 | 449 |
|
437 | | - response = compat_client.responses.create( |
| 450 | + response = _create_guardrailed_response_or_skip( |
| 451 | + compat_client, |
438 | 452 | model=text_model_id, |
439 | 453 | input=[ |
440 | 454 | { |
|
0 commit comments