diff --git a/backend/onyx/agents/agent_search/orchestration/nodes/tool_call.py b/backend/onyx/agents/agent_search/orchestration/nodes/tool_call.py index 3773ca36587b..17f8411e139e 100644 --- a/backend/onyx/agents/agent_search/orchestration/nodes/tool_call.py +++ b/backend/onyx/agents/agent_search/orchestration/nodes/tool_call.py @@ -20,6 +20,10 @@ from onyx.utils.logger import setup_logger logger = setup_logger() +class ToolCallException(Exception): + """Exception raised for errors during tool calls.""" + + def emit_packet(packet: AnswerPacket, writer: StreamWriter) -> None: write_custom_event("basic_response", packet, writer) @@ -45,13 +49,18 @@ def tool_call( emit_packet(tool_kickoff, writer) - tool_responses = [] - for response in tool_runner.tool_responses(): - tool_responses.append(response) - emit_packet(response, writer) + try: + tool_responses = [] + for response in tool_runner.tool_responses(): + tool_responses.append(response) + emit_packet(response, writer) - tool_final_result = tool_runner.tool_final_result() - emit_packet(tool_final_result, writer) + tool_final_result = tool_runner.tool_final_result() + emit_packet(tool_final_result, writer) + except Exception as e: + raise ToolCallException( + f"Error during tool call for {tool.display_name}: {e}" + ) from e tool_call = ToolCall(name=tool.name, args=tool_args, id=tool_id) tool_call_summary = ToolCallSummary( diff --git a/backend/onyx/chat/process_message.py b/backend/onyx/chat/process_message.py index f87de5f48885..d1e5fbea609c 100644 --- a/backend/onyx/chat/process_message.py +++ b/backend/onyx/chat/process_message.py @@ -7,6 +7,7 @@ from typing import cast from sqlalchemy.orm import Session +from onyx.agents.agent_search.orchestration.nodes.tool_call import ToolCallException from onyx.chat.answer import Answer from onyx.chat.chat_utils import create_chat_chain from onyx.chat.chat_utils import create_temporary_persona @@ -945,19 +946,25 @@ def stream_chat_message_objects( return except Exception as e: - logger.exception("Failed to process chat message.") - + logger.exception(f"Failed to process chat message due to {e}") error_msg = str(e) stack_trace = traceback.format_exc() - if llm: - client_error_msg = litellm_exception_to_error_msg(e, llm) - if llm.config.api_key and len(llm.config.api_key) > 2: - error_msg = error_msg.replace(llm.config.api_key, "[REDACTED_API_KEY]") - stack_trace = stack_trace.replace( - llm.config.api_key, "[REDACTED_API_KEY]" - ) - yield StreamingError(error=client_error_msg, stack_trace=stack_trace) + if isinstance(e, ToolCallException): + yield StreamingError(error=error_msg, stack_trace=stack_trace) + else: + if llm: + client_error_msg = litellm_exception_to_error_msg(e, llm) + if llm.config.api_key and len(llm.config.api_key) > 2: + error_msg = error_msg.replace( + llm.config.api_key, "[REDACTED_API_KEY]" + ) + stack_trace = stack_trace.replace( + llm.config.api_key, "[REDACTED_API_KEY]" + ) + + yield StreamingError(error=client_error_msg, stack_trace=stack_trace) + db_session.rollback() return diff --git a/backend/onyx/tools/tool_implementations/internet_search/internet_search_tool.py b/backend/onyx/tools/tool_implementations/internet_search/internet_search_tool.py index 38e1249372a1..089739384edf 100644 --- a/backend/onyx/tools/tool_implementations/internet_search/internet_search_tool.py +++ b/backend/onyx/tools/tool_implementations/internet_search/internet_search_tool.py @@ -218,6 +218,9 @@ class InternetSearchTool(Tool): headers=self.headers, params={"q": query, "count": self.num_results}, ) + + response.raise_for_status() + results = response.json() # If no hits, Bing does not include the webPages key diff --git a/web/src/app/chat/ChatPage.tsx b/web/src/app/chat/ChatPage.tsx index bffe673c7271..b339c1bc67ac 100644 --- a/web/src/app/chat/ChatPage.tsx +++ b/web/src/app/chat/ChatPage.tsx @@ -2975,8 +2975,7 @@ export function ChatPage({ } else { return (