File size: 4,153 Bytes
1637cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
"""
Debug script to find where .lower() is being called on non-strings
"""

import os
import sys

# Set up path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

# Set minimal env vars
os.environ["ANTHROPIC_API_KEY"] = "test-key"

def find_lower_calls():
    """Find all .lower() calls in the code"""
    print("Searching for all .lower() calls in app.py...")
    print("-" * 60)
    
    with open('app.py', 'r') as f:
        lines = f.readlines()
    
    lower_calls = []
    for i, line in enumerate(lines, 1):
        if '.lower()' in line:
            lower_calls.append((i, line.strip()))
    
    print(f"Found {len(lower_calls)} .lower() calls:\n")
    for line_num, line in lower_calls:
        print(f"Line {line_num}: {line}")
        # Check if there's protection
        if 'isinstance' in lines[line_num-2:line_num]:
            print("  βœ… Has type checking")
        else:
            print("  ⚠️  No type checking nearby")
        print()

def test_problematic_inputs():
    """Test inputs that might cause .lower() errors"""
    print("\nTesting problematic inputs...")
    print("-" * 60)
    
    # Test cases that might break .lower()
    test_inputs = [
        "normal string",
        ["list", "of", "strings"],
        {"dict": "value"},
        123,
        None,
        [{"nested": "structure"}],
        b"bytes string",
    ]
    
    for test_input in test_inputs:
        print(f"\nInput: {repr(test_input)} (type: {type(test_input)})")
        
        # Test direct .lower()
        try:
            result = test_input.lower()
            print(f"  βœ… .lower() works: {result}")
        except AttributeError as e:
            print(f"  ❌ .lower() fails: {e}")
        
        # Test with type checking
        try:
            if isinstance(test_input, str):
                result = test_input.lower()
                print(f"  βœ… With type check: {result}")
            else:
                result = str(test_input).lower()
                print(f"  βœ… With str() conversion: {result}")
        except Exception as e:
            print(f"  ❌ Even with protection: {e}")

def test_message_content():
    """Test what might be in message.content"""
    print("\n\nTesting message content scenarios...")
    print("-" * 60)
    
    # Simulate different message contents
    class MockMessage:
        def __init__(self, content):
            self.content = content
    
    test_messages = [
        MockMessage("Normal text content"),
        MockMessage(["List", "content"]),  # This might happen!
        MockMessage({"type": "text", "content": "dict content"}),
        MockMessage(None),
    ]
    
    for i, msg in enumerate(test_messages):
        print(f"\nMessage {i}: content = {repr(msg.content)}")
        
        # Simulate what might happen in the code
        if hasattr(msg, "content") and msg.content:
            content = msg.content
            print(f"  Content type: {type(content)}")
            
            # This would fail on non-strings!
            try:
                content = content.strip()
                print(f"  βœ… .strip() works")
            except AttributeError:
                print(f"  ❌ .strip() fails - content is not a string!")
            
            # Safe approach
            if isinstance(content, list):
                content = " ".join(str(item) for item in content)
                print(f"  βœ… Converted list to string: {content}")
            elif not isinstance(content, str):
                content = str(content)
                print(f"  βœ… Converted to string: {content}")

if __name__ == "__main__":
    print("=" * 80)
    print("DEBUG: Finding .lower() error sources")
    print("=" * 80)
    
    find_lower_calls()
    test_problematic_inputs()
    test_message_content()
    
    print("\n" + "=" * 80)
    print("CONCLUSION:")
    print("The error likely occurs when message.content is a list instead of string")
    print("This can happen with multimodal messages or tool responses")
    print("Solution: Always check type before calling .lower() or .strip()")
    print("=" * 80)