From 0f11a9d033dfb14753c6694adf71771a897b9078 Mon Sep 17 00:00:00 2001 From: Howard Gil Date: Wed, 3 Apr 2024 01:03:40 -0700 Subject: [PATCH] Setting session to none if server does not return 200 for /sessions --- agentops/client.py | 6 +++++- agentops/worker.py | 13 +++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/agentops/client.py b/agentops/client.py index 8c2fa188..4001d4d1 100644 --- a/agentops/client.py +++ b/agentops/client.py @@ -212,7 +212,11 @@ def start_session(self, tags: Optional[List[str]] = None, config: Optional[Confi self._session = Session(uuid4(), tags or self._tags, host_env=get_host_env()) self._worker = Worker(config or self.config) - self._worker.start_session(self._session) + start_session_result = self._worker.start_session(self._session) + if not start_session_result: + self._session = None + return logging.warning("AgentOps: Cannot start session") + logging.info('View info on this session at https://app.agentops.ai/drilldown?session_id={}' .format(self._session.session_id)) diff --git a/agentops/worker.py b/agentops/worker.py index d409ee10..6b7320ef 100644 --- a/agentops/worker.py +++ b/agentops/worker.py @@ -49,10 +49,15 @@ def start_session(self, session: Session) -> None: "session": session.__dict__ } serialized_payload = json.dumps(filter_unjsonable(payload)).encode("utf-8") - HttpClient.post(f'{self.config.endpoint}/sessions', - serialized_payload, - self.config.api_key, - self.config.parent_key) + res = HttpClient.post(f'{self.config.endpoint}/sessions', + serialized_payload, + self.config.api_key, + self.config.parent_key) + + if res.code != 200: + return False + + return True def end_session(self, session: Session) -> None: self.stop_flag.set()