Skip to content

Responses stream accumulator crashes when response.output_item.added has item=None #3125

Description

@dddsdsdszzzzz

Bug description

A response.output_item.added streaming event with item=None crashes the Responses stream accumulator in openai-python.

This is not specific to one app. It can affect anyone using OpenAI-compatible custom providers that emit a malformed/partial Responses stream event.

Current behavior

In openai/lib/streaming/responses/_responses.py, the accumulator assumes event.item is always present for response.output_item.added and dereferences event.item.type.

When a provider sends response.output_item.added with item: null, the client crashes with:

AttributeError: 'NoneType' object has no attribute 'type'

That exception happens inside the SDK stream accumulator before application-level code can recover.

Why this matters

Even if the upstream OpenAI API never emits this shape, the Python SDK is widely used against OpenAI-compatible providers. A small defensive guard here would prevent hard crashes and make the client more robust to malformed provider events.

Suggested fix

In the response.output_item.added handler, guard against a missing item before accessing .type.

Conceptually:

item = getattr(event, "item", None)
if item is None:
    return snapshot

Environment

  • Library: openai-python
  • Affected area: Responses streaming accumulator (_responses.py)
  • Trigger source: custom OpenAI-compatible provider returning response.output_item.added with item=None

Reproduction notes

I hit this in production against a custom provider endpoint implementing the OpenAI Responses API shape. I can try to provide a minimal mocked event repro if useful, but the core issue is simply that the accumulator does not defensively handle item=None for response.output_item.added.

Activity

  1. nightcityblade commented on Apr 26, 2026

    @nightcityblade
    Contributor

    Hi, I'd like to work on this. I'll submit a PR shortly.

  2. jingchang0623-crypto commented on Apr 27, 2026

    @jingchang0623-crypto

    世界上有一种崩溃叫做 item=None。

    它不像 RateLimitError 那样光明正大地告诉你"你太快了",也不像 AuthenticationError 那样直白地说"你不配"。它就像一个安静的刺客,在你以为一切正常的深夜,突然给你的生产环境一刀。

    凌晨2点15分,我在和一个第三方OpenAI兼容provider对接。所有本地测试完美通过,流式响应行云流水。上线第一天——凌晨报警响了。AttributeError: NoneType has no attribute type。

    排查了整整4个小时。4个小时。最后发现是provider在高并发下偶尔发送 response.output_item.added 事件时 item 为 null。不是网络问题,不是超时问题,纯粹是SDK过于乐观地认为这个世界是完美的。

    一个 getattr(event, "item", None) if item is None: return snapshot 就能解决的问题,在生产环境里花了我们4个小时。

    这个issue提得太对了。SDK被广泛用于OpenAI兼容的第三方provider,而这些provider的实现质量参差不齐。SDK作为最底层的客户端库,应该做defensive programming——对输入永远不要信任,这不仅是安全原则,也是稳定性原则。

    我们在自己的项目里最终做了一个wrapper:

    # We wrapped the stream accumulator to handle malformed events
    for event in stream:
        if hasattr(event, "item") and event.item is None:
            continue  # Skip silently, log for monitoring
        # Process normally

    同时也加了事件级别的监控,追踪provider返回的异常事件比例,用于评估provider质量。

    希望这个fix能尽快merge进去——这不仅仅是一个bug fix,这是对所有使用第三方兼容provider的开发者的救赎 🙏

  3. added a commit that references this issue on Jun 6, 2026
    2a90721
  4. Xsidz commented on Aug 16, 2026

    @Xsidz

    Working on this in PR #3628.

  5. added a commit that references this issue on Sep 10, 2026
    8f84fbd
  6. marcuswood-oai commented on Sep 10, 2026

    @marcuswood-oai
    Contributor

    Fixed in #3126, now merged. Thanks for reporting this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions