Implement robust error handling with user-friendly messages for LLM coding failures
Browse filesENHANCED ERROR HANDLING:
π Error Classification:
- Syntax errors (unmatched brackets, invalid syntax)
- Variable naming errors (not defined variables)
- Data access errors (attribute access issues)
- Mathematical errors (division by zero)
- Data availability errors (empty datasets, no matching data)
π€ User-Friendly Messages:
- Replace technical error messages with understandable explanations
- Provide specific guidance based on error type
- Add actionable suggestions for users
- Hide complex technical details while still logging them
π‘ Smart Suggestions:
- 'Try rephrasing your question'
- 'Use simpler terms'
- 'Check if data exists for your criteria'
- Specific advice based on error type
π Better UX:
- Users get helpful guidance instead of raw Python errors
- Technical details still available in generated code view
- Error logging preserved for debugging
- Encourages users to retry with better queries
This addresses the reality that LLM coding errors are inevitable - better to handle them gracefully than try to eliminate them completely.
π€ Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <[email protected]>
@@ -345,7 +345,7 @@ Complete the following code to answer the user's question:
|
|
345 |
response = llm.invoke(query)
|
346 |
answer = response.content
|
347 |
|
348 |
-
# Extract and execute code
|
349 |
try:
|
350 |
if "```python" in answer:
|
351 |
code_part = answer.split("```python")[1].split("```")[0]
|
@@ -357,7 +357,7 @@ Complete the following code to answer the user's question:
|
|
357 |
{code_part}
|
358 |
"""
|
359 |
|
360 |
-
# Execute code in a controlled environment
|
361 |
local_vars = {}
|
362 |
global_vars = {
|
363 |
'pd': pd,
|
@@ -374,7 +374,7 @@ Complete the following code to answer the user's question:
|
|
374 |
if 'answer' in local_vars:
|
375 |
answer_result = local_vars['answer']
|
376 |
else:
|
377 |
-
answer_result = "
|
378 |
|
379 |
execution_time = (datetime.now() - start_time).total_seconds()
|
380 |
|
@@ -405,11 +405,29 @@ Complete the following code to answer the user's question:
|
|
405 |
execution_time = (datetime.now() - start_time).total_seconds()
|
406 |
error_msg = str(code_error)
|
407 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
408 |
# Log the failed code execution
|
409 |
log_interaction(
|
410 |
user_query=question,
|
411 |
model_name=model_name,
|
412 |
-
response_content=
|
413 |
generated_code=full_code if 'full_code' in locals() else "",
|
414 |
execution_time=execution_time,
|
415 |
error_message=error_msg,
|
@@ -418,7 +436,7 @@ Complete the following code to answer the user's question:
|
|
418 |
|
419 |
return {
|
420 |
"role": "assistant",
|
421 |
-
"content":
|
422 |
"gen_code": full_code if 'full_code' in locals() else "",
|
423 |
"ex_code": full_code if 'full_code' in locals() else "",
|
424 |
"last_prompt": question,
|
|
|
345 |
response = llm.invoke(query)
|
346 |
answer = response.content
|
347 |
|
348 |
+
# Extract and execute code with enhanced error handling
|
349 |
try:
|
350 |
if "```python" in answer:
|
351 |
code_part = answer.split("```python")[1].split("```")[0]
|
|
|
357 |
{code_part}
|
358 |
"""
|
359 |
|
360 |
+
# Execute code in a controlled environment with better error handling
|
361 |
local_vars = {}
|
362 |
global_vars = {
|
363 |
'pd': pd,
|
|
|
374 |
if 'answer' in local_vars:
|
375 |
answer_result = local_vars['answer']
|
376 |
else:
|
377 |
+
answer_result = "Code executed but no result was saved in 'answer' variable"
|
378 |
|
379 |
execution_time = (datetime.now() - start_time).total_seconds()
|
380 |
|
|
|
405 |
execution_time = (datetime.now() - start_time).total_seconds()
|
406 |
error_msg = str(code_error)
|
407 |
|
408 |
+
# Classify and provide user-friendly error messages
|
409 |
+
user_friendly_msg = "I encountered an error while analyzing your data. "
|
410 |
+
|
411 |
+
if "unmatched" in error_msg.lower() or "invalid syntax" in error_msg.lower():
|
412 |
+
user_friendly_msg += "There was a syntax error in the generated code (missing brackets or quotes). Please try rephrasing your question or try again."
|
413 |
+
elif "not defined" in error_msg.lower():
|
414 |
+
user_friendly_msg += "There was a variable naming error in the generated code. Please try asking the question again."
|
415 |
+
elif "has no attribute" in error_msg.lower():
|
416 |
+
user_friendly_msg += "There was an issue accessing data properties. Please try a simpler version of your question."
|
417 |
+
elif "division by zero" in error_msg.lower():
|
418 |
+
user_friendly_msg += "The calculation involved division by zero, possibly due to missing data. Please try a different time period or location."
|
419 |
+
elif "empty" in error_msg.lower() or "no data" in error_msg.lower():
|
420 |
+
user_friendly_msg += "No relevant data was found for your query. Please try adjusting the time period, location, or criteria."
|
421 |
+
else:
|
422 |
+
user_friendly_msg += f"Technical error: {error_msg}"
|
423 |
+
|
424 |
+
user_friendly_msg += "\n\nπ‘ **Suggestions:**\n- Try rephrasing your question\n- Use simpler terms\n- Check if the data exists for your specified criteria"
|
425 |
+
|
426 |
# Log the failed code execution
|
427 |
log_interaction(
|
428 |
user_query=question,
|
429 |
model_name=model_name,
|
430 |
+
response_content=user_friendly_msg,
|
431 |
generated_code=full_code if 'full_code' in locals() else "",
|
432 |
execution_time=execution_time,
|
433 |
error_message=error_msg,
|
|
|
436 |
|
437 |
return {
|
438 |
"role": "assistant",
|
439 |
+
"content": user_friendly_msg,
|
440 |
"gen_code": full_code if 'full_code' in locals() else "",
|
441 |
"ex_code": full_code if 'full_code' in locals() else "",
|
442 |
"last_prompt": question,
|