Skip to content

Conversation

@PupiBott
Copy link

@PupiBott PupiBott commented Nov 5, 2025

Problem

When finish_reason == "content_filter", the SDK raises ContentFilterFinishReasonError without providing access to the underlying ChatCompletion object. This makes it impossible to:

  • Track usage/costs via completion.usage
  • Log completion ID for debugging
  • Access any response metadata

This is inconsistent with LengthFinishReasonError, which includes completion as a public attribute.

Solution

Modified ContentFilterFinishReasonError to accept and expose completion parameter, matching the pattern established by LengthFinishReasonError.

Changes:

  1. Updated ContentFilterFinishReasonError constructor to accept completion parameter
  2. Updated parsing call site to pass completion
  3. Updated streaming call site to pass completion_snapshot
  4. Added comprehensive tests

Backward Compatibility

Existing try/except blocks will continue to work. The only breaking change would be code that manually instantiates ContentFilterFinishReasonError() without parameters, which is not a realistic use case.

Related Issues

Example Usage

try:
    result = await client.beta.chat.completions.parse(...)
except ContentFilterFinishReasonError as e:
    # Now you can access:
    print(f"Completion ID: {e.completion.id}")
    print(f"Tokens used: {e.completion.usage.total_tokens}")
    print(f"Model: {e.completion.model}")

Testing

Added two new tests:

  • test_parse_content_filter_includes_completion: Verifies completion access and usage info
  • test_parse_content_filter_backward_compatibility: Ensures existing code still works

All tests pass including existing LengthFinishReasonError tests.

Add 'completion' attribute to ContentFilterFinishReasonError for parity
with LengthFinishReasonError, allowing developers to access usage info
and response metadata when content filter is triggered.

Changes:
- Modified ContentFilterFinishReasonError class to accept completion parameter
- Updated _parsing/_completions.py to pass completion object
- Updated streaming/_completions.py to pass completion_snapshot
- Added comprehensive tests verifying completion access and backward compatibility

Fixes openai#2690
@PupiBott PupiBott requested a review from a team as a code owner November 5, 2025 03:15
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.

Include response information in ContentFilterFinishReasonError

1 participant