Chapter 22 Exercises: Debugging and Troubleshooting with AI

Tier 1: Recall and Basic Understanding (Exercises 1-6)

Exercise 1: Error Classification

Classify each of the following Python errors into the correct category (Syntax, Name/Attribute, Type, Value/Key, Import, Runtime):

# Error A
>>> int("hello")
ValueError: invalid literal for int() with base 10: 'hello'

# Error B
>>> my_list = [1, 2, 3]
>>> my_list.add(4)
AttributeError: 'list' object has no attribute 'add'

# Error C
>>> x = 10 / 0
ZeroDivisionError: division by zero

# Error D
>>> import nonexistent_module
ModuleNotFoundError: No module named 'nonexistent_module'

# Error E
>>> def greet(name)
SyntaxError: expected ':'

# Error F
>>> data = {"a": 1}
>>> data["b"]
KeyError: 'b'

Exercise 2: Stack Trace Reading

Given the following stack trace, answer these questions: 1. In which file and function did the error originate? 2. What was the chain of calls leading to the error? 3. What is the error type and value? 4. Which lines are from user code vs. library code?

Traceback (most recent call last):
  File "main.py", line 12, in <module>
    app.run()
  File "/usr/lib/python3.11/site-packages/flask/app.py", line 1191, in run
    run_simple(host, port, self, **options)
  File "app/routes.py", line 45, in index
    users = get_active_users()
  File "app/models.py", line 78, in get_active_users
    return db.session.query(User).filter(User.active == True).all()
  File "/usr/lib/python3.11/site-packages/sqlalchemy/orm/query.py", line 2831, in all
    return self._execute_and_instances(context)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: users

Exercise 3: DESCRIBE Framework Identification

Read the following debugging prompt and identify which parts of the DESCRIBE framework are present and which are missing:

My Flask app crashes when I submit the registration form. Here's the error:

TypeError: Object of type datetime is not JSON serializable

I'm using Flask 2.3 and Python 3.11 on Ubuntu 22.04.

List each component of DESCRIBE and whether it is present, partially present, or missing.

Exercise 4: Error Message Vocabulary

Match each error message phrase to its meaning:

Phrase Meaning Options
"not subscriptable" A. Cannot use [] indexing on this type
"takes 2 positional arguments but 3 were given" B. Forgot self parameter in a method
"object is not callable" C. Used () on something that is not a function
"unexpected keyword argument" D. Passed a named parameter the function does not accept
"unhashable type: 'list'" E. Tried to use a mutable type as a dictionary key

Exercise 5: Debugging Workflow Ordering

Put the following debugging steps in the correct order according to the four-phase debugging cycle:

  • [ ] Ask AI to explain the root cause in depth
  • [ ] Copy the full stack trace and relevant code
  • [ ] Verify that the fix does not introduce new issues
  • [ ] Identify the exact steps that trigger the bug
  • [ ] Understand why the suggested fix should work
  • [ ] Write a regression test for the bug
  • [ ] Note the environment details (Python version, OS)
  • [ ] Document the resolution for the team

Exercise 6: Quick Template Selection

For each scenario, select the most appropriate quick debugging template (Error Explanation, Code Not Working as Expected, or Performance Issue):

  1. Your function returns None instead of a dictionary
  2. You see RecursionError: maximum recursion depth exceeded
  3. Your API endpoint takes 30 seconds to respond
  4. You get ImportError: cannot import name 'deprecated_function'
  5. A list comprehension produces duplicates when it should not
  6. Database queries are consuming 90% of request time

Tier 2: Application and Structured Debugging (Exercises 7-12)

Exercise 7: Writing an Effective Bug Report

You encounter the following error in your Django application. Write a complete debugging prompt using the DESCRIBE framework that you would send to an AI assistant:

Traceback (most recent call last):
  File "/home/user/myapp/views.py", line 23, in create_order
    order = Order.objects.create(
  File "/usr/lib/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/lib/python3.11/site-packages/django/db/models/query.py", line 677, in create
    obj.save(force_insert=True, using=self.db)
django.db.utils.IntegrityError: NOT NULL constraint failed: orders_order.customer_id

Additional context: You recently added a customer ForeignKey field to the Order model. The form does not include a customer field because the customer should be set from the logged-in user.

Exercise 8: Structured Log Analysis

Given the following application logs, identify the pattern and write a prompt to AI explaining what you observe and asking for help:

[2024-03-15 09:00:01] INFO  Request started method=GET path=/api/products
[2024-03-15 09:00:01] INFO  Cache hit key=products_all
[2024-03-15 09:00:01] INFO  Request completed status=200 duration=45ms

[2024-03-15 09:15:01] INFO  Request started method=GET path=/api/products
[2024-03-15 09:15:01] INFO  Cache miss key=products_all
[2024-03-15 09:15:02] INFO  DB query executed table=products rows=1523 duration=850ms
[2024-03-15 09:15:02] INFO  Cache set key=products_all ttl=900
[2024-03-15 09:15:02] INFO  Request completed status=200 duration=920ms

[2024-03-15 09:30:01] INFO  Request started method=GET path=/api/products
[2024-03-15 09:30:01] INFO  Cache miss key=products_all
[2024-03-15 09:30:02] INFO  DB query executed table=products rows=1523 duration=870ms
[2024-03-15 09:30:02] INFO  Cache set key=products_all ttl=900
[2024-03-15 09:30:02] INFO  Request completed status=200 duration=940ms

[2024-03-15 09:45:01] INFO  Request started method=GET path=/api/products
[2024-03-15 09:45:01] INFO  Cache miss key=products_all
[2024-03-15 09:45:02] INFO  DB query executed table=products rows=1524 duration=860ms

Exercise 9: pdb Session Interpretation

You set a breakpoint in a sorting function and captured this pdb session. Identify the bug and explain what is wrong:

> sort_utils.py(12)custom_sort()
-> sorted_items = sorted(items, key=lambda x: x["priority"])
(Pdb) p items
[{'name': 'Task A', 'priority': 1}, {'name': 'Task B', 'priority': '2'}, {'name': 'Task C', 'priority': 3}, {'name': 'Task D', 'priority': 'high'}]
(Pdb) p type(items[0]["priority"])
<class 'int'>
(Pdb) p type(items[1]["priority"])
<class 'str'>
(Pdb) p type(items[3]["priority"])
<class 'str'>
(Pdb) c
Traceback (most recent call last):
  File "sort_utils.py", line 12, in custom_sort
    sorted_items = sorted(items, key=lambda x: x["priority"])
TypeError: '<' not supported between instances of 'str' and 'int'

Write both your diagnosis and the prompt you would send to AI for a robust fix.

Exercise 10: Environment Diagnosis

You are trying to run a colleague's project and encounter this sequence of errors. For each error, explain the likely cause and the fix:

# Attempt 1
$ python app.py
bash: python: command not found

# Attempt 2
$ python3 app.py
Traceback (most recent call last):
  File "app.py", line 1, in <module>
    from flask import Flask
ModuleNotFoundError: No module named 'flask'

# Attempt 3
$ pip3 install -r requirements.txt
$ python3 app.py
Traceback (most recent call last):
  File "app.py", line 5, in <module>
    from app.database import init_db
  File "app/database.py", line 2, in <module>
    import psycopg2
ModuleNotFoundError: No module named 'psycopg2'

# Attempt 4
$ pip3 install psycopg2
ERROR: Failed building wheel for psycopg2
Error: pg_config executable not found.

Exercise 11: Profiling Output Analysis

Analyze the following cProfile output and write a prompt to AI asking for optimization advice. Identify the bottleneck yourself first.

         5000003 function calls in 8.234 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.523    0.523    8.234    8.234 report.py:1(generate_report)
  1000000    4.567    0.000    6.789    0.000 report.py:15(format_row)
  1000000    2.222    0.000    2.222    0.000 report.py:25(validate_data)
  1000000    0.534    0.000    0.534    0.000 {method 'format' of 'str'}
  1000000    0.234    0.000    0.234    0.000 {built-in method builtins.len}
  1000000    0.112    0.000    0.112    0.000 {built-in method builtins.isinstance}
        1    0.032    0.032    0.032    0.032 {built-in method builtins.print}
        1    0.010    0.010    0.010    0.010 report.py:5(load_data)

Exercise 12: Dependency Conflict Resolution

You receive this error when trying to install your project's dependencies. Write a structured AI prompt that includes all relevant information:

ERROR: Cannot install -r requirements.txt (line 3) and
-r requirements.txt (line 7) because these package versions have
conflicting dependencies.

The conflict is caused by:
    The user requested pandas==2.0.0
    numpy 1.26.0 (from pandas==2.0.0) requires numpy>=1.23.2
    tensorflow==2.12.0 requires numpy>=1.22.0,<1.24.0

The user requested numpy==1.26.0
But tensorflow==2.12.0 requires numpy>=1.22.0,<1.24.0

Your requirements.txt:

flask==3.0.0
sqlalchemy==2.0.23
pandas==2.0.0
scikit-learn==1.3.2
matplotlib==3.8.2
requests==2.31.0
tensorflow==2.12.0
numpy==1.26.0

Tier 3: Analysis and Multi-Step Debugging (Exercises 13-18)

Exercise 13: Multi-Error Chain Analysis

You encounter the following chain of errors while setting up a new feature. For each error, explain what happened, what the developer likely tried to fix it, and why the fix led to the next error:

Error 1:
ImportError: cannot import name 'Celery' from 'celery'

Error 2 (after pip install celery):
kombu.exceptions.OperationalError: [Errno 111] Connection refused

Error 3 (after starting Redis):
celery.exceptions.NotRegistered: 'app.tasks.process_order'

Error 4 (after fixing task registration):
billiard.exceptions.WorkerLostError: Worker exited prematurely: signal 9 (SIGKILL)

For each error, write the diagnostic question you would ask AI.

Exercise 14: Log Pattern Detection

You are given 50 lines of logs from a web application experiencing intermittent failures. Analyze the logs, identify at least three patterns, and write a structured prompt to AI explaining your findings:

[10:00:00] INFO  req_id=001 path=/api/data user=alice status=200 duration=120ms db_queries=3
[10:00:01] INFO  req_id=002 path=/api/data user=bob status=200 duration=115ms db_queries=3
[10:00:02] INFO  req_id=003 path=/api/data user=charlie status=200 duration=130ms db_queries=3
[10:00:15] INFO  req_id=004 path=/api/export user=alice status=200 duration=2340ms db_queries=45
[10:00:30] INFO  req_id=005 path=/api/data user=dave status=200 duration=125ms db_queries=3
[10:01:00] INFO  req_id=006 path=/api/data user=alice status=200 duration=890ms db_queries=3
[10:01:00] WARN  req_id=006 slow_query table=analytics duration=750ms
[10:01:01] INFO  req_id=007 path=/api/data user=bob status=200 duration=135ms db_queries=3
[10:02:00] INFO  req_id=008 path=/api/export user=bob status=200 duration=4560ms db_queries=89
[10:02:05] ERROR req_id=009 path=/api/export user=charlie status=500 duration=30000ms db_queries=156
[10:02:05] ERROR req_id=009 error="OperationalError: database connection pool exhausted"
[10:02:06] ERROR req_id=010 path=/api/data user=dave status=500 duration=5001ms db_queries=0
[10:02:06] ERROR req_id=010 error="OperationalError: QueuePool limit reached"
[10:02:07] INFO  req_id=011 path=/api/data user=alice status=200 duration=145ms db_queries=3
[10:02:30] INFO  req_id=012 path=/api/export user=dave status=200 duration=8900ms db_queries=120
[10:03:00] ERROR req_id=013 path=/api/export user=alice status=500 duration=30000ms db_queries=201
[10:03:00] ERROR req_id=013 error="OperationalError: database connection pool exhausted"

Exercise 15: Performance Debugging Workflow

You have a Flask API endpoint that responds in 50ms in development but takes 5-10 seconds in production. Design a complete debugging plan:

  1. What profiling tools would you use?
  2. What metrics would you collect?
  3. What differences between dev and production might explain the slowdown?
  4. Write three specific prompts you would send to AI at different stages of investigation.

Exercise 16: Debugging Across Services

Your microservice architecture has three services: an API gateway, a user service, and a notification service. A user reports that they did not receive a confirmation email after placing an order. You have logs from all three services:

API Gateway:

[14:23:01] INFO  POST /orders request_id=abc123 user_id=42
[14:23:02] INFO  Order created order_id=ORD-789 request_id=abc123
[14:23:02] INFO  POST /orders response status=201 request_id=abc123

User Service:

[14:23:01] INFO  GET /users/42 request_id=abc123
[14:23:01] INFO  User found user_id=42 email=user@example.com request_id=abc123

Notification Service:

[14:23:02] INFO  Event received type=order_created order_id=ORD-789
[14:23:02] INFO  Looking up notification preferences user_id=42
[14:23:02] WARN  No notification preferences found user_id=42, using defaults
[14:23:02] INFO  Sending email to=user@example.com template=order_confirmation
[14:23:03] INFO  Email queued message_id=msg-456
[14:23:03] INFO  Event processed type=order_created duration=1002ms

Based on these logs, the notification service says it sent the email. Write a structured debugging prompt to AI explaining the situation and asking for diagnostic next steps.

Exercise 17: Reproducing an Intermittent Bug

A test passes 95% of the time but fails intermittently. The test output when it fails:

FAILED tests/test_cache.py::test_concurrent_access
AssertionError: assert 98 == 100

During the test, 100 concurrent tasks each incremented a counter.
Expected final value: 100
Actual value: 98 (varies between 95-100 on each run)

Write a diagnosis of what is likely happening, then craft an AI prompt asking for: 1. Confirmation of your diagnosis 2. A fix for the production code 3. A reliable way to test concurrent behavior

You receive a security audit report indicating that your application is vulnerable to SQL injection. The auditor provided this evidence:

Request: GET /api/users?search='; DROP TABLE users; --
Response: 500 Internal Server Error

Log entry:
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError)
unterminated quoted string at or near "'; DROP TABLE users; --'"

Write an AI prompt that: 1. Explains the vulnerability 2. Shares the relevant code (invent realistic code that would have this vulnerability) 3. Asks for a comprehensive fix, not just a patch for this one endpoint


Tier 4: Evaluation and Synthesis (Exercises 19-24)

Exercise 19: Evaluating AI Debugging Suggestions

An AI assistant provided the following three suggestions for fixing a RecursionError. Evaluate each suggestion for correctness, completeness, and potential side effects:

The buggy code:

def flatten(nested_list):
    result = []
    for item in nested_list:
        if isinstance(item, list):
            result.extend(flatten(item))
        else:
            result.append(item)
    return result

# Fails with: RecursionError: maximum recursion depth exceeded
data = []
current = data
for i in range(2000):
    current.append([])
    current = current[0]

AI Suggestion 1: "Increase the recursion limit with sys.setrecursionlimit(10000)"

AI Suggestion 2: "Convert to an iterative approach using a stack"

AI Suggestion 3: "Add a depth limit parameter: def flatten(nested_list, max_depth=100)"

For each suggestion, state whether you would apply it and why.

Exercise 20: Building a Debugging Checklist

Create a comprehensive debugging checklist for a web application that covers: 1. Client-side issues (5 items) 2. Network issues (5 items) 3. Server-side issues (5 items) 4. Database issues (5 items) 5. Environment/deployment issues (5 items)

For each item, note when AI assistance would be most valuable.

Exercise 21: Debugging Tool Comparison

Compare and contrast the following debugging approaches for each scenario. Which approach would you choose and why?

Scenarios: 1. A function returns incorrect results for specific inputs 2. An application gradually consumes more memory over hours 3. A third-party API integration intermittently fails 4. CSS layout breaks on mobile devices 5. Database queries are slower than expected

Approaches: - A: Print statement debugging - B: Interactive debugger (pdb) - C: Logging and log analysis - D: Profiling tools - E: AI-assisted analysis

Exercise 22: Designing a Debug Helper Tool

Design (on paper or in pseudocode) a Python tool that: 1. Captures the current exception and its stack trace 2. Collects relevant local variables from each stack frame 3. Gathers environment information (Python version, installed packages) 4. Formats everything into a structured AI prompt using the DESCRIBE framework 5. Optionally sends the prompt to an AI API and returns the response

Describe the architecture, main functions, and data flow. Consider privacy implications (what should NOT be sent to an AI API).

Exercise 23: Root Cause Analysis

For each of the following symptoms, propose three possible root causes at different levels (code, architecture, process). Then write the diagnostic AI prompt for the most likely cause.

  1. Symptom: Application response time has gradually increased from 200ms to 2s over the past month
  2. Symptom: Users report seeing other users' data after deploying a new release
  3. Symptom: Background job processor crashes every night at 2 AM
  4. Symptom: File upload feature works for small files but fails for files over 10MB

Exercise 24: Post-Mortem Analysis

A production incident occurred: the application returned 500 errors for 45 minutes before the team identified and fixed the issue. Write a complete post-mortem document that includes:

  1. Incident timeline
  2. Root cause analysis
  3. AI-assisted debugging steps that were used
  4. What AI could have helped identify faster
  5. Action items to prevent recurrence
  6. Improvements to monitoring and logging

Use this scenario: A deployment included a database migration that added a NOT NULL column without a default value, causing all INSERT operations to fail.


Tier 5: Creation and Real-World Application (Exercises 25-30)

Exercise 25: Build an Error Pattern Classifier

Write a Python program that: 1. Takes a Python error traceback as input 2. Classifies it into categories (syntax, type, import, database, network, authentication, etc.) 3. Extracts key information (error type, file, line number, relevant variables) 4. Generates a structured AI debugging prompt using the DESCRIBE template 5. Includes relevant diagnostic questions based on the error category

Test your classifier with at least 5 different error types.

Exercise 26: Create a Log Analysis Pipeline

Build a log analysis tool that: 1. Parses log files in common formats (JSON, key-value, and standard text) 2. Identifies error clusters (groups of related errors) 3. Detects temporal patterns (time-based correlations) 4. Calculates error rates and trends 5. Generates a summary report suitable for sharing with AI for deeper analysis

Process a sample log file with at least 100 entries.

Exercise 27: Interactive Debugging Assistant

Create a command-line tool that acts as an interactive debugging assistant: 1. Accepts a Python file and an error message as input 2. Reads the relevant source code around the error location 3. Checks for common bug patterns (mutable defaults, off-by-one, None returns) 4. Suggests potential fixes based on the error type 5. Generates a formatted debugging session summary

The tool should work without any AI API — it should use pattern matching and heuristics.

Exercise 28: Dependency Conflict Resolver

Build a tool that: 1. Reads a requirements.txt file 2. Checks for version conflicts between packages 3. Queries PyPI for available versions (you may mock this) 4. Suggests compatible version combinations 5. Generates an AI prompt with the conflict details for complex cases that cannot be auto-resolved

Exercise 29: Performance Debugging Dashboard

Create a Python script that: 1. Profiles a given function using cProfile 2. Identifies the top 5 bottleneck functions 3. Calculates percentage breakdowns of time spent 4. Generates an optimization-focused AI prompt with the profiling data 5. Produces a simple text-based report

Test it against a deliberately inefficient function (e.g., one that uses nested loops where a dictionary lookup would suffice).

Exercise 30: Complete Debugging Toolkit

Combine elements from Exercises 25-29 into a unified debugging toolkit with a CLI interface. The toolkit should have subcommands:

debug-toolkit analyze-error <traceback-file>
debug-toolkit analyze-logs <log-file>
debug-toolkit profile <python-file> <function-name>
debug-toolkit check-deps <requirements-file>
debug-toolkit generate-prompt <error-file> --template describe

Each subcommand should produce output formatted for sharing with an AI assistant. Write tests for at least three subcommands.