Json parsing error with content moderation
Confirm this is an issue with the Python library and not an underlying OpenAI API
- [x] This is an issue with the Python library
Describe the bug
Hello,
I've seen an example where I'm submitting to the responses.parse endpoint, and I get back a pydantic validation error. Upon inspection, it looks like the model is returning "I'm sorry, but I cannot assist you with that request.", which obviously fails to json parsing step.
I'd expect it to raise something like a APIResponseValidationError (if not something more specifically related to moderation), instead of a pydantic_core.ValidationError.
To Reproduce
I've found it very difficult to reproduce as the original example is from internal business documents I cannot share, and my attempts to trigger that response have failed.
The basic steps are:
- Create
client - Submit problematic prompt to
client.responses.parsewithtext_format=pydantic schema - Observe error
For context, I am using the async client, through Microsoft AI Foundry, with chatgpt-4.1-mini, and the content filter is DefaultV2. The Foundry API version is 2025-03-01-preview.
Code snippets
from openai import AsyncAzureOpenAI
credential = DefaultAzureCredential(logging_enable=True)
openai_client = AsyncAzureOpenAI(
api_version="2025-03-01-preview",
azure_endpoint=xxx,
azure_ad_token_provider=get_bearer_token_provider(
credential,
"https://cognitiveservices.azure.com/.default",
),
)
await openai_client.responses.parse(
input=prompt,
text_format=pydantic_schema
)
OS
Windows 11
Python version
Python 3.12.3
Library version
openai 2.15.0
Thanks for the detailed report — this does look like a real SDK-level issue.
responses.parse() appears to assume the model will always return JSON-conformant output, but when content moderation is triggered the model instead returns a plain-text refusal. That refusal then falls through to Pydantic validation, resulting in a low-level pydantic_core.ValidationError.
From a consumer perspective, this feels like an error-handling gap in the Python client. Moderation-triggered responses are expected runtime behavior and should likely be surfaced as a higher-level SDK exception (e.g. a moderation-specific error or a wrapped APIResponseValidationError), rather than leaking raw schema validation errors.
Even if the underlying API response is valid, responses.parse() probably needs a guard to detect non-JSON / refusal responses before attempting Pydantic validation.
Happy to help test a fix or provide a minimal repro if useful.
Thanks for the report! The SDK already handles responses explicitly marked as refusals. If ordinary output text doesn’t match the requested JSON schema, it raises ValidationError so your application can handle it. We’re closing this as expected SDK behavior. If an explicitly marked refusal still causes a parsing error on the latest SDK, please share a minimal example with sensitive data removed and we can reopen.