Repository: pydantic/pydantic-ai
File: tests/models/test_openai.py (Python)

Code before:
def test_azure_prompt_filter_error(allow_model_requests: None) -> None:
    body = {
        'error': {
            'code': 'content_filter',
            'message': 'The content was filtered.',
            'innererror': {
                'code': 'ResponsibleAIPolicyViolation',
                'content_filter_result': {'hate': {'filtered': True, 'severity': 'high'}},
            },
        }
    }

    mock_client = MockOpenAI.create_mock(
        APIStatusError(
            'content filter',
            response=httpx.Response(status_code=400, request=httpx.Request('POST', 'https://example.com/v1')),
            body=body,
        )
    )

    m = OpenAIChatModel('gpt-5-mini', provider=AzureProvider(openai_client=cast(AsyncAzureOpenAI, mock_client)))
    agent = Agent(m)

    try:
        agent.run_sync('bad prompt')
    except ContentFilterError as e:
        # Check that the exception was raised
        assert "Content filter triggered. Finish reason: 'content_filter'" in str(e)
        # Verify body contains the inner error details (serialized)
        assert 'ResponsibleAIPolicyViolation' in str(e.body)


def test_responses_azure_prompt_filter_error(allow_model_requests: None) -> None:
    mock_client = MockOpenAIResponses.create_mock(
        APIStatusError(
            'content filter',
            response=httpx.Response(status_code=400, request=httpx.Request('POST', 'https://example.com/v1')),
            body={'error': {'code': 'content_filter', 'message': 'The content was filtered.'}},
        )
    )

    m = OpenAIResponsesModel('gpt-5-mini', provider=AzureProvider(openai_client=cast(AsyncAzureOpenAI, mock_client)))
    agent = Agent(m)

    with pytest.raises(ContentFilterError, match=r"Content filter triggered. Finish reason: 'content_filter'"):
        agent.run_sync('bad prompt')


async def test_openai_response_filter_error_sync(allow_model_requests: None):
    c = completion_message(
        ChatCompletionMessage(content=None, role='assistant'),

Review comment (suggestion):
This test will succeed if no exception is raised :) `with pytest.raises(...) with exc_info` let's you access the `exc_info` after the `with` context.

Code after:


async def test_openai_model_settings_temperature_ignored_on_gpt_5(allow_model_requests: None, openai_api_key: str):
    m = OpenAIChatModel('gpt-5', provider=OpenAIProvider(api_key=openai_api_key))
    agent = Agent(m)

    result = await agent.run('What is the capital of France?', model_settings=ModelSettings(temperature=0.0))
    assert result.output == snapshot('Paris.')


async def test_openai_model_cerebras_provider(allow_model_requests: None, cerebras_api_key: str):
    m = OpenAIChatModel('llama3.3-70b', provider=CerebrasProvider(api_key=cerebras_api_key))
    agent = Agent(m)

    result = await agent.run('What is the capital of France?')
    assert result.output == snapshot('The capital of France is Paris.')


async def test_openai_model_cerebras_provider_qwen_3_coder(allow_model_requests: None, cerebras_api_key: str):
    class Location(TypedDict):
        city: str
        country: str

    m = OpenAIChatModel('qwen-3-coder-480b', provider=CerebrasProvider(api_key=cerebras_api_key))
    agent = Agent(m, output_type=Location)

    result = await agent.run('What is the capital of France?')
    assert result.output == snapshot({'city': 'Paris', 'country': 'France'})


async def test_openai_model_cerebras_provider_harmony(allow_model_requests: None, cerebras_api_key: str):
    m = OpenAIChatModel('gpt-oss-120b', provider=CerebrasProvider(api_key=cerebras_api_key))
    agent = Agent(m)

    result = await agent.run('What is the capital of France?')
    assert result.output == snapshot('The capital of France is **Paris**.')


def test_deprecated_openai_model(openai_api_key: str):
    with pytest.warns(DeprecationWarning):
        from pydantic_ai.models.openai import OpenAIModel  # type: ignore[reportDeprecated]

        provider = OpenAIProvider(api_key=openai_api_key)
        OpenAIModel('gpt-4o', provider=provider)  # type: ignore[reportDeprecated]


async def test_cache_point_filtering(allow_model_requests: None):
    """Test that CachePoint is filtered out in OpenAI Chat Completions requests."""
    c = completion_message(ChatCompletionMessage(content='response', role='assistant'))
    mock_client = MockOpenAI.create_mock(c)
    m = OpenAIChatModel('gpt-4o', provider=OpenAIProvider(openai_client=mock_client))