Skip to content

Commit f9a94ce

Browse files
authored
Bug fix for Redis TypeError (#2411)
1 parent f0f1051 commit f9a94ce

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

python/packages/core/agent_framework/_threads.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def __init__(
140140
"""
141141
if not messages:
142142
self.messages: list[ChatMessage] = []
143+
return
143144
if not isinstance(messages, list):
144145
raise TypeError("Messages should be a list")
145146
new_messages: list[ChatMessage] = []

python/packages/core/tests/core/test_threads.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,18 @@ def test_init_empty(self) -> None:
384384

385385
assert len(state.messages) == 0
386386

387+
def test_init_none(self) -> None:
388+
"""Test ChatMessageStoreState initialization with None messages."""
389+
state = ChatMessageStoreState(messages=None)
390+
391+
assert len(state.messages) == 0
392+
393+
def test_init_no_messages_arg(self) -> None:
394+
"""Test ChatMessageStoreState initialization without messages argument."""
395+
state = ChatMessageStoreState()
396+
397+
assert len(state.messages) == 0
398+
387399

388400
class TestThreadState:
389401
"""Test cases for AgentThreadState class."""
@@ -415,3 +427,22 @@ def test_init_defaults(self) -> None:
415427

416428
assert state.service_thread_id is None
417429
assert state.chat_message_store_state is None
430+
431+
def test_init_with_chat_message_store_state_no_messages(self) -> None:
432+
"""Test AgentThreadState initialization with chat_message_store_state without messages field.
433+
434+
This tests the scenario where a custom ChatMessageStore (like RedisChatMessageStore)
435+
serializes its state without a 'messages' field, containing only configuration data
436+
like thread_id, redis_url, etc.
437+
"""
438+
store_data: dict[str, Any] = {
439+
"type": "redis_store_state",
440+
"thread_id": "test_thread_123",
441+
"redis_url": "redis://localhost:6379",
442+
"key_prefix": "chat_messages",
443+
}
444+
state = AgentThreadState.from_dict({"chat_message_store_state": store_data})
445+
446+
assert state.service_thread_id is None
447+
assert state.chat_message_store_state is not None
448+
assert state.chat_message_store_state.messages == []

python/packages/redis/tests/test_redis_chat_message_store.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,20 @@ async def test_extend(self, redis_store, mock_redis_client, sample_messages):
495495
# Verify rpush was called for each message
496496
pipeline_mock = mock_redis_client.pipeline.return_value.__aenter__.return_value
497497
assert pipeline_mock.rpush.call_count >= 2
498+
499+
async def test_serialize_with_agent_thread(self, redis_store, sample_messages):
500+
"""Test that RedisChatMessageStore can be serialized within an AgentThread.
501+
502+
This test verifies the fix for issue #1991 where calling thread.serialize()
503+
with a RedisChatMessageStore would fail with "Messages should be a list" error.
504+
"""
505+
from agent_framework import AgentThread
506+
507+
thread = AgentThread(message_store=redis_store)
508+
await thread.on_new_messages(sample_messages)
509+
510+
serialized = await thread.serialize()
511+
512+
assert serialized is not None
513+
assert "chat_message_store_state" in serialized
514+
assert serialized["chat_message_store_state"] is not None

0 commit comments

Comments
 (0)