AgentOpsSessionNotInitializedError
agentops.exceptions.AgentOpsSessionNotInitializedError
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
agentops_client.run_task(task)
File "/usr/local/lib/python3.10/site-packages/agentops/client.py", line 88, in run_task
raise AgentOpsSessionNotInitializedError("AgentOps session not initialized. Call initialize_session() first.")
agentops.exceptions.AgentOpsSessionNotInitializedError: AgentOps session not initialized. Call initialize_session() first. Why it happens
AgentOps requires an explicit session initialization step to set up authentication, configuration, and context. If you attempt to run tasks or operations without calling initialize_session(), the client lacks necessary state and raises this error.
Detection
Check that your code calls initialize_session() on the AgentOps client before any task execution. Add logging around session setup to confirm initialization status.
Causes & fixes
Forgot to call initialize_session() before running AgentOps tasks
Add a call to initialize_session() with required parameters before invoking any task methods on the AgentOps client.
Session initialization failed silently due to missing or invalid credentials
Verify environment variables or config files provide valid credentials and handle exceptions during initialize_session() to catch failures early.
Calling AgentOps client methods in a different thread or process without session context
Ensure session initialization is done in the same runtime context or pass session tokens explicitly when using concurrency.
Code: broken vs fixed
from agentops import AgentOpsClient
client = AgentOpsClient()
result = client.run_task('my_task') # This line triggers AgentOpsSessionNotInitializedError
print(result) import os
from agentops import AgentOpsClient
os.environ['AGENTOPS_API_KEY'] = os.environ.get('AGENTOPS_API_KEY', 'your_api_key_here') # Use environment variable for API key
client = AgentOpsClient()
client.initialize_session(api_key=os.environ['AGENTOPS_API_KEY']) # Session initialization added
result = client.run_task('my_task')
print(result) # Now works without error Workaround
Wrap calls to AgentOps client methods in try/except AgentOpsSessionNotInitializedError and prompt for session initialization or retry after setting up the session.
Prevention
Always initialize the AgentOps session at application startup or before any AgentOps operations, and validate session state before task execution to avoid runtime errors.