Skip to content

Commit bd6c834

Browse files
fix: Handle None text values in Claude conversations importer (#353)
Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: Paul Hernandez <phernandez@users.noreply.github.com>
1 parent 994c8b8 commit bd6c834

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/basic_memory/importers/claude_conversations_importer.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ def _format_chat_markdown(
153153
# Handle message content
154154
content = msg.get("text", "")
155155
if msg.get("content"):
156-
content = " ".join(c.get("text", "") for c in msg["content"])
156+
# Filter out None values before joining
157+
content = " ".join(
158+
str(c.get("text", "")) for c in msg["content"]
159+
if c and c.get("text") is not None
160+
)
157161
lines.append(content)
158162

159163
# Handle attachments

tests/cli/test_import_claude_conversations.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,55 @@ def test_import_conversation_with_attachments(tmp_path):
149149
assert "**Attachment: test.txt**" in content
150150
assert "```" in content
151151
assert "Test file content" in content
152+
153+
154+
def test_import_conversation_with_none_text_values(tmp_path):
155+
"""Test importing conversation with None text values in content array (issue #236)."""
156+
# Create conversation with None text values
157+
conversation = {
158+
"uuid": "test-uuid",
159+
"name": "Test With None Text",
160+
"created_at": "2025-01-05T20:55:32.499880+00:00",
161+
"updated_at": "2025-01-05T20:56:39.477600+00:00",
162+
"chat_messages": [
163+
{
164+
"uuid": "msg-1",
165+
"text": None,
166+
"sender": "human",
167+
"created_at": "2025-01-05T20:55:32.499880+00:00",
168+
"content": [
169+
{"type": "text", "text": "Valid text here"},
170+
{"type": "text", "text": None}, # This caused the TypeError
171+
{"type": "text", "text": "More valid text"},
172+
],
173+
},
174+
{
175+
"uuid": "msg-2",
176+
"text": None,
177+
"sender": "assistant",
178+
"created_at": "2025-01-05T20:55:40.123456+00:00",
179+
"content": [
180+
{"type": "text", "text": None}, # All None case
181+
{"type": "text", "text": None},
182+
],
183+
},
184+
],
185+
}
186+
187+
json_file = tmp_path / "with_none_text.json"
188+
with open(json_file, "w", encoding="utf-8") as f:
189+
json.dump([conversation], f)
190+
191+
config = get_project_config()
192+
config.home = tmp_path
193+
194+
# Run import - should not fail with TypeError
195+
result = runner.invoke(app, ["import", "claude", "conversations", str(json_file)])
196+
assert result.exit_code == 0
197+
198+
# Check that valid text is preserved and None values are filtered out
199+
conv_path = tmp_path / "conversations/20250105-Test_With_None_Text.md"
200+
assert conv_path.exists()
201+
content = conv_path.read_text(encoding="utf-8")
202+
assert "Valid text here" in content
203+
assert "More valid text" in content

0 commit comments

Comments
 (0)