Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
pors opened this issue Jun 26, 2023 · 2 comments
Closed

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

pors opened this issue Jun 26, 2023 · 2 comments
Labels
bug Something isn't working fixed in v1 Issues addressed by the v1 beta

Comments

@pors
Copy link

pors commented Jun 26, 2023

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

@pors pors added the bug Something isn't working label Jun 26, 2023
@jamesjohnson1025
Copy link

hi, did you find any alternative way to fix the issue ?.

@RobertCraigie RobertCraigie added the fixed in v1 Issues addressed by the v1 beta label Oct 18, 2023
@RobertCraigie
Copy link
Collaborator

This should be fixed in our upcoming v1 beta as we've moved away from aiohttp to httpx!

safa0 pushed a commit to safa0/openai-agents-python that referenced this issue Apr 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed in v1 Issues addressed by the v1 beta
Projects
None yet
Development

No branches or pull requests

3 participants