-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_vertical_output.py
More file actions
158 lines (130 loc) · 5.03 KB
/
debug_vertical_output.py
File metadata and controls
158 lines (130 loc) · 5.03 KB
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
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
"""
Debug script to identify and fix vertical output issues
Common causes of vertical output:
1. Iterating over string instead of string.split()
2. Incorrect streaming/chunking
3. Character encoding issues
4. JSON parsing issues
5. Improper list iteration
"""
import re
def test_common_vertical_output_bugs():
"""Test common patterns that cause vertical output"""
print("=== Testing Common Vertical Output Patterns ===\n")
# Example 1: Wrong iteration (causes vertical output)
text = "Analyzed page structure and JavaScript interaction logic"
print("❌ WRONG - Iterating over string directly:")
for i, char in enumerate(text):
if i < 10: # Only show first 10 to avoid spam
print(f"- {char}")
elif i == 10:
print("- ... (continuing vertically)")
break
print("\n✅ CORRECT - Process string as whole:")
print(f"Normal output: {text}")
# Example 2: Wrong list handling
print("\n❌ WRONG - Processing response character by character:")
response_text = "Normal response"
# This is what causes vertical output:
# for char in response_text:
# yield char # Each character yielded separately
print("✅ CORRECT - Process response as complete text:")
print(f"Complete response: {response_text}")
# Example 3: JSON parsing issues
print("\n❌ WRONG - Treating JSON string as iterable:")
json_like = '{"type": "text", "content": "Hello"}'
print("If we iterate over JSON string:")
for i, char in enumerate(json_like):
if i < 15:
print(f"- {char}")
elif i == 15:
print("- ... (vertical output)")
break
print("\n✅ CORRECT - Parse JSON first:")
import json
try:
parsed = json.loads(json_like)
print(f"Parsed content: {parsed.get('content', '')}")
except:
print("JSON parsing failed")
def analyze_anthropic_streaming_issue():
"""Analyze potential Anthropic streaming response issues"""
print("\n=== Analyzing Anthropic API Response Patterns ===\n")
# Simulate problematic streaming patterns
print("Potential issue 1: Character-level streaming")
response_content = "Analyzed page structure"
print("❌ If processing character by character:")
print("Output would be:")
example_chars = ["A", "n", "a", "l", "y", "z", "e", "d", " ", "p", "a", "g", "e"]
for char in example_chars:
print(f"- {char}")
print("\n✅ Correct streaming should accumulate:")
accumulated = ""
for char in example_chars:
accumulated += char
print(f"Final result: {accumulated}")
print("\nPotential issue 2: Incorrect content block processing")
# Simulate what might happen in create_message
mock_response = {
"content": [
{"type": "text", "text": "Analyzed page structure and JavaScript interaction logic"}
]
}
print("❌ Wrong way - iterating over text characters:")
text_content = mock_response["content"][0]["text"]
print(f"If we do: for char in text_content")
print("Result: Vertical output")
print("\n✅ Correct way - use the text directly:")
print(f"Direct use: {text_content}")
def check_potential_fixes():
"""Show potential fixes for vertical output"""
print("\n=== Potential Fixes ===\n")
print("1. Fix streaming in anthropic_provider.py:")
print("""
# ❌ Wrong:
for char in response.content:
yield char
# ✅ Correct:
if response.content:
yield response.content
""")
print("2. Fix content processing:")
print("""
# ❌ Wrong:
for content_block in response.content:
if content_block.type == "text":
for char in content_block.text: # This causes vertical output
yield char
# ✅ Correct:
for content_block in response.content:
if content_block.type == "text":
yield content_block.text # Yield complete text
""")
print("3. Fix message handling:")
print("""
# ❌ Wrong:
assistant_content = ""
for item in response:
assistant_content += item # If item is individual characters
# ✅ Correct:
assistant_content = response.get("content", "") # Get complete content
""")
def main():
"""Run all debug tests"""
test_common_vertical_output_bugs()
analyze_anthropic_streaming_issue()
check_potential_fixes()
print("\n" + "="*60)
print("DIAGNOSIS:")
print("The vertical output (each character on a separate line)")
print("is typically caused by:")
print("1. Iterating over a string character by character")
print("2. Yielding individual characters in streaming responses")
print("3. Processing API response content incorrectly")
print("\nTo fix this, check:")
print("- anthropic_provider.py: create_message method")
print("- agent.py: response processing loop")
print("- Any place where string iteration happens")
if __name__ == "__main__":
main()