fix: guard None refusal in ItemHelpers.extract_last_content#3428
Open
rmotgi1227 wants to merge 1 commit into
Open
fix: guard None refusal in ItemHelpers.extract_last_content#3428rmotgi1227 wants to merge 1 commit into
rmotgi1227 wants to merge 1 commit into
Conversation
PR openai#3394 added ``or ""`` for ``ResponseOutputText.text`` in the ``extract_last_content`` method but left the ``ResponseOutputRefusal`` branch unguarded. ``ResponseOutputRefusal.refusal`` is typed as ``str`` per the Responses API schema, but the same provider gateways (e.g. LiteLLM) and ``model_construct`` paths during streaming that surface ``None`` for ``.text`` can also surface ``None`` for ``.refusal``. Without the guard, callers that rely on the ``-> str`` return type receive ``None`` and may crash downstream. Add the same ``or ""`` coercion to the refusal branch and add regression tests mirroring the ones added for the text branch. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PR #3394 added an
or \"\"guard forResponseOutputText.textinextract_last_contentbut left theResponseOutputRefusalbranch unguarded:ResponseOutputRefusal.refusalis typed asstrper the Responses API schema, but the same provider gateways (e.g. LiteLLM) andmodel_constructpaths during streaming that surfaceNonefor.textcan also surfaceNonefor.refusal. Without the guard, callers that rely on the-> strreturn type ofextract_last_contentreceiveNoneand may crash downstream.The fix adds the same
or ""coercion to the refusal branch, matching the precedent from #3394 for text andextract_refusal(which already usescontent_item.refusal or ""at items.py:739).Test plan
Added two regression tests in
tests/utils/test_pretty_print_and_items.pymirroring the structure of the tests added in #3394:test_extract_last_content_returns_empty_string_for_none_refusal— verifies the method returns""(notNone) whenResponseOutputRefusal.refusalisNonetest_extract_last_content_returns_refusal_normally— verifies unaffected happy-path behaviourIssue number
Discovered while auditing for the same guard-None pattern that #3394 and #3375 fixed.
Checks
make lintandmake format