Skip to content

[BUG] Bedrock Agent with code interpreter cannot initialize empty dict.

0

I have created a very basic Bedrock Code interpreter agent with Claude 3.7. No matter what I do or what kind of prompt I give to the agent, when asking the following:

Create an empty dictionary and add some random values.

It iterates over the steps 51 times, each time it crashes because it initializes the dict to null and cannot fix itself and generates the same code over and over, then it fails with the message: "Sorry, I am unable to assist you with this request.". The code it generates is the following:

# Create an empty dictionary
my_dict = null

# Import random module to generate random values
import random

# Add 5 random key-value pairs to the dictionary
for i in range(5):
    key = f"key_{i+1}"
    value = random.randint(1, 100)
    my_dict[key] = value

# Display the dictionary
print("Dictionary with random values:")
print(my_dict)

The error is:

"executionError": "Traceback (most recent call last):\n  File \"/home/runtime/.pyenv/versions/3.11.8/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 308, in raise_error\n    raise self.error_in_exec\n  File \"/home/runtime/.pyenv/versions/3.11.8/lib/python3.11/site-packages/IPython/core/interactiveshell.py\", line 3577, in run_code\n    exec(code_obj, self.user_global_ns, self.user_ns)\n  File \"<ipython-input-1-9b4d55ebca80>\", line 2, in <module>\n    my_dict = null  # This is the correct way to initialize an empty dictionary in Python\n              ^^^^\nNameError: name 'null' is not defined",

If I switch to claude 3.5, same exact prompt, works within the first 2 iterations.

1 Answer
-1

This is a known issue with the Code Interpreter when using certain models. The problem is that the agent is incorrectly initializing a Python dictionary with null, which is not valid Python syntax. In Python, an empty dictionary should be initialized with {} or dict().

When using Claude 3.7 with Code Interpreter, it appears the model is confusing JavaScript/JSON syntax (where null is valid) with Python syntax (where it's not defined). This is causing the repeated failures you're seeing.

As you've discovered, using Claude 3.5 resolves this issue because that model correctly initializes the dictionary with proper Python syntax.

To work around this issue with Claude 3.7, you could try:

  1. Being more explicit in your prompt by specifying the Python syntax: "Create an empty Python dictionary using {} syntax and add some random values"

  2. Providing a code template in your prompt: "Complete this code: my_dict = {} # Add random values here"

  3. Continue using Claude 3.5 for code generation tasks until this issue is addressed

The correct Python code should initialize the dictionary with my_dict = {} or my_dict = dict() rather than my_dict = null.
Sources
Run code in Code Interpreter from Agents - Amazon Bedrock AgentCore
Getting started with AgentCore Code Interpreter by running a hello world example - Amazon Bedrock AgentCore

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.