Skip to content

Bug Detector

Ask Nova to identify and fix bugs in Python code.

System Prompt Template

You are an expert {programming language} developer who specializes in debugging and code quality.
Follow these instructions:
{enumerated instructions}

User Prompt Template

## Code ##
{code to be debugged}

Example

Amazon Nova Pro System Prompt

You are an expert Python developer who specializes in debugging and code quality.
Follow these instructions:
1. Read and understand the code provided in ## Code ##
2. List any bugs, potential issues, or code smells in ## Issues ##:
   - Actual bugs that will cause errors
   - Potential runtime issues
   - Style/maintainability concerns
3. Explain the impact of each issue in ## Impact ##
4. Provide the fixed code in ## Fixed Code ## using ```python``` code blocks
5. Add comments explaining the fixes

Amazon Nova Pro User Prompt

## Code ##
def calculate_average(numbers):
    total = 0
    for num in numbers:
        total += num
    return total/len(numbers)

def process_data(data_list):
    results = []
    for item in data_list:
        if type(item) in [int, float]:
            avg = calculate_average(item)  # Bug: passing single number instead of list
            results.append(avg)
        elif type(item) == list:
            if len(item) == 0:  # Bug: no handling of empty list
                results.append(item)
            else:
                results.append(calculate_average(item))
    return results

# Example usage with bugs
data = [
    [1, 2, 3],
    4,
    [],
    [5, 6]
]
print(process_data(data))  # Will raise TypeError

Amazon Nova Pro Sample Response

Response

Issues

  1. Actual Bug:
  2. In process_data, when item is an integer or float, calculate_average(item) is called. This is incorrect because calculate_average expects a list, not a single number.

  3. Actual Bug:

  4. In process_data, empty lists are appended to the results without calculating an average, which may not be the intended behavior.

  5. Potential Runtime Issue:

  6. Division by zero is possible in calculate_average if an empty list is passed, though this is somewhat handled by the second bug above.

  7. Style/Maintainability Concern:

  8. The type checking using type(item) in [int, float] and type(item) == list is not Pythonic. It would be better to use isinstance.

  9. Style/Maintainability Concern:

  10. The function calculate_average does not handle the case where the input list is empty, which should be addressed to avoid division by zero.

Impact

  1. The first bug causes a TypeError because calculate_average is called with a single number instead of a list.
  2. The second bug means that empty lists are not handled correctly, leading to potentially incorrect results or unexpected behavior.
  3. Without proper handling of empty lists, a division by zero error could occur in calculate_average.
  4. Using isinstance instead of type checks makes the code more Pythonic and easier to maintain.
  5. Not handling empty lists in calculate_average can lead to runtime errors and makes the function less robust.

Fixed Code

def calculate_average(numbers):
    if not numbers:  # Check for empty list
        return 0  # or some other appropriate value or behavior
    total = 0
    for num in numbers:
        total += num
    return total / len(numbers)

def process_data(data_list):
    results = []
    for item in data_list:
        if isinstance(item, (int, float)):  # Use isinstance for type checking
            results.append(item)  # Single numbers are appended as is
        elif isinstance(item, list):  # Use isinstance for type checking
            avg = calculate_average(item)
            results.append(avg)
    return results

# Example usage
data = [
    [1, 2, 3],
    4,
    [],
    [5, 6]
]
print(process_data(data))  # Should now work correctly

Comments on Fixes

  • Added a check for empty lists in calculate_average to return 0, avoiding division by zero.
  • Changed type checks in process_data to use isinstance for better practice.
  • Ensured that single numbers are appended directly to the results list without attempting to calculate an average.
  • Handled empty lists by calculating their average as 0 in calculate_average.

API Request

import boto3
import json 

bedrock_runtime = boto3.client(
            service_name="bedrock-runtime",
            region_name="us-west-2"
        )

response = bedrock_runtime.converse(
                modelId='us.amazon.nova-pro-v1:0',
                system = [
                    {
                        "text": """You are an expert Python developer who specializes in debugging and code quality.
                            Follow these instructions:
                            1. Read and understand the code provided in ## Code ##
                            2. List any bugs, potential issues, or code smells in ## Issues ##:
                               - Actual bugs that will cause errors
                               - Potential runtime issues
                               - Style/maintainability concerns
                            3. Explain the impact of each issue in ## Impact ##
                            4. Provide the fixed code in ## Fixed Code ## using ```python``` code blocks
                            5. Add comments explaining the fixes
                            """
                    }
                ],
                messages = [
                {
                    "role": "user",
                    "content": [
                        {
                            "text": """## Code ##
                                def calculate_average(numbers):
                                    total = 0
                                    for num in numbers:
                                        total += num
                                    return total/len(numbers)

                                def process_data(data_list):
                                    results = []
                                    for item in data_list:
                                        if type(item) in [int, float]:
                                            avg = calculate_average(item)  # Bug: passing single number instead of list
                                            results.append(avg)
                                        elif type(item) == list:
                                            if len(item) == 0:  # Bug: no handling of empty list
                                                results.append(item)
                                            else:
                                                results.append(calculate_average(item))
                                    return results

                                # Example usage with bugs
                                data = [
                                    [1, 2, 3],
                                    4,
                                    [],
                                    [5, 6]
                                ]
                                print(process_data(data))  # Will raise TypeError"""
                        }
                    ]
                }
            ],
            inferenceConfig={
                "temperature": 0.1,
                "topP": .99,
                "maxTokens": 1024
            }
            )

print(json.dumps(response, indent=2))
aws bedrock-runtime converse \
  --model-id "us.amazon.nova-pro-v1:0" \
  --system '[
    {
      "text": "You are an expert Python developer who specializes in debugging and code quality.\nFollow these instructions:\n1. Read and understand the code provided in ## Code ##\n2. List any bugs, potential issues, or code smells in ## Issues ##:\n   - Actual bugs that will cause errors\n   - Potential runtime issues\n   - Style/maintainability concerns\n3. Explain the impact of each issue in ## Impact ##\n4. Provide the fixed code in ## Fixed Code ## using ```python``` code blocks\n5. Add comments explaining the fixes"
    }
  ]' \
  --messages '[
    {
      "role": "user",
      "content": [
        {
          "text": "## Code ##\ndef calculate_average(numbers):\n    total = 0\n    for num in numbers:\n        total += num\n    return total/len(numbers)\n\ndef process_data(data_list):\n    results = []\n    for item in data_list:\n        if type(item) in [int, float]:\n            avg = calculate_average(item)  # Bug: passing single number instead of list\n            results.append(avg)\n        elif type(item) == list:\n            if len(item) == 0:  # Bug: no handling of empty list\n                results.append(item)\n            else:\n                results.append(calculate_average(item))\n    return results\n\n# Example usage with bugs\ndata = [\n    [1, 2, 3],\n    4,\n    [],\n    [5, 6]\n]\nprint(process_data(data))  # Will raise TypeError"
        }
      ]
    }
  ]' \
  --inference-config '{
    "temperature": 0.1,
    "topP": 0.99,
    "maxTokens": 512
  }' \
  --region us-west-2
{
  "modelId": "us.amazon.nova-pro-v1:0",
  "system": [
    {
      "text": "You are an expert Python developer who specializes in debugging and code quality.\nFollow these instructions:\n1. Read and understand the code provided in ## Code ##\n2. List any bugs, potential issues, or code smells in ## Issues ##:\n   - Actual bugs that will cause errors\n   - Potential runtime issues\n   - Style/maintainability concerns\n3. Explain the impact of each issue in ## Impact ##\n4. Provide the fixed code in ## Fixed Code ## using ```python``` code blocks\n5. Add comments explaining the fixes"
    }
  ],
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "text": "## Code ##\ndef calculate_average(numbers):\n    total = 0\n    for num in numbers:\n        total += num\n    return total/len(numbers)\n\ndef process_data(data_list):\n    results = []\n    for item in data_list:\n        if type(item) in [int, float]:\n            avg = calculate_average(item)  # Bug: passing single number instead of list\n            results.append(avg)\n        elif type(item) == list:\n            if len(item) == 0:  # Bug: no handling of empty list\n                results.append(item)\n            else:\n                results.append(calculate_average(item))\n    return results\n\n# Example usage with bugs\ndata = [\n    [1, 2, 3],\n    4,\n    [],\n    [5, 6]\n]\nprint(process_data(data))  # Will raise TypeError"
        }
      ]
    }
  ],
  "inferenceConfig": {
    "temperature": 0.1,
    "topP": 0.99,
    "maxTokens": 512
  }
}