Skip to content

Fix: Handle content moderation in responses.parse()#2850

Open
veeceey wants to merge 1 commit intoopenai:mainfrom
veeceey:fix/issue-2834-content-moderation-parse-error
Open

Fix: Handle content moderation in responses.parse()#2850
veeceey wants to merge 1 commit intoopenai:mainfrom
veeceey:fix/issue-2834-content-moderation-parse-error

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 8, 2026

Summary

Fixed responses.parse() to properly handle content moderation responses. When the API returns a plain-text refusal due to content filtering, the SDK now raises ContentFilterFinishReasonError instead of leaking raw Pydantic validation errors.

Problem

When using responses.parse() with structured output (via text_format parameter), content moderation can trigger plain-text refusals like "I'm sorry, but I cannot assist you with that request." These refusals are not valid JSON and cause pydantic_core.ValidationError to be raised directly, providing a poor developer experience.

As noted in the issue, this is an error-handling gap where moderation-triggered responses (expected runtime behavior) should be surfaced as higher-level SDK exceptions rather than low-level Pydantic validation errors.

Solution

Modified parse_text() to:

  1. Wrap JSON parsing in a try-except block
  2. Check for response.incomplete_details.reason == "content_filter" to detect moderation
  3. Raise ContentFilterFinishReasonError for content filter cases
  4. Raise APIResponseValidationError with helpful context for other parsing failures

Testing

Added comprehensive test cases:

  • ✅ Content filter detection and proper exception raising
  • ✅ General validation error handling with helpful error messages
  • ✅ Both sync and async client support

Impact

  • Better error messages for developers
  • Consistent exception handling across the SDK
  • Content moderation failures are now properly distinguished from schema validation issues

Fixes #2834

Fixed responses.parse() to properly handle content moderation responses.
When the API returns a plain-text refusal due to content filtering (e.g.,
"I'm sorry, but I cannot assist you with that request."), the SDK now
raises ContentFilterFinishReasonError instead of leaking raw Pydantic
validation errors.

For other JSON parsing failures, the SDK now raises APIResponseValidationError
with helpful context about what was expected vs what was received.

Changes:
- Modified parse_text() to accept response object and check for content_filter
- Added try-except around JSON parsing with proper error handling
- Added tests for both content filter and general validation error cases

Fixes openai#2834
@veeceey veeceey requested a review from a team as a code owner February 8, 2026 01:14
@veeceey
Copy link
Author

veeceey commented Feb 8, 2026

Ready for review and merge. All tests passing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Json parsing error with content moderation

1 participant