99
1010
1111async def start_openai_conversations_session (openai_client : Optional [AsyncOpenAI ] = None ) -> str :
12- _openai_client = openai_client
12+ _maybe_openai_client = openai_client
1313 if openai_client is None :
14- _openai_client = get_default_openai_client () or AsyncOpenAI ()
14+ _maybe_openai_client = get_default_openai_client () or AsyncOpenAI ()
15+ # this never be None here
16+ _openai_client : AsyncOpenAI = _maybe_openai_client # type: ignore [assignment]
1517
16- response = await _openai_client .conversations .create (items = []) # type: ignore [union-attr]
18+ response = await _openai_client .conversations .create (items = [])
1719 return response .id
1820
1921
22+ _EMPTY_SESSION_ID = ""
23+
24+
2025class OpenAIConversationsSession (SessionABC ):
2126 def __init__ (
2227 self ,
@@ -25,28 +30,30 @@ def __init__(
2530 openai_client : Optional [AsyncOpenAI ] = None ,
2631 ):
2732 # this implementation allows to set this value later
28- self .session_id = session_id # type: ignore
29- self .openai_client = openai_client
30- if self .openai_client is None :
31- self .openai_client = get_default_openai_client () or AsyncOpenAI ()
33+ self .session_id = session_id or _EMPTY_SESSION_ID
34+ _openai_client = openai_client
35+ if _openai_client is None :
36+ _openai_client = get_default_openai_client () or AsyncOpenAI ()
37+ # this never be None here
38+ self .openai_client : AsyncOpenAI = _openai_client
3239
3340 async def _ensure_session_id (self ) -> None :
34- if self .session_id is None :
41+ if self .session_id == _EMPTY_SESSION_ID :
3542 self .session_id = await start_openai_conversations_session (self .openai_client )
3643
3744 async def get_items (self , limit : Optional [int ] = None ) -> list [TResponseInputItem ]:
3845 await self ._ensure_session_id ()
3946
4047 all_items = []
4148 if limit is None :
42- async for item in self .openai_client .conversations .items .list ( # type: ignore [union-attr]
49+ async for item in self .openai_client .conversations .items .list (
4350 conversation_id = self .session_id ,
4451 order = "asc" ,
4552 ):
4653 # calling model_dump() to make this serializable
4754 all_items .append (item .model_dump ())
4855 else :
49- async for item in self .openai_client .conversations .items .list ( # type: ignore [union-attr]
56+ async for item in self .openai_client .conversations .items .list (
5057 conversation_id = self .session_id ,
5158 limit = limit ,
5259 order = "desc" ,
@@ -61,7 +68,7 @@ async def get_items(self, limit: Optional[int] = None) -> list[TResponseInputIte
6168
6269 async def add_items (self , items : list [TResponseInputItem ]) -> None :
6370 await self ._ensure_session_id ()
64- await self .openai_client .conversations .items .create ( # type: ignore [union-attr]
71+ await self .openai_client .conversations .items .create (
6572 conversation_id = self .session_id ,
6673 items = items ,
6774 )
@@ -71,15 +78,15 @@ async def pop_item(self) -> Optional[TResponseInputItem]:
7178 items = await self .get_items (limit = 1 )
7279 if not items :
7380 return None
74- await self . openai_client . conversations . items . delete ( # type: ignore [union-attr ]
75- conversation_id = self .session_id ,
76- item_id = str ( items [ 0 ][ "id" ]), # type: ignore
81+ item_id : str = str ( items [ 0 ][ "id" ]) # type: ignore [typeddict-item ]
82+ await self .openai_client . conversations . items . delete (
83+ conversation_id = self . session_id , item_id = item_id
7784 )
7885 return items [0 ]
7986
8087 async def clear_session (self ) -> None :
8188 await self ._ensure_session_id ()
82- await self .openai_client .conversations .delete ( # type: ignore [union-attr]
89+ await self .openai_client .conversations .delete (
8390 conversation_id = self .session_id ,
8491 )
85- self .session_id = None # type: ignore
92+ self .session_id = _EMPTY_SESSION_ID
0 commit comments