TypeError: cannot pickle 'IntVar' object

119 views
Skip to first unread message

Alberto Manzini

unread,
Mar 27, 2025, 8:01:39 AM3/27/25
to or-tools-discuss
Hi everyone, I was trying to pickle a model but this error was raised

TypeError: cannot pickle 'IntVar' object

Could it be an issue with protobuf version?

Here is my pip freeze
numpy==1.26.0
ortools==9.12.4544
pandas==2.2.0
protobuf==5.29.4

I tried both on python=3.8.10 and 3.12.9

Here is the toy model I'm using

from ortools.sat.python import cp_model
import pickle

model = cp_model.CpModel()

list_presences = []
list_intervals = []
for i in range(11):
  start = model.NewIntVar(0, 10, name=f'start_{i}')
  end = model.NewIntVar(0, 10, name=f'end_{i}')
  duration = model.NewIntVar(1, 2, name=f'duration_{i}')
  presence = model.NewBoolVar(name=f'presence_{i}')
  interval = model.NewOptionalIntervalVar(start, duration, end, presence, name=f'interval_{i}')
  list_presences.append(presence)
  list_intervals.append(interval)

model.AddNoOverlap(list_intervals)
model.Add(sum(list_presences) == len(list_presences)).OnlyEnforceIf(presence.Not())

with open(r'test.pkl', 'wb') as f:
  pickle.dump(model, f)

Laurent Perron

unread,
Mar 27, 2025, 8:02:45 AM3/27/25
to or-tools-discuss
Fixed on main. 

--Laurent


--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/or-tools-discuss/c7940c04-0d3d-4dda-a052-e6ef2755b22cn%40googlegroups.com.

Alberto Manzini

unread,
Mar 27, 2025, 8:13:02 AM3/27/25
to or-tools-discuss
Wow, thanks

Can I pip install it with some command or should I clone the repo, build the library and get the wheel for main branch?

Laurent Perron

unread,
Mar 27, 2025, 8:14:32 AM3/27/25
to or-tools...@googlegroups.com
you need to build. 
Use the v99bugfix branch
Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00



Alberto Manzini

unread,
Mar 27, 2025, 8:17:52 AM3/27/25
to or-tools-discuss
Ok got it, thanks

Zhiwei Feng

unread,
Feb 27, 2026, 8:59:15 PM (7 days ago) Feb 27
to or-tools-discuss
Hi Laurent,

I still have the same error:TypeError: cannot pickle 'IntVar' object on new version: Name: ortools Version: 9.14.6206.
we can easily reproduce the error with above toy model


Laurent Perron

unread,
Feb 28, 2026, 2:53:28 AM (7 days ago) Feb 28
to or-tools...@googlegroups.com
pretty sure this is fixed on 9.15
Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00


Zhiwei Feng

unread,
Feb 28, 2026, 2:58:16 AM (7 days ago) Feb 28
to or-tools-discuss
with 9.15.6755, i still got error:
Traceback (most recent call last):
  File "/Users/User/Documents/Git/Engine/pyxis-algorithm/test/test_pickle.py", line 21, in <module>
    pickle.dump(model, f)
    ~~~~~~~~~~~^^^^^^^^^^
TypeError: cannot pickle 'CpModel' object

test_pickle.py is exactly the above test code

Laurent Perron

unread,
Feb 28, 2026, 4:01:40 AM (7 days ago) Feb 28
to or-tools...@googlegroups.com
I have added support for copy/deepcopy, not pickle



--
--Laurent

Reply all
Reply to author
Forward
0 new messages