Fixing structured outputs
Browse files- app.py +16 -3
- langgraph_agent/structured_output.py +9 -0
app.py
CHANGED
|
@@ -911,20 +911,26 @@ async def chat_with_tool_visibility(
|
|
| 911 |
if content:
|
| 912 |
# Clear "Thinking..." on first real content
|
| 913 |
if chat_response == "💭 _Thinking..._":
|
|
|
|
| 914 |
chat_response = ""
|
| 915 |
progress(0.7, desc="📝 Generating response...")
|
| 916 |
|
| 917 |
# Handle both string (OpenAI) and list (Anthropic) content formats
|
|
|
|
| 918 |
if isinstance(content, list):
|
| 919 |
# Anthropic returns list of content blocks - extract text
|
| 920 |
for block in content:
|
| 921 |
if hasattr(block, 'text'):
|
| 922 |
-
|
| 923 |
elif isinstance(block, dict) and 'text' in block:
|
| 924 |
-
|
| 925 |
else:
|
| 926 |
# OpenAI/HF return string directly
|
| 927 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 928 |
yield chat_response, tool_log
|
| 929 |
|
| 930 |
# Tool finished
|
|
@@ -947,7 +953,10 @@ async def chat_with_tool_visibility(
|
|
| 947 |
# Format output for chat display (with image rendering)
|
| 948 |
formatted_output = format_tool_output_for_chat(tool_output)
|
| 949 |
if formatted_output.strip():
|
|
|
|
|
|
|
| 950 |
chat_response += formatted_output
|
|
|
|
| 951 |
|
| 952 |
yield chat_response, tool_log
|
| 953 |
|
|
@@ -956,6 +965,10 @@ async def chat_with_tool_visibility(
|
|
| 956 |
# yield chat_response, tool_log
|
| 957 |
progress(0.9, desc="✨ Finalizing response...")
|
| 958 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 959 |
try:
|
| 960 |
from langgraph_agent.structured_output import parse_agent_response
|
| 961 |
formatted_response = await parse_agent_response(
|
|
|
|
| 911 |
if content:
|
| 912 |
# Clear "Thinking..." on first real content
|
| 913 |
if chat_response == "💭 _Thinking..._":
|
| 914 |
+
print("[STREAM] Clearing 'Thinking...' placeholder")
|
| 915 |
chat_response = ""
|
| 916 |
progress(0.7, desc="📝 Generating response...")
|
| 917 |
|
| 918 |
# Handle both string (OpenAI) and list (Anthropic) content formats
|
| 919 |
+
content_to_add = ""
|
| 920 |
if isinstance(content, list):
|
| 921 |
# Anthropic returns list of content blocks - extract text
|
| 922 |
for block in content:
|
| 923 |
if hasattr(block, 'text'):
|
| 924 |
+
content_to_add += block.text
|
| 925 |
elif isinstance(block, dict) and 'text' in block:
|
| 926 |
+
content_to_add += block['text']
|
| 927 |
else:
|
| 928 |
# OpenAI/HF return string directly
|
| 929 |
+
content_to_add = content
|
| 930 |
+
|
| 931 |
+
if content_to_add:
|
| 932 |
+
print(f"[STREAM] Adding LLM content: {content_to_add[:100]}...")
|
| 933 |
+
chat_response += content_to_add
|
| 934 |
yield chat_response, tool_log
|
| 935 |
|
| 936 |
# Tool finished
|
|
|
|
| 953 |
# Format output for chat display (with image rendering)
|
| 954 |
formatted_output = format_tool_output_for_chat(tool_output)
|
| 955 |
if formatted_output.strip():
|
| 956 |
+
print(f"[STREAM] Adding formatted tool output ({len(formatted_output)} chars): {formatted_output[:200]}...")
|
| 957 |
+
print(f"[STREAM] chat_response length before: {len(chat_response)}")
|
| 958 |
chat_response += formatted_output
|
| 959 |
+
print(f"[STREAM] chat_response length after: {len(chat_response)}")
|
| 960 |
|
| 961 |
yield chat_response, tool_log
|
| 962 |
|
|
|
|
| 965 |
# yield chat_response, tool_log
|
| 966 |
progress(0.9, desc="✨ Finalizing response...")
|
| 967 |
|
| 968 |
+
print(f"\n[FINAL] chat_response length before parsing: {len(chat_response)}")
|
| 969 |
+
print(f"[FINAL] chat_response preview (first 300): {chat_response[:300]}")
|
| 970 |
+
print(f"[FINAL] chat_response preview (last 300): {chat_response[-300:]}\n")
|
| 971 |
+
|
| 972 |
try:
|
| 973 |
from langgraph_agent.structured_output import parse_agent_response
|
| 974 |
formatted_response = await parse_agent_response(
|
langgraph_agent/structured_output.py
CHANGED
|
@@ -52,11 +52,17 @@ def extract_urls_from_text(text: str) -> tuple[List[str], List[str]]:
|
|
| 52 |
audio_pattern_files = r'https?://[^\s)}\]]+?\.(?:mp3|wav|ogg|m4a)(?:\?[^\s)}\]]*)?'
|
| 53 |
audio_pattern_xenocanto = r'https?://xeno-canto\.org/\d+/download'
|
| 54 |
|
|
|
|
|
|
|
| 55 |
# Extract all URLs
|
| 56 |
raw_image_urls = re.findall(image_pattern, text, re.IGNORECASE)
|
| 57 |
raw_audio_urls_files = re.findall(audio_pattern_files, text, re.IGNORECASE)
|
| 58 |
audio_urls_xenocanto = list(set(re.findall(audio_pattern_xenocanto, text, re.IGNORECASE)))
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# Clean URLs (remove trailing quotes, commas, etc.)
|
| 61 |
def clean_url(url: str) -> str:
|
| 62 |
return url.rstrip('",;)')
|
|
@@ -112,6 +118,9 @@ async def parse_agent_response(
|
|
| 112 |
"""
|
| 113 |
try:
|
| 114 |
print("[STRUCTURED OUTPUT] Starting parsing...")
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
# Extract URLs using regex (fast, no API call)
|
| 117 |
image_urls, audio_urls = extract_urls_from_text(raw_response)
|
|
|
|
| 52 |
audio_pattern_files = r'https?://[^\s)}\]]+?\.(?:mp3|wav|ogg|m4a)(?:\?[^\s)}\]]*)?'
|
| 53 |
audio_pattern_xenocanto = r'https?://xeno-canto\.org/\d+/download'
|
| 54 |
|
| 55 |
+
print(f"[EXTRACT_URLS] Searching text of length {len(text)}")
|
| 56 |
+
|
| 57 |
# Extract all URLs
|
| 58 |
raw_image_urls = re.findall(image_pattern, text, re.IGNORECASE)
|
| 59 |
raw_audio_urls_files = re.findall(audio_pattern_files, text, re.IGNORECASE)
|
| 60 |
audio_urls_xenocanto = list(set(re.findall(audio_pattern_xenocanto, text, re.IGNORECASE)))
|
| 61 |
|
| 62 |
+
print(f"[EXTRACT_URLS] Found {len(raw_image_urls)} raw image URLs")
|
| 63 |
+
print(f"[EXTRACT_URLS] Found {len(raw_audio_urls_files)} audio file URLs")
|
| 64 |
+
print(f"[EXTRACT_URLS] Found {len(audio_urls_xenocanto)} xeno-canto URLs")
|
| 65 |
+
|
| 66 |
# Clean URLs (remove trailing quotes, commas, etc.)
|
| 67 |
def clean_url(url: str) -> str:
|
| 68 |
return url.rstrip('",;)')
|
|
|
|
| 118 |
"""
|
| 119 |
try:
|
| 120 |
print("[STRUCTURED OUTPUT] Starting parsing...")
|
| 121 |
+
print(f"[STRUCTURED OUTPUT] Raw response length: {len(raw_response)} characters")
|
| 122 |
+
print(f"[STRUCTURED OUTPUT] First 500 chars: {raw_response[:500]}")
|
| 123 |
+
print(f"[STRUCTURED OUTPUT] Last 500 chars: {raw_response[-500:]}")
|
| 124 |
|
| 125 |
# Extract URLs using regex (fast, no API call)
|
| 126 |
image_urls, audio_urls = extract_urls_from_text(raw_response)
|