Skip to content

Commit db19a43

Browse files
committed
Revert "🐛 fixed asyncio bugs (openai#584)"
This reverts commit 532d3df.
1 parent 284c179 commit db19a43

File tree

1 file changed

+11
-30
lines changed

1 file changed

+11
-30
lines changed

openai/api_requestor.py

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import threading
77
import time
88
import warnings
9+
from contextlib import asynccontextmanager
910
from json import JSONDecodeError
1011
from typing import (
11-
AsyncContextManager,
1212
AsyncGenerator,
13+
AsyncIterator,
1314
Callable,
1415
Dict,
1516
Iterator,
@@ -367,9 +368,8 @@ async def arequest(
367368
request_id: Optional[str] = None,
368369
request_timeout: Optional[Union[float, Tuple[float, float]]] = None,
369370
) -> Tuple[Union[OpenAIResponse, AsyncGenerator[OpenAIResponse, None]], bool, str]:
370-
ctx = AioHTTPSession()
371+
ctx = aiohttp_session()
371372
session = await ctx.__aenter__()
372-
result = None
373373
try:
374374
result = await self.arequest_raw(
375375
method.lower(),
@@ -383,9 +383,6 @@ async def arequest(
383383
)
384384
resp, got_stream = await self._interpret_async_response(result, stream)
385385
except Exception:
386-
# Close the request before exiting session context.
387-
if result is not None:
388-
result.release()
389386
await ctx.__aexit__(None, None, None)
390387
raise
391388
if got_stream:
@@ -396,15 +393,10 @@ async def wrap_resp():
396393
async for r in resp:
397394
yield r
398395
finally:
399-
# Close the request before exiting session context. Important to do it here
400-
# as if stream is not fully exhausted, we need to close the request nevertheless.
401-
result.release()
402396
await ctx.__aexit__(None, None, None)
403397

404398
return wrap_resp(), got_stream, self.api_key
405399
else:
406-
# Close the request before exiting session context.
407-
result.release()
408400
await ctx.__aexit__(None, None, None)
409401
return resp, got_stream, self.api_key
410402

@@ -778,22 +770,11 @@ def _interpret_response_line(
778770
return resp
779771

780772

781-
class AioHTTPSession(AsyncContextManager):
782-
def __init__(self):
783-
self._session = None
784-
self._should_close_session = False
785-
786-
async def __aenter__(self):
787-
self._session = openai.aiosession.get()
788-
if self._session is None:
789-
self._session = await aiohttp.ClientSession().__aenter__()
790-
self._should_close_session = True
791-
792-
return self._session
793-
794-
async def __aexit__(self, exc_type, exc_value, traceback):
795-
if self._session is None:
796-
raise RuntimeError("Session is not initialized")
797-
798-
if self._should_close_session:
799-
await self._session.__aexit__(exc_type, exc_value, traceback)
773+
@asynccontextmanager
774+
async def aiohttp_session() -> AsyncIterator[aiohttp.ClientSession]:
775+
user_set_session = openai.aiosession.get()
776+
if user_set_session:
777+
yield user_set_session
778+
else:
779+
async with aiohttp.ClientSession() as session:
780+
yield session

0 commit comments

Comments
 (0)