chore: improve agent error logging

This commit is contained in:
csh28 2026-05-11 20:29:27 +08:00
parent 1f06450402
commit e4fc0db07d
2 changed files with 11 additions and 3 deletions

View File

@ -1,6 +1,7 @@
"""Logging callback handler module.""" """Logging callback handler module."""
import logging import logging
import traceback
from typing import Any, Optional, Dict, List from typing import Any, Optional, Dict, List
from langchain_core.callbacks import BaseCallbackHandler from langchain_core.callbacks import BaseCallbackHandler
from langchain_core.messages import BaseMessage from langchain_core.messages import BaseMessage
@ -80,4 +81,8 @@ class LoggingCallbackHandler(BaseCallbackHandler):
self, error: Exception, **kwargs: Any self, error: Exception, **kwargs: Any
) -> None: ) -> None:
"""Called when a tool invocation raises an error.""" """Called when a tool invocation raises an error."""
self.logger.error(f"❌ Tool Error: {error}") self.logger.error(
"❌ Tool Error: %s\n%s",
repr(error),
"".join(traceback.format_exception(type(error), error, error.__traceback__)),
)

View File

@ -3,6 +3,7 @@ import os
import asyncio import asyncio
import shutil import shutil
import time import time
import traceback
from typing import Union, Optional, Any, List, Dict from typing import Union, Optional, Any, List, Dict
from fastapi import APIRouter, HTTPException, Header, Body from fastapi import APIRouter, HTTPException, Header, Body
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
@ -185,9 +186,11 @@ async def enhanced_generate_stream_response(
await output_queue.put(("agent_done", None)) await output_queue.put(("agent_done", None))
except Exception as e: except Exception as e:
logger.error(f"Error in agent task: {e}") logger.error(f"Error in agent task: {e}\n{traceback.format_exc()}")
# Send error information to the client # Send error information to the client
await output_queue.put(("agent", f'data: {{"error": "{str(e)}"}}\n\n')) await output_queue.put(
("agent", f"data: {json.dumps({'error': str(e)}, ensure_ascii=False)}\n\n")
)
# Send completion signal to ensure the output controller exits normally # Send completion signal to ensure the output controller exits normally
await output_queue.put(("agent_done", None)) await output_queue.put(("agent_done", None))