Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NoSessionException when user does not init() #307

Merged
merged 11 commits into from
Jul 23, 2024
Merged

NoSessionException when user does not init() #307

merged 11 commits into from
Jul 23, 2024

Conversation

HowieG
Copy link
Contributor

@HowieG HowieG commented Jul 21, 2024

📥 Pull Request

📘 Description
The sessions overhaul made it such that we are expecting a session when user never calls init(). For crew this shows up as @track_agent erroring bc "No session". safe_get_session() raises an error when no session exists, but no session should exist bc user never inits. Affects more than @track_agent

🎯 Goal
only check for session when init() called (global is_initialized = True)

🔍 Additional Context
User complaining in Discord and I recreated in examples/crew/job_posting.py

🧪 Testing
TODO

Thank you for your contribution to Agentops!


NoSessionException Traceback (most recent call last)
File ~/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages/agentops/meta_client.py:53, in handle_exceptions..wrapper(self, *args, **kwargs)
52 try:
---> 53 return method(self, *args, **kwargs)
54 except Exception as e:

File ~/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages/agentops/client.py:566, in Client._safe_get_session(self)
565 if len(self._sessions) == 0:
--> 566 raise NoSessionException("No session exists")
568 elif len(self._sessions) > 1:

NoSessionException: No session exists

During handling of the above exception, another exception occurred:

IndexError Traceback (most recent call last)
File ~/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages/agentops/meta_client.py:53, in handle_exceptions..wrapper(self, *args, **kwargs)
52 try:
---> 53 return method(self, *args, **kwargs)
54 except Exception as e:

File ~/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages/agentops/client.py:455, in Client.create_agent(self, name, agent_id, session)
453 else:
454 # if no session passed, assume single session
--> 455 session = self._safe_get_session()
456 session.create_agent(name=name, agent_id=agent_id)

File ~/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages/agentops/meta_client.py:61, in handle_exceptions..wrapper(self, *args, **kwargs)
57 if config is not None:
58 type(self).send_exception_to_server(
59 e,
60 self.config._api_key,
---> 61 self._sessions[0], # TODO: find which session caused exception
62 )
63 raise e

IndexError: list index out of range

During handling of the above exception, another exception occurred:

IndexError Traceback (most recent call last)
Cell In[5], line 15
12 specific_benefits = input("What are specific_benefits you offer?\n")
14 # Create Agents
---> 15 researcher_agent = agents.research_agent()
16 writer_agent = agents.writer_agent()
17 review_agent = agents.review_agent()

Cell In[3], line 13
12 def research_agent(self):
---> 13 return Agent(
14 role="Research Analyst",
15 goal="Analyze the company website and provided description to extract insights on culture, values, and specific needs.",
16 tools=[web_search_tool, serper_dev_tool],
17 backstory="Expert in analyzing company cultures and identifying key values and needs from various sources, including websites and brief descriptions.",
18 verbose=True,
19 )

File ~/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages/agentops/agent.py:26, in track_agent..decorator..new_init(self, *args, **kwargs)
23 if session is not None:
24 self.agent_ops_session_id = session.session_id
---> 26 Client().create_agent(
27 name=self.agent_ops_agent_name,
28 agent_id=self.agent_ops_agent_id,
29 session=session,
30 )
31 except AttributeError as e:
32 logger.warning(
33 "Failed to track an agent. This often happens if agentops.init() was not "
34 "called before initializing an agent with the @track_agent decorator."
35 )

File ~/Desktop/agentops/AgentOps-AI/agentops/env/lib/python3.12/site-packages/agentops/meta_client.py:61, in handle_exceptions..wrapper(self, *args, **kwargs)
56 config = getattr(self, "config", None)
57 if config is not None:
58 type(self).send_exception_to_server(
59 e,
60 self.config._api_key,
---> 61 self._sessions[0], # TODO: find which session caused exception
62 )
63 raise e

IndexError: list index out of range

@HowieG HowieG changed the title NoSessionException when user does not init() DRAFT: NoSessionException when user does not init() Jul 21, 2024
@bboynton97
Copy link
Contributor

this fixes it in the SDK but we still need to determine why we were being called at all if nothing agentops was in the script

@bboynton97
Copy link
Contributor

as is, this breaks a lot of tests. i think this is bnot sending the first call to agentops

Copy link

codecov bot commented Jul 21, 2024

Codecov Report

Attention: Patch coverage is 60.00000% with 14 lines in your changes missing coverage. Please review.

Flag Coverage Δ
unittests 45.07% <60.00%> (-0.19%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
agentops/state.py 100.00% <100.00%> (ø)
agentops/__init__.py 82.25% <75.00%> (ø)
agentops/meta_client.py 88.57% <75.00%> (-5.18%) ⬇️
agentops/agent.py 21.87% <33.33%> (+1.18%) ⬆️
agentops/client.py 63.88% <47.36%> (-2.37%) ⬇️

... and 1 file with indirect coverage changes

@bboynton97
Copy link
Contributor

I refactored how state is being managed and it fixed the tests. this is now working as expected with agentops alone and with crew

Copy link
Contributor

@siyangqiu siyangqiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some questions about design. We can skip if this is urgent

tests/test_record_function.py Outdated Show resolved Hide resolved
agentops/state.py Show resolved Hide resolved
@HowieG
Copy link
Contributor Author

HowieG commented Jul 22, 2024

Is this good to go @bboynton97 ?

@HowieG HowieG changed the title DRAFT: NoSessionException when user does not init() NoSessionException when user does not init() Jul 23, 2024
@HowieG
Copy link
Contributor Author

HowieG commented Jul 23, 2024

Tested my change:
Screenshot 2024-07-22 at 5 35 16 PM

@HowieG HowieG enabled auto-merge (squash) July 23, 2024 00:37
@HowieG HowieG merged commit a2f2f7f into main Jul 23, 2024
9 of 10 checks passed
@HowieG HowieG deleted the crew-no-session branch July 23, 2024 00:37
"If multiple sessions exist, you must use session.function(). Example: session.add_tags(...) instead "
"of agentops.add_tags(...). More info: "
"https://docs.agentops.ai/v1/concepts/core-concepts#session-management"
f"Multiple sessions detected. You must use session.{calling_function}(). More info: https://docs.agentops.ai/v1/concepts/core-concepts#session-management"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@Tulleb
Copy link

Tulleb commented Jul 27, 2024

Also fixed #308 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants