High severity beginner · Fix: 2-5 min

RuntimeError

agentops.errors.RuntimeError: Session end not called

What this error means
AgentOps requires explicit session end calls; this error occurs when a session is not properly closed before program termination.

Stack trace

traceback
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
QUICK FIX
Wrap your agent operations in a try/finally block and call session.end() in the finally clause to guarantee session closure.

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

1

Forgetting to call the session end method after completing agent operations

✓ Fix

Always call the session end method explicitly after your agent finishes processing to properly close the session.

2

Exception or early return in code prevents reaching the session end call

✓ Fix

Use try/finally blocks to guarantee the session end method is called even if exceptions occur.

3

Using asynchronous or multi-threaded code without proper synchronization on session lifecycle

✓ Fix

Ensure session end calls are thread-safe and properly awaited in async contexts to avoid premature termination.

Code: broken vs fixed

Broken - triggers the error
python
from agentops import Agent

agent = Agent()
agent.start_session()
agent.run()
# Missing agent.end_session() call here, triggers RuntimeError
Fixed - works correctly
python
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.")
Added a try/finally block to ensure agent.end_session() is always called, preventing the RuntimeError by properly closing the session.

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.

Python 3.9+ · agentops >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.