|
| 1 | +""" |
| 2 | +Regression test for Issue #113: Interrupt objects not properly deserialized |
| 3 | +
|
| 4 | +When using interrupt() with RedisSaver, Interrupt objects are serialized to |
| 5 | +dictionaries but not reconstructed back to Interrupt objects on deserialization. |
| 6 | +
|
| 7 | +This causes AttributeError: 'dict' object has no attribute 'id' when trying |
| 8 | +to resume execution with Command(resume=...). |
| 9 | +
|
| 10 | +The error occurs in LangGraph's _pending_interrupts() method when it tries to |
| 11 | +access value[0].id, but value[0] is a dict instead of an Interrupt object. |
| 12 | +""" |
| 13 | + |
| 14 | +import operator |
| 15 | +from typing import Annotated, TypedDict |
| 16 | +from uuid import uuid4 |
| 17 | + |
| 18 | +import pytest |
| 19 | +from langchain_core.messages import AnyMessage |
| 20 | +from langgraph.graph import END, START, StateGraph |
| 21 | +from langgraph.types import Command, Interrupt, interrupt |
| 22 | + |
| 23 | +from langgraph.checkpoint.redis import RedisSaver |
| 24 | + |
| 25 | + |
| 26 | +class AgentState(TypedDict): |
| 27 | + """State for the test agent.""" |
| 28 | + |
| 29 | + messages: Annotated[list[AnyMessage], operator.add] |
| 30 | + |
| 31 | + |
| 32 | +def review_node(state: AgentState): |
| 33 | + """Node that interrupts for review.""" |
| 34 | + random_str = str(uuid4()) |
| 35 | + print(f"Generated string: {random_str}") |
| 36 | + print("-------- entry interrupt --------") |
| 37 | + |
| 38 | + # This creates an Interrupt object that needs to be serialized |
| 39 | + user_input = interrupt({"test": "data"}) |
| 40 | + |
| 41 | + print(f"Received input: {user_input.get('test')}") |
| 42 | + print("-------- exit interrupt --------") |
| 43 | + return {"messages": [random_str]} |
| 44 | + |
| 45 | + |
| 46 | +def test_interrupt_serialization_roundtrip(redis_url: str) -> None: |
| 47 | + """ |
| 48 | + Test that Interrupt objects are properly serialized and deserialized. |
| 49 | +
|
| 50 | + This is a unit test that directly tests the serializer behavior. |
| 51 | + """ |
| 52 | + from langgraph.checkpoint.redis.jsonplus_redis import JsonPlusRedisSerializer |
| 53 | + |
| 54 | + serializer = JsonPlusRedisSerializer() |
| 55 | + |
| 56 | + # Create an Interrupt object |
| 57 | + original_interrupt = Interrupt(value={"test": "data"}, resumable=True) |
| 58 | + |
| 59 | + # Serialize it |
| 60 | + serialized = serializer.dumps(original_interrupt) |
| 61 | + |
| 62 | + # Deserialize it |
| 63 | + deserialized = serializer.loads(serialized) |
| 64 | + |
| 65 | + # This should be an Interrupt object, not a dict |
| 66 | + assert isinstance(deserialized, Interrupt), ( |
| 67 | + f"Expected Interrupt object, got {type(deserialized)}. " |
| 68 | + f"This causes AttributeError when LangGraph tries to access attributes" |
| 69 | + ) |
| 70 | + assert deserialized.value == {"test": "data"} |
| 71 | + assert deserialized.resumable is True |
| 72 | + |
| 73 | + |
| 74 | +def test_interrupt_in_pending_sends(redis_url: str) -> None: |
| 75 | + """ |
| 76 | + Test that Interrupt objects in pending_sends are properly deserialized. |
| 77 | +
|
| 78 | + This tests the actual scenario from issue #113 where interrupts stored |
| 79 | + in checkpoint writes need to be reconstructed. |
| 80 | + """ |
| 81 | + from langgraph.checkpoint.redis.jsonplus_redis import JsonPlusRedisSerializer |
| 82 | + |
| 83 | + serializer = JsonPlusRedisSerializer() |
| 84 | + |
| 85 | + # Simulate what gets stored in pending_sends |
| 86 | + # In the real scenario, pending_sends contains tuples of (channel, value) |
| 87 | + # where value might be an Interrupt object |
| 88 | + pending_sends = [ |
| 89 | + ("__interrupt__", [Interrupt(value={"test": "data"}, resumable=False)]), |
| 90 | + ("messages", ["some message"]), |
| 91 | + ] |
| 92 | + |
| 93 | + # Serialize the pending_sends |
| 94 | + serialized = serializer.dumps(pending_sends) |
| 95 | + |
| 96 | + # Deserialize |
| 97 | + deserialized = serializer.loads(serialized) |
| 98 | + |
| 99 | + # Check the structure |
| 100 | + assert isinstance(deserialized, list) |
| 101 | + assert len(deserialized) == 2 |
| 102 | + |
| 103 | + # The first item should have reconstructed Interrupt object |
| 104 | + channel, value = deserialized[0] |
| 105 | + assert channel == "__interrupt__" |
| 106 | + assert isinstance(value, list) |
| 107 | + assert len(value) == 1 |
| 108 | + |
| 109 | + # THIS IS THE CRITICAL CHECK - value[0] must be an Interrupt, not a dict |
| 110 | + assert isinstance(value[0], Interrupt), ( |
| 111 | + f"Expected Interrupt object in pending_sends, got {type(value[0])}. " |
| 112 | + f"This is the root cause of 'dict' object has no attribute error" |
| 113 | + ) |
| 114 | + assert value[0].value == {"test": "data"} |
| 115 | + assert value[0].resumable is False |
| 116 | + |
| 117 | + |
| 118 | +def test_interrupt_resume_workflow(redis_url: str) -> None: |
| 119 | + """ |
| 120 | + Integration test reproducing the exact scenario from issue #113. |
| 121 | +
|
| 122 | + This test should fail with AttributeError until the fix is implemented. |
| 123 | + """ |
| 124 | + with RedisSaver.from_conn_string(redis_url) as checkpointer: |
| 125 | + checkpointer.setup() |
| 126 | + |
| 127 | + builder = StateGraph(AgentState) |
| 128 | + builder.add_node("review", review_node) |
| 129 | + builder.add_edge(START, "review") |
| 130 | + builder.add_edge("review", END) |
| 131 | + |
| 132 | + graph = builder.compile(checkpointer=checkpointer) |
| 133 | + |
| 134 | + # Use unique thread ID |
| 135 | + config = {"configurable": {"thread_id": f"test-interrupt-{uuid4()}"}} |
| 136 | + |
| 137 | + # First invocation - should hit the interrupt |
| 138 | + initial = graph.invoke({}, config=config) |
| 139 | + print(f"Initial result: {initial}") |
| 140 | + |
| 141 | + # Resume with Command - this is where the error occurs |
| 142 | + # The error happens because pending_sends contains dicts instead of Interrupt objects |
| 143 | + # When LangGraph tries to access Interrupt attributes |
| 144 | + # It fails because value[0] is {'value': ..., 'resumable': ..., 'ns': ..., 'when': ...} not Interrupt(...) |
| 145 | + final_state = graph.invoke(Command(resume={"test": "response"}), config=config) |
| 146 | + |
| 147 | + # If we get here, the test passed |
| 148 | + assert "messages" in final_state |
| 149 | + print(f"Final messages: {final_state['messages']}") |
0 commit comments