Skip to content

fix: tolerate None text in ResponseOutputText content items#2883

Merged
seratch merged 2 commits intoopenai:mainfrom
beveradb:fix/extract-text-none-safe
Apr 15, 2026
Merged

fix: tolerate None text in ResponseOutputText content items#2883
seratch merged 2 commits intoopenai:mainfrom
beveradb:fix/extract-text-none-safe

Conversation

@beveradb
Copy link
Copy Markdown
Contributor

Summary

ItemHelpers.extract_text does text += content_item.text, which raises TypeError: can only concatenate str (not "NoneType") to str when a ResponseOutputText content item has text=None.

Although the Responses API schema declares text: str, this surfaces in practice when output items are assembled via model_construct during streaming or forwarded through provider gateways (e.g. LiteLLM partial responses). The crash propagates up from inside execute_tools_and_side_effects (called from run_single_turn_streamed), aborting the entire agent turn.

We hit this in production on an agent backed by a LiteLLM → Azure OpenAI Responses gateway: ~57 user-facing failures per day, all surfacing as the same unhelpful "Something went wrong" message. The full traceback ends in:

File ".../agents/run_internal/turn_resolution.py", line 618, in execute_tools_and_side_effects
    ItemHelpers.extract_text(message_items[-1].raw_item) if message_items else None
File ".../agents/items.py", line 686, in extract_text
    text += content_item.text
TypeError: can only concatenate str (not "NoneType") to str

Fix: coerce with or "" so a missing .text contributes nothing instead of crashing. Behavior on well-formed messages is unchanged (covered by the existing test_extract_text_concatenates_all_text_segments).

Test plan

  • uv run pytest tests/test_items_helpers.py — all 26 tests pass, including the new regression test.
  • New test test_extract_text_tolerates_none_text_content covers:
    • single ResponseOutputText with text=None → returns None (per return text or None)
    • mixed content with None text + real text → returns the real text, both orderings.
  • make lint and make format clean.

Issue number

N/A — opening a fresh report.

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation — none needed; method behavior on the documented happy path is unchanged.
  • I've run make lint and make format
  • I've made sure tests pass

`ItemHelpers.extract_text` does `text += content_item.text`, which raises
`TypeError: can only concatenate str (not "NoneType") to str` when a
`ResponseOutputText` content item has `text=None`.

Although the Responses API schema declares `text: str`, this surfaces in
practice when output items are assembled via `model_construct` during
streaming or forwarded through provider gateways (e.g. LiteLLM partial
responses). The crash propagates from inside
`execute_tools_and_side_effects`, aborting the agent turn.

Coerce with `or ""` so callers see an empty contribution instead of
crashing. Adds a regression test covering single None-text content items
and mixed (None + real text) content lists.
@github-actions github-actions bot added bug Something isn't working feature:core labels Apr 14, 2026
@seratch seratch added this to the 0.14.x milestone Apr 15, 2026
@seratch
Copy link
Copy Markdown
Member

seratch commented Apr 15, 2026

@codex review again

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@seratch seratch changed the title fix(items): tolerate None text in ResponseOutputText content items fix: tolerate None text in ResponseOutputText content items Apr 15, 2026
@seratch seratch merged commit 48ad4aa into openai:main Apr 15, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working feature:core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants