fix(responses): handle null text values in output_text property#3050
fix(responses): handle null text values in output_text property#3050Vitalcheffe wants to merge 2 commits intoopenai:mainfrom
Conversation
Closes openai#3011 - The output_text property now skips content items where text is null instead of raising TypeError during string concatenation - Updated ResponseOutputText.text type from str to Optional[str] to accurately reflect the API contract where text can be null - Added regression tests for null-only and mixed null/text responses Impact: Fixes crashes when models return null text fields in output_text content items, which occurs with certain models like openai/gpt-oss-safeguard-120b.
|
Note on the type change: ResponseOutputText.text moving to Optional[str] is technically a widening change — existing code that already handles the output_text property (via the joined string) is unaffected. The type change only impacts code that directly accesses .text on individual content items, which the SDK docs don’t expose as a primary API. Happy to add a deprecation note or keep str with an internal cast if preferred. |
|
Also worth noting on the silent skip behavior: null text items are skipped silently to match the best-effort aggregation pattern used elsewhere in the SDK (e.g. |
…ignature - Keeps ResponseOutputText.text as str (no breaking change) - Guards output_text property against null values at aggregation time - Updates tests to reflect corrected approach
What
Fixes
Response.output_textto handle null text values in content items, preventing aTypeErrorwhen concatenating output text.Why
When the API returns a response where an
output_textcontent item hastext: null(observed withopenai/gpt-oss-safeguard-120band potentially other models), accessingresponse.output_textraisesTypeError: sequence item 0: expected str instance, NoneType found.See: #3011
How
response.py: Added acontent.text is not Noneguard before appending to the texts list in theoutput_textproperty. Content items with null text are silently skipped.response_output_text.py: Changedtext: strtotext: Optional[str] = Noneto accurately reflect that the API can return null for this field.test_responses.py: Added two regression tests:test_output_text_with_null_text— verifies no crash when all text is nulltest_output_text_mixed_null_and_text— verifies null items are skipped while valid text is concatenatedTesting
ruff checkpasses on all modified filesChecklist
str, null items are simply skipped)Closes #3011