feat: pickle the residual non-portable cases (callables, dynamic Pydantic models, session save/load)

未关闭
#1,847 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
ai, backend

调研方向

Start with the listed files: mellea/stdlib/components/genstub.py, mellea/backends/tools.py, mellea/stdlib/context/compactor.py, mellea/stdlib/session.py, and mellea/core/base.py; read issue #1535 and test/core/test_pickle.py for the existing behavior. The work spans callables, dynamic Pydantic models, custom compactors, session state, and long linked contexts, each needing a serialization or rebind strategy. Done means these residual cases are handled or documented and covered by tests, with backend reattachment documented for session loading.

由索引模型根据 Issue 内容生成。

描述

enhancement p2
What feature would you like to be added?

Issue #1535 already made the common pickle cases work: ModelOutputThunk/ComputedModelOutputThunk now drop their in-flight _gen machinery and backend-native raw/logits on pickle, and — because a Context is just a linked list of its contents and a Component is a Protocol — this transitively made SimpleContext/ChatContext and simple/data-only components (e.g. Message, CBlock) picklable too, all verified in test/core/test_pickle.py.

This issue covers only the residual hard cases #1535 deliberately left out — the ones blocked by user- and backend-supplied objects rather than core machinery, each of which needs its own re-bind/rebuild strategy:

What to change

mellea/stdlib/components/genstub.py
Make GenerativeFunction/Function picklable — _func (raw callable, portable only if top-level) and _response_model (a dynamically-created Pydantic model, which does not pickle by default) need a __getstate__/__setstate__ or a rebuild-on-load strategy. Simple components already pickle.

mellea/backends/tools.py
Tool objects reach a pickled thunk through four paths, each needing a re-bind/rebuild strategy:

  1. ModelToolCall.func (the bound MelleaTool) on tool calls.
  2. ModelOption.TOOLS in _call.model_options and _generate_log.model_options, when a tool was offered but never called. A thunk with no tool calls therefore does not always pickle.
  3. _generate_log.extra["tools_available"], which holds the same tool objects.
  4. @tool-decorated functions. MelleaTool._call_tool is the original function, and pickle resolves it by qualified name. The decorator rebinds that name to the MelleaTool, so pickling fails with PicklingError: it's not the same object, even when the tool is never called. A plain function wrapped with MelleaTool.from_callable pickles fine.

Async tools also wrap the function in a local closure (tool_call in from_callable), which pickle cannot serialize by reference. Not yet verified; expected to fail regardless of the decorator.

Strategy: either pickle the tool by module + qualified name and re-bind on load, or drop the callable and keep only the JSON schema, requiring a re-bind before loaded tools can run. Leaning toward the first, since it keeps tool identity without serializing code.

mellea/stdlib/context/compactor.py
A ChatContext whose _compactor holds a non-top-level pin_predicate (lambda/closure) or an arbitrary custom InlineCompactor still silently blocks context pickling. Document and/or handle. (A ChatContext with a built-in compactor already pickles via #1535.)

mellea/stdlib/session.py
Add a high-level save()/load() session API (to/from a path) that serializes context + relevant state, backend excluded, with a documented re-attach step. New capability, not touched by #1535.

mellea/core/base.py
Consider _previous linked-list recursion limits for very long conversations (RecursionError risk) — an iterative __reduce__ if needed. The per-node pickling itself already works.

Why it's out of scope for #1535

#1535 targets the core picklability machinery (resetting _gen/raw/logits on the thunk, which cascades to Contexts and simple Components). Each item above depends on a caller/backend-supplied callable, a dynamically-built Pydantic model, or a new session-level API — distinct design work, and the issue itself notes it "may need to be converted into an epic."

Why is this feature needed?

Covers out of scope portion of #1535

Any alternative solution that you may want to share?

No response

Anything else we need to know?

No response

主要语言
Python
星标
1.8k
派生
161
平均合并
4 天 3 小时
30 天内合并 PR
45

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

generative-computing/mellea 的其他 Issue

查看 generative-computing/mellea 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。