@@ -121,8 +121,21 @@ async def create_chat_completion_with_tools(
121121 tool_result = await tool (** tool_args ) if asyncio .iscoroutinefunction (tool ) else tool (** tool_args )
122122 break
123123 except Exception as e :
124- logger .error (f"Error executing tool { tool_name } : { e } " )
125- tool_result = f"Tool error: { str (e )} "
124+ error_type = type (e ).__name__
125+ error_msg = str (e )
126+ logger .error (
127+ f"Error executing tool '{ tool_name } ': { error_type } : { error_msg } " ,
128+ exc_info = True
129+ )
130+ # Provide user-friendly error message
131+ if "timeout" in error_msg .lower () or "timed out" in error_msg .lower ():
132+ tool_result = f"Tool '{ tool_name } ' timed out. The operation took too long to complete. Please try again or check your network connection."
133+ elif "connection" in error_msg .lower () or "network" in error_msg .lower ():
134+ tool_result = f"Tool '{ tool_name } ' failed due to a network issue. Please check your internet connection and try again."
135+ elif "permission" in error_msg .lower () or "access" in error_msg .lower ():
136+ tool_result = f"Tool '{ tool_name } ' failed due to insufficient permissions. Please check your API keys or access credentials."
137+ else :
138+ tool_result = f"Tool '{ tool_name } ' encountered an error: { error_msg } . Please check the logs for more details."
126139
127140 # Add tool result to conversation
128141 tool_message = ToolMessage (content = str (tool_result ), tool_call_id = tool_id )
@@ -159,7 +172,12 @@ async def create_chat_completion_with_tools(
159172 return response .content , []
160173
161174 except Exception as e :
162- logger .error (f"Error in tool-enabled chat completion: { str (e )} " )
175+ error_type = type (e ).__name__
176+ error_msg = str (e )
177+ logger .error (
178+ f"Error in tool-enabled chat completion: { error_type } : { error_msg } " ,
179+ exc_info = True
180+ )
163181 logger .info ("Falling back to simple chat completion without tools" )
164182
165183 # Fallback to simple chat completion without tools
@@ -202,8 +220,21 @@ def search_tool(query: str) -> str:
202220 else :
203221 return f"No search results found for: { query } "
204222 except Exception as e :
205- logger .error (f"Search tool error: { str (e )} " )
206- return f"Search error: { str (e )} "
223+ error_type = type (e ).__name__
224+ error_msg = str (e )
225+ logger .error (
226+ f"Search tool error: { error_type } : { error_msg } " ,
227+ exc_info = True
228+ )
229+ # Provide context-aware error messages
230+ if "api" in error_msg .lower () or "key" in error_msg .lower ():
231+ return f"Search failed: API key issue. Please verify your search API credentials are configured correctly."
232+ elif "timeout" in error_msg .lower () or "timed out" in error_msg .lower ():
233+ return f"Search timed out. The search request took too long. Please try again with a different query."
234+ elif "rate limit" in error_msg .lower () or "quota" in error_msg .lower ():
235+ return f"Search rate limit exceeded. Please wait a moment before trying again."
236+ else :
237+ return f"Search encountered an error: { error_msg } . Please check your search provider configuration."
207238
208239 return search_tool
209240
@@ -232,8 +263,19 @@ def custom_tool(*args, **kwargs) -> str:
232263 result = function (* args , ** kwargs )
233264 return str (result ) if result is not None else "Tool executed successfully"
234265 except Exception as e :
235- logger .error (f"Custom tool '{ name } ' error: { str (e )} " )
236- return f"Tool error: { str (e )} "
266+ error_type = type (e ).__name__
267+ error_msg = str (e )
268+ logger .error (
269+ f"Custom tool '{ name } ' error: { error_type } : { error_msg } " ,
270+ exc_info = True
271+ )
272+ # Provide informative error message without exposing internal details
273+ if "validation" in error_msg .lower () or "invalid" in error_msg .lower ():
274+ return f"Tool '{ name } ' received invalid input. Please check the parameters and try again."
275+ elif "not found" in error_msg .lower () or "missing" in error_msg .lower ():
276+ return f"Tool '{ name } ' could not find required resources. Please verify the input data is correct."
277+ else :
278+ return f"Tool '{ name } ' encountered an error: { error_msg } . Please check the tool configuration."
237279
238280 # Set tool metadata
239281 custom_tool .name = name
0 commit comments