Skip to content

fix: handle null text values in Response.output_text#3057

Open
akhilc08 wants to merge 2 commits intoopenai:mainfrom
akhilc08:fix/output-text-null-handling
Open

fix: handle null text values in Response.output_text#3057
akhilc08 wants to merge 2 commits intoopenai:mainfrom
akhilc08:fix/output-text-null-handling

Conversation

@akhilc08
Copy link
Copy Markdown

@akhilc08 akhilc08 commented Apr 3, 2026

Summary

  • Fix TypeError in Response.output_text when content items have text: null
  • Add a None check before appending text to the list, so null text values are skipped instead of causing "".join() to fail
  • Add regression tests for both mixed null/non-null and all-null text scenarios

Problem

Some models return output_text content items where the text field is null. When output_text iterates over these items, it appends None to the list and then "".join(texts) raises:

TypeError: sequence item 0: expected str instance, NoneType found

Fix

Changed the condition in the output_text property from:

if content.type == "output_text":

to:

if content.type == "output_text" and content.text is not None:

Closes #3011

Test plan

  • Added test_output_text_with_null_text — verifies mixed null/non-null text items produce correct output
  • Added test_output_text_all_null_text — verifies all-null text items return empty string
  • AST validation passes
  • Fix logic verified independently

🤖 Generated with Claude Code

akhilc08 and others added 2 commits April 3, 2026 13:44
When OPENAI_BASE_URL is set to an empty string, os.environ.get()
returns "" rather than None, so the `if base_url is None` fallback
to the default API endpoint never triggers. Change the check to
`if not base_url` so that both None and empty string fall through
to the default https://api.openai.com/v1 endpoint.

Applied to both OpenAI (sync) and AsyncOpenAI classes.

Closes openai#2927

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Skip content items where text is None to prevent TypeError when
joining strings. Some models return output_text content items with
null text fields.

Closes openai#3011

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@akhilc08 akhilc08 requested a review from a team as a code owner April 3, 2026 17:45
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 56e915d7a0

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

if base_url is None:
base_url = os.environ.get("OPENAI_BASE_URL")
if base_url is None:
if not base_url:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Do not default explicit empty base_url values

Using if not base_url here changes semantics for explicitly provided values: OpenAI(base_url="") (and the async constructor’s equivalent branch) now silently targets https://api.openai.com/v1 instead of surfacing an invalid configuration. This can turn a caller/config bug into unintended traffic to production, which is harder to detect than a startup failure. The fallback should apply only when base_url is missing (None/unset env), not when the caller passed an explicit empty string.

Useful? React with 👍 / 👎.

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.

Response.output_text fails to handle null text values in content items

1 participant