1+ from typing import Optional
2+
13from openai import AsyncOpenAI
24
35from agents .models ._openai_shared import get_default_openai_client
68from .session import SessionABC
79
810
9- async def start_openai_session (openai_client : AsyncOpenAI | None = None ) -> str :
11+ async def start_openai_session (openai_client : Optional [ AsyncOpenAI ] = None ) -> str :
1012 _openai_client = openai_client
1113 if openai_client is None :
1214 _openai_client = get_default_openai_client () or AsyncOpenAI ()
1315
14- response = await _openai_client .conversations .create (items = [])
16+ response = await _openai_client .conversations .create (items = []) # type: ignore [union-attr]
1517 return response .id
1618
1719
1820class OpenAISession (SessionABC ):
1921 def __init__ (
2022 self ,
2123 session_id : str | None = None ,
22- openai_client : AsyncOpenAI | None = None ,
24+ openai_client : Optional [ AsyncOpenAI ] = None ,
2325 ):
24- self .session_id = session_id
26+ # this implementation allows to set this value later
27+ self .session_id = session_id # type: ignore
2528 self .openai_client = openai_client
2629 if self .openai_client is None :
2730 self .openai_client = get_default_openai_client () or AsyncOpenAI ()
@@ -35,27 +38,29 @@ async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]:
3538
3639 all_items = []
3740 if limit is None :
38- async for item in self .openai_client .conversations .items .list (
41+ async for item in self .openai_client .conversations .items .list ( # type: ignore [union-attr]
3942 conversation_id = self .session_id ,
4043 order = "asc" ,
4144 ):
45+ # calling model_dump() to make this serializable
4246 all_items .append (item .model_dump ())
4347 else :
44- async for item in self .openai_client .conversations .items .list (
48+ async for item in self .openai_client .conversations .items .list ( # type: ignore [union-attr]
4549 conversation_id = self .session_id ,
4650 limit = limit ,
4751 order = "desc" ,
4852 ):
53+ # calling model_dump() to make this serializable
4954 all_items .append (item .model_dump ())
5055 if limit is not None and len (all_items ) >= limit :
5156 break
5257 all_items .reverse ()
5358
54- return all_items
59+ return all_items # type: ignore
5560
5661 async def add_items (self , items : list [TResponseInputItem ]) -> None :
5762 await self ._ensure_session_id ()
58- await self .openai_client .conversations .items .create (
63+ await self .openai_client .conversations .items .create ( # type: ignore [union-attr]
5964 conversation_id = self .session_id ,
6065 items = items ,
6166 )
@@ -65,15 +70,15 @@ async def pop_item(self) -> TResponseInputItem | None:
6570 items = await self .get_items (limit = 1 )
6671 if not items :
6772 return None
68- await self .openai_client .conversations .items .delete (
73+ await self .openai_client .conversations .items .delete ( # type: ignore [union-attr]
6974 conversation_id = self .session_id ,
70- item_id = items [0 ]. id ,
75+ item_id = str ( items [0 ][ "id" ]), # type: ignore
7176 )
7277 return items [0 ]
7378
7479 async def clear_session (self ) -> None :
7580 await self ._ensure_session_id ()
76- await self .openai_client .conversations .delete (
81+ await self .openai_client .conversations .delete ( # type: ignore [union-attr]
7782 conversation_id = self .session_id ,
7883 )
79- self .session_id = None
84+ self .session_id = None # type: ignore
0 commit comments