Remove Optlang object from COBRApy model

46 views
Skip to first unread message

Nicholas Y

unread,
Nov 22, 2025, 2:31:10 PM11/22/25
to cobra pie
Hi,

My name is Nick, and I have a question regarding the COBRApy Python package. 

I understand that COBRApy models contain an optlang object within them. I was wondering if it is possible to remove this optlang object from COBRApy models, or to create models without the optlang object. 

The reason for this is I would like to be able to represent a very large genome scale model in COBRApy, and I want to be able to pickle this model for fast save/reload. However, because COBRApy models contain optlang objects, I am running into issues loading the model from a pickle file. Is it possible to remove an optlang object from a COBRApy model and if so, how do I do it? Thanks!

Best regards,
Nick Yousefi

Matthias König

unread,
Nov 24, 2025, 2:57:34 AM11/24/25
to cobra pie
Hi Nick,
if your problem is the serialization you should try the dill library
https://github.com/uqfoundation/dill
It can handle many uses cases pickle struggles with.
Best Matthias

Nicholas Y

unread,
Nov 26, 2025, 9:35:02 AM11/26/25
to cobra pie
Hi Matthias,

Thank you for your suggestion. I will try it and see if it works.

I still do want to confirm, though, whether there is an official way to remove the optlang solver from COBRApy models. Ideally, I would still like to remove it if possible. If this is not possible with COBRApy's current design, that is fine, but I would appreciate if you could please confirm if that is the case.

If there is no official way to remove the optlang object, I am considering using a fork of COBRApy in which I modify the code to remove the optlang object. I was wondering if this would be feasible and if you could provide any insights as to what parts of the code I would need to change to achieve this.

Thank you very much for your assistance. It is much appreciated.

Best regards,
Nick Yousefi

Nicholas Y

unread,
Dec 5, 2025, 12:04:59 AM12/5/25
to cobra pie
Hi,

Just wanted to follow up on this since I hadn't received a reply. I would appreciate if you could at least clarify whether removing the optlang object is officially supported, even if you aren't sure about the other parts of my question. Thanks!

Best regards,
Nick Yousefi

Joshua Lerman

unread,
Dec 5, 2025, 12:21:53 AM12/5/25
to Nicholas Y, cobra pie
i suppose one way is to revert to a much older version of cobrapy.   optlang wasn’t always a dependency.

josh 

--
You received this message because you are subscribed to the Google Groups "cobra pie" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cobra-pie+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/cobra-pie/6fdacf78-aceb-4cd3-8f69-77387b2c4338n%40googlegroups.com.

ch.d...@gmail.com

unread,
Feb 20, 2026, 2:42:48 PM (9 days ago) Feb 20
to cobra pie
Optlang is usually quite efficient in pickling. We use it in MICOM to save models and it is quite fast even for models with 1 million reactions or more. You wouldn't really see much speed-up anyway because recreating the optlang solver instance from scratch is not that fast either. So if you want to actually simulate the model after unpacking you would curve to rebuild it.

Nicholas Y

unread,
Feb 22, 2026, 4:15:52 PM (7 days ago) Feb 22
to cobra pie
Hi,

Thanks for your replies! Yes, I was working on a project where I was building custom community models using COBRApy, but I was having an issue with pickling. The models themselves would pickle fine, but when I tried to load them from the .pkl files, I would run into issues with them hanging and getting stuck. The code would not progress, nor would it error when trying to load them. It would just stay stuck for hours until I quit the program. I assumed it was because of serialization issues with the optlang object, but I could have been wrong about this.

In any case, I was able to find a workaround for my application that fixed the issue.

Thanks for your assistance!

Best regards,
Nicholas

Reply all
Reply to author
Forward
0 new messages