TypeError: Type is not msgpack serializable: module #21

Open
opened 2026-02-16 07:16:32 -05:00 by yindo · 0 comments
Owner

Originally created by @datalee on GitHub (Jan 4, 2026).

when i run the example code:

import math

from langchain_core.tools import tool

def add(a: float, b: float) -> float:
    """Add two numbers together."""
    return a + b

def multiply(a: float, b: float) -> float:
    """Multiply two numbers together."""
    return a * b

def divide(a: float, b: float) -> float:
    """Divide two numbers."""
    return a / b

def subtract(a: float, b: float) -> float:
    """Subtract two numbers."""
    return a - b

def sin(a: float) -> float:
    """Take the sine of a number."""
    return math.sin(a)

def cos(a: float) -> float:
    """Take the cosine of a number."""
    return math.cos(a)

def radians(a: float) -> float:
    """Convert degrees to radians."""
    return math.radians(a)

def exponentiation(a: float, b: float) -> float:
    """Raise one number to the power of another."""
    return a**b

def sqrt(a: float) -> float:
    """Take the square root of a number."""
    return math.sqrt(a)

def ceil(a: float) -> float:
    """Round a number up to the nearest integer."""
    return math.ceil(a)

tools = [
    add,
    multiply,
    divide,
    subtract,
    sin,
    cos,
    radians,
    exponentiation,
    sqrt,
    ceil,
]
import builtins
import contextlib
import io
from typing import Any


def eval(code: str, _locals: dict[str, Any]) -> tuple[str, dict[str, Any]]:
    # Store original keys before execution
    original_keys = set(_locals.keys())

    try:
        with contextlib.redirect_stdout(io.StringIO()) as f:
            exec(code, builtins.__dict__, _locals)
        result = f.getvalue()
        if not result:
            result = "<code ran, no output printed to stdout>"
    except Exception as e:
        result = f"Error during execution: {repr(e)}"

    # Determine new variables created during execution
    new_keys = set(_locals.keys()) - original_keys
    new_vars = {key: _locals[key] for key in new_keys}
    return result, new_vars

from langchain.chat_models import init_chat_model
from langgraph_codeact import create_codeact
from langgraph.checkpoint.memory import MemorySaver

model = init_chat_model("claude-3-7-sonnet-latest", model_provider="anthropic")

code_act = create_codeact(model, tools, eval)
agent = code_act.compile(checkpointer=MemorySaver())


messages = [{
    "role": "user",
    "content": "2013年成立的公司,今年多少岁了"
}]
for typ, chunk in agent.stream(
    {"messages": messages},
    stream_mode=["values", "messages"],
    config={"configurable": {"thread_id": 1}},
):
    if typ == "messages":
        print(chunk[0].content, end="")
    elif typ == "values":
        print("\n\n---answer---\n\n", chunk)

got the error:

\langgraph\checkpoint\serde\jsonplus.py", line 676, in _msgpack_enc
    return ormsgpack.packb(data, default=_msgpack_default, option=_option)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Type is not msgpack serializable: module
Originally created by @datalee on GitHub (Jan 4, 2026). when i run the example code: ```python import math from langchain_core.tools import tool def add(a: float, b: float) -> float: """Add two numbers together.""" return a + b def multiply(a: float, b: float) -> float: """Multiply two numbers together.""" return a * b def divide(a: float, b: float) -> float: """Divide two numbers.""" return a / b def subtract(a: float, b: float) -> float: """Subtract two numbers.""" return a - b def sin(a: float) -> float: """Take the sine of a number.""" return math.sin(a) def cos(a: float) -> float: """Take the cosine of a number.""" return math.cos(a) def radians(a: float) -> float: """Convert degrees to radians.""" return math.radians(a) def exponentiation(a: float, b: float) -> float: """Raise one number to the power of another.""" return a**b def sqrt(a: float) -> float: """Take the square root of a number.""" return math.sqrt(a) def ceil(a: float) -> float: """Round a number up to the nearest integer.""" return math.ceil(a) tools = [ add, multiply, divide, subtract, sin, cos, radians, exponentiation, sqrt, ceil, ] import builtins import contextlib import io from typing import Any def eval(code: str, _locals: dict[str, Any]) -> tuple[str, dict[str, Any]]: # Store original keys before execution original_keys = set(_locals.keys()) try: with contextlib.redirect_stdout(io.StringIO()) as f: exec(code, builtins.__dict__, _locals) result = f.getvalue() if not result: result = "<code ran, no output printed to stdout>" except Exception as e: result = f"Error during execution: {repr(e)}" # Determine new variables created during execution new_keys = set(_locals.keys()) - original_keys new_vars = {key: _locals[key] for key in new_keys} return result, new_vars from langchain.chat_models import init_chat_model from langgraph_codeact import create_codeact from langgraph.checkpoint.memory import MemorySaver model = init_chat_model("claude-3-7-sonnet-latest", model_provider="anthropic") code_act = create_codeact(model, tools, eval) agent = code_act.compile(checkpointer=MemorySaver()) messages = [{ "role": "user", "content": "2013年成立的公司,今年多少岁了" }] for typ, chunk in agent.stream( {"messages": messages}, stream_mode=["values", "messages"], config={"configurable": {"thread_id": 1}}, ): if typ == "messages": print(chunk[0].content, end="") elif typ == "values": print("\n\n---answer---\n\n", chunk) ``` got the error: ``` \langgraph\checkpoint\serde\jsonplus.py", line 676, in _msgpack_enc return ormsgpack.packb(data, default=_msgpack_default, option=_option) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: Type is not msgpack serializable: module ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph-codeact#21