@@ -52,8 +52,11 @@ def __init__(
5252 # Create a session if it does not exist yet
5353 if session is None :
5454 logger .debug ("session_id=<%s> | session not found, creating new session" , self .session_id )
55+ self ._is_new_session = True
5556 session = Session (session_id = session_id , session_type = SessionType .AGENT )
5657 session_repository .create_session (session )
58+ else :
59+ self ._is_new_session = False
5760
5861 self .session = session
5962
@@ -170,7 +173,11 @@ def initialize(self, agent: "Agent", **kwargs: Any) -> None:
170173 raise SessionException ("The `agent_id` of an agent must be unique in a session." )
171174 self ._latest_agent_message [agent .agent_id ] = None
172175
173- session_agent = self .session_repository .read_agent (self .session_id , agent .agent_id )
176+ # Skip read_agent call for new sessions since no agents can exist yet
177+ if self ._is_new_session :
178+ session_agent = None
179+ else :
180+ session_agent = self .session_repository .read_agent (self .session_id , agent .agent_id )
174181
175182 if session_agent is None :
176183 logger .debug (
@@ -299,7 +306,12 @@ def initialize_multi_agent(self, source: "MultiAgentBase", **kwargs: Any) -> Non
299306 source: Multi-agent source object to restore state into
300307 **kwargs: Additional keyword arguments for future extensibility.
301308 """
302- state = self .session_repository .read_multi_agent (self .session_id , source .id , ** kwargs )
309+ # Skip read_multi_agent call for new sessions since no multi-agents can exist yet
310+ if self ._is_new_session :
311+ state = None
312+ else :
313+ state = self .session_repository .read_multi_agent (self .session_id , source .id , ** kwargs )
314+
303315 if state is None :
304316 self .session_repository .create_multi_agent (self .session_id , source , ** kwargs )
305317 else :
@@ -317,7 +329,11 @@ def initialize_bidi_agent(self, agent: "BidiAgent", **kwargs: Any) -> None:
317329 raise SessionException ("The `agent_id` of an agent must be unique in a session." )
318330 self ._latest_agent_message [agent .agent_id ] = None
319331
320- session_agent = self .session_repository .read_agent (self .session_id , agent .agent_id )
332+ # Skip read_agent call for new sessions since no agents can exist yet
333+ if self ._is_new_session :
334+ session_agent = None
335+ else :
336+ session_agent = self .session_repository .read_agent (self .session_id , agent .agent_id )
321337
322338 if session_agent is None :
323339 logger .debug (
0 commit comments