Skip to content

Streaming A Background Response #2828

@jpiabrantes

Description

@jpiabrantes

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

In the past, I was streaming responses an sometimes I would get disconnected from OpenAI's server with an exception httpx.ReadError, httpx.ReadTimeout or httpx.RemoteProtocolError exception.

Then I read your documentation on Streaming a background response but now I often get another error:

openai.BadRequestError: This response can no longer be streamed because it is more than 5 minutes old

Here's my code:

state = {
        "cursor": None,
        "response_id": None,
    }
    max_retries = 3

    async with RealtimeDB() as realtime_db:
        for attempt in range(max_retries):
            try:
                if state["cursor"] is None or state["response_id"] is None:
                    # First attempt, or failed before getting response_id - start fresh
                    stream = await aopenai_client.responses.create(
                        model=model,
                        input=input,
                        instructions=instructions,
                        previous_response_id=previous_response_id,
                        tools=tools,
                        reasoning=reasoning,
                        stream=True,
                        background=True,
                    )
                    async for event in stream:
                        await _handle_event(event, state, realtime_db, thought_path, realtime_path)
                else:
                    # Resume from cursor
                    async with aopenai_client.responses.stream(
                        response_id=state["response_id"],
                        starting_after=state["cursor"],
                    ) as stream:
                        async for event in stream:
                            await _handle_event(event, state, realtime_db, thought_path, realtime_path)

                return state

            except (httpx.ReadError, httpx.ReadTimeout, httpx.RemoteProtocolError):
                if attempt == max_retries - 1:
                    raise
                await asyncio.sleep(2**attempt)

Note the error happens ~ten times a day out of ~1000 messages a day.

To Reproduce

state = {
        "cursor": None,
        "response_id": None,
    }
    max_retries = 3

    async with RealtimeDB() as realtime_db:
        for attempt in range(max_retries):
            try:
                if state["cursor"] is None or state["response_id"] is None:
                    # First attempt, or failed before getting response_id - start fresh
                    stream = await aopenai_client.responses.create(
                        model=model,
                        input=input,
                        instructions=instructions,
                        previous_response_id=previous_response_id,
                        tools=tools,
                        reasoning=reasoning,
                        stream=True,
                        background=True,
                    )
                    async for event in stream:
                        await _handle_event(event, state, realtime_db, thought_path, realtime_path)
                else:
                    # Resume from cursor
                    async with aopenai_client.responses.stream(
                        response_id=state["response_id"],
                        starting_after=state["cursor"],
                    ) as stream:
                        async for event in stream:
                            await _handle_event(event, state, realtime_db, thought_path, realtime_path)

                return state

            except (httpx.ReadError, httpx.ReadTimeout, httpx.RemoteProtocolError):
                if attempt == max_retries - 1:
                    raise
                await asyncio.sleep(2**attempt)

Code snippets

OS

macOS

Python version

3.12.2

Library version

2.15.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions