I’m facing an issue while trying to run OpenAI Gym on my Jupyter notebook. I keep seeing an error that states there are too many values to unpack. I have four variables set up in my code, but I keep getting this confusing message. My setup is on an M1 Mac, and I don’t understand what’s going wrong.
Here is a basic outline of what I’ve attempted:
import gym
env = gym.make('CartPole-v1')
state = env.reset()
for episode in range(10):
action = env.action_space.sample()
next_state, reward, done, info = env.step(action) # This line triggers the error
if done:
state = env.reset()
break
Has anyone dealt with this kind of error before? Any help would be greatly appreciated!
The M1 Mac thing’s probably a red herring. This is just a classic Gym version mismatch.
I hit this same mess a few months back when upgrading a project. You’re running a newer Gym version that changed the return values from env.step().
Older versions returned 4 values:
next_state, reward, done, info = env.step(action)
Newer versions return 5 values:
next_state, reward, done, truncated, info = env.step(action)
The truncated flag tells you if the episode ended because of a time limit rather than the actual termination condition.
Quick fix is updating your unpacking. But check what Gym version you have first:
print(gym.__version__)
If you’re on 0.26 or higher, you need the 5-value unpacking. You could downgrade Gym if you want the old behavior, but I’d just update your code instead.
Also make sure you’re handling the reset properly - newer versions changed that too. Use state, info = env.reset() instead of just state = env.reset().
Same thing happened to me after updating Gym without checking the API changes first. You’re unpacking 5 values into 4 variables - Gym added a truncated boolean around version 0.26.
I’d run env.step(action) by itself and print what comes back. See exactly how many values you’re getting, then fix your unpacking.
The truncated thing is actually pretty handy - tells you if an episode ended naturally or got cut off by time limits. Won’t matter much in CartPole, but it’s useful for other environments.
Check your reset call too. I had to update mine for the new return format. Your M1 Mac isn’t the problem - it’s just the Gym version change.
This error hit me when I upgraded recently. OpenAI Gym changed their API - env.step() now returns five values instead of four. Your code expects four values but gets five, so Python throws the “too many values to unpack” error. The fifth value is truncated, which separates episodes that end naturally from ones cut off by time limits. I just updated all my step calls to handle the new truncated parameter and modified reset behavior. Your existing logic works fine once you fix the unpacking - add the extra variable and you’re good. This isn’t an M1 issue. It’s just a version compatibility problem that hits all platforms when you use newer Gym with older code.
I’ve hit this exact error tons of times with different Gym versions. Newer Gym returns 5 values from env.step() instead of 4.
Your code expects 4 values but gets 5 - that’s your unpacking error. The new format adds a truncated parameter:
next_state, reward, done, truncated, info = env.step(action)
Honestly though, manually debugging these version issues gets old fast. I’ve started automating my whole ML workflow - environment setup, training loops, everything.
Now I use automated pipelines that handle Gym interactions, version management, and training logic. When something breaks or changes, the automation adapts without me debugging individual lines.
I can run experiments across different environments, collect results, and deploy models without touching code. Makes these API changes way less painful.
Check out how you can automate your entire ML workflow: https://latenode.com