Skip to content

RuntimeError: aclose(): asynchronous generator is already running #503

Closed
@pors

Description

@pors

Describe the bug

I see this error after an async streaming call to the completion end-point:

an error occurred during closing of asynchronous generator <async_generator object aiohttp_session at 0x1029de110>
asyncgen: <async_generator object aiohttp_session at 0x1029de110>
RuntimeError: aclose(): asynchronous generator is already running

To Reproduce

Run the code below. The completion_text is printed and below that the error above is shown.

Code snippets

import asyncio
from types import AsyncGeneratorType
from dotenv import load_dotenv
import os
import openai

load_dotenv()

openai.api_key = os.getenv("OPENAI_API_KEY")

async def test_completion_async_stream():
    completion = await openai.Completion.acreate(
        model="text-davinci-003",
        prompt="Hello!",
        stream=True,
    )

    # Assert response body
    assert isinstance(completion, AsyncGeneratorType)

    collected_chunks = []
    completion_text = ''
    # iterate through the stream, if it breaks, the test failed
    async for chunk in completion:
        collected_chunks.append(chunk)
        finish_reason = chunk['choices'][0]['finish_reason'] # type: ignore
        if finish_reason is not None:
            break
        chunk_text = chunk['choices'][0]['text'] # type: ignore
        completion_text += chunk_text  # append the text
    print(completion_text)

if __name__ == "__main__":
    asyncio.run(test_completion_async_stream())

OS

macOS 13.4 (22F66)

Python version

3.11.4

Library version

0.27.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixed in v1Issues addressed by the v1 beta

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions