openai / openai/openai-python

Json parsing error with content moderation

Open
#2,834 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
31.6k
Forks
5.7k
Avg merge
1d 6h
Merged PRs (30d)
96

Description

Confirm this is an issue with the Python library and not an underlying OpenAI API
  • 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:

  1. Create client
  2. Submit problematic prompt to client.responses.parse with text_format= pydantic schema
  3. 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the client.responses.parse entry point and trace how a moderation refusal is handled before Pydantic validation, using the async Azure configuration and response format described in the issue. Done means the behavior is reproducibly characterized and the refusal raises the library's intended API response or moderation-related error instead of a raw pydantic_core.ValidationError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.