RuntimeError
agentops.errors.RuntimeError: Session end not called
Stack trace
Traceback (most recent call last):
File "main.py", line 42, in <module>
agent.run()
File "/usr/local/lib/python3.9/site-packages/agentops/agent.py", line 88, in run
raise RuntimeError("Session end not called")
agentops.errors.RuntimeError: Session end not called Why it happens
AgentOps manages conversational or task sessions that must be explicitly ended to release resources and finalize logs. If the developer forgets to call the session end method, the runtime raises this error to prevent resource leaks and inconsistent state.
Detection
Monitor your code to ensure every session start is paired with a session end call; add logging around session lifecycle methods to catch missing end calls before runtime errors occur.
Causes & fixes
Forgetting to call the session end method after completing agent operations
Always call the session end method explicitly after your agent finishes processing to properly close the session.
Exception or early return in code prevents reaching the session end call
Use try/finally blocks to guarantee the session end method is called even if exceptions occur.
Using asynchronous or multi-threaded code without proper synchronization on session lifecycle
Ensure session end calls are thread-safe and properly awaited in async contexts to avoid premature termination.
Code: broken vs fixed
from agentops import Agent
agent = Agent()
agent.start_session()
agent.run()
# Missing agent.end_session() call here, triggers RuntimeError import os
from agentops import Agent
agent = Agent()
agent.start_session()
try:
agent.run()
finally:
agent.end_session() # Added to ensure session ends properly
print("Agent session completed successfully.") Workaround
If you cannot refactor immediately, catch the RuntimeError and call session end in the except block as a fallback to avoid crashes.
Prevention
Design your agent workflows to always pair session start and end calls, preferably using context managers or decorators to automate session lifecycle management.