File size: 5,017 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python3
"""
Test script to debug the 'list' object has no attribute 'lower' error
"""

import os
import sys

# Add current directory to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

# Set test API keys
os.environ["ANTHROPIC_API_KEY"]= "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"

# Mock the API calls to avoid actual API usage
from unittest.mock import patch, MagicMock

def test_agent_with_various_inputs():
    """Test the agent with different input types that might cause errors"""

    print("Testing agent with various input types...")

    # Test cases that might cause the error
    test_cases = [
        # Normal string
        "What is 2 + 2?",

        # Question with image reference
        "Look at the image and tell me what you see",

        # Question with list-like content
        "Calculate the sum of [1, 2, 3, 4, 5]",

        # Question with code
        "What is the output of this code:\n```python\nprint([1, 2, 3])\n```",

        # Reversed text question
        ".rewsna eht sa 'tfel' drow eht fo etisoppo eht etirw",

        # Question with attachment reference
        "What is the final numeric output from the attached Python code?",
    ]

    # Import the agent
    try:
        from app import LangGraphAgent, _clean_answer

        # Test the _clean_answer function directly with different inputs
        print("\n1. Testing _clean_answer function:")
        print("-" * 50)

        test_answers = [
            "42",
            ["The", "answer", "is", "42"],  # List input
            {"answer": "42"},  # Dict input
            42,  # Integer
            None,  # None
            ["list", "with", "numbers", 1, 2, 3],  # Mixed list
        ]

        # Create a mock agent to test _clean_answer
        class MockAgent:
            def _clean_answer(self, answer):
                # This is the current implementation
                answer = answer.strip()  # This will fail on lists!

                lower_answer = answer.lower()  # This will also fail!
                return answer

        mock_agent = MockAgent()

        for test_answer in test_answers:
            print(f"\nTesting with: {test_answer} (type: {type(test_answer)})")
            try:
                result = mock_agent._clean_answer(test_answer)
                print(f"βœ… Success: {result}")
            except AttributeError as e:
                print(f"❌ AttributeError: {e}")
            except Exception as e:
                print(f"❌ Other error: {type(e).__name__}: {e}")

        # Test with actual agent if possible
        print("\n\n2. Testing with tool responses that might return lists:")
        print("-" * 50)

        # Mock tool responses that might cause issues
        tool_responses = [
            # Normal response
            {"tool": "calculator", "output": "42"},

            # List response (this might be the issue!)
            {"tool": "python_executor", "output": ["Result:", "42"]},

            # Complex response
            {"tool": "web_search", "output": {"results": ["item1", "item2"]}},
        ]

        for response in tool_responses:
            print(f"\nTool response: {response}")
            output = response.get("output", "")
            print(f"Output type: {type(output)}")
            if isinstance(output, list):
                print("⚠️  This is a LIST - might cause 'lower' error!")

    except ImportError as e:
        print(f"Import error: {e}")
    except Exception as e:
        print(f"Unexpected error: {type(e).__name__}: {e}")

def test_message_content_types():
    """Test what types of content messages might contain"""
    print("\n\n3. Testing message content types:")
    print("-" * 50)

    from langchain_core.messages import HumanMessage, AIMessage

    # Test different message contents
    test_contents = [
        "Normal string message",
        ["List", "as", "content"],  # This might happen!
        {"type": "image", "data": "base64..."},  # Multimodal content
        None,
    ]

    for content in test_contents:
        print(f"\nTesting message with content: {content} (type: {type(content)})")
        try:
            msg = AIMessage(content=content)
            print(f"Message created successfully")
            print(f"Message.content type: {type(msg.content)}")
        except Exception as e:
            print(f"Error creating message: {e}")

if __name__ == "__main__":
    print("=" * 60)
    print("GAIA Agent Error Debugging Test")
    print("=" * 60)

    test_agent_with_various_inputs()
    test_message_content_types()

    print("\n\nConclusion:")
    print("-" * 50)
    print("The error likely occurs when:")
    print("1. A tool returns a list instead of a string")
    print("2. The message content is a list (multimodal)")
    print("3. The _clean_answer method tries to call .strip() or .lower() on a list")
    print("\nFix: Add type checking in _clean_answer method!")