M3 Graded Lab: C1M3_Assignment error: UnboundLocalError

Hi, I get an error in Exercise 1 of the C1M3_Assigment that I don’t understand. When I run:

# Test your code!
unittests.test_generate_research_report_with_tools(generate_research_report_with_tools)

This is the result:

🛠️ arxiv_search_tool({'query': 'Radio observations of recurrent novae', 'max_results': 5})
🛠️ arxiv_search_tool({'query': 'Radio observations of recurrent novae'})
🛠️ arxiv_search_tool({'query': 'Radio observations of recurrent novae', 'max_results': 5})
🛠️ tavily_search_tool({'query': 'Radio observations of recurrent novae', 'max_results': 5})
🛠️ arxiv_search_tool({'query': 'Radio observations of recurrent novae'})
🛠️ arxiv_search_tool({'query': 'Radio observations of recurrent novae'})
🛠️ tavily_search_tool({'query': 'Radio observations of recurrent novae', 'max_results': 5, 'include_images': False})
🛠️ arxiv_search_tool({'query': 'radio observations of recurrent novae', 'max_results': 5})
🛠️ tavily_search_tool({'query': 'radio observations of recurrent novae', 'max_results': 5, 'include_images': False})
🛠️ arxiv_search_tool({'query': 'Radio observations of recurrent novae', 'max_results': 5})
🛠️ tavily_search_tool({'query': 'Radio observations of recurrent novae', 'max_results': 5})
🛠️ arxiv_search_tool({'query': 'Radio observations of recurrent novae', 'max_results': 5})
🛠️ tavily_search_tool({'query': 'Radio observations of recurrent novae', 'max_results': 5})
🛠️ arxiv_search_tool({'query': 'Radio observations of recurrent novae', 'max_results': 5})
🛠️ tavily_search_tool({'query': 'Radio observations of recurrent novae', 'max_results': 5, 'include_images': False})
🛠️ arxiv_search_tool({'query': 'Radio observations of recurrent novae', 'max_results': 5})
🛠️ tavily_search_tool({'query': 'Radio observations of recurrent novae', 'max_results': 5, 'include_images': False})
Failed test case: generate_research_report_with_tools raised UnboundLocalError: cannot access local variable 'final_text' where it is not associated with a value.
Expected: no exception
Got: cannot access local variable 'final_text' where it is not associated with a value

I don’t understand the UnboundLocalError. The local variable ‘final_text’ is outside of the code that I’m supposed to edit for the assignment.

how did you recall tool_call_id and content. Also hope you have used the correct name assigned to the tool

please don’t post any grade function codes here, it is against community guidelines

1 Like

hi @B_rad

recalling tool_call_id as id is an error as id is global variable in the chatcompletionmessage, check the section just before grade function code cell. recall it as per the instruction mentions

every call has an attribute called id, hint it is part of the tool_call_id name

I don’t understand why the recall of tool_call_id as “id” is an error. It’s a global variable in chatcompletionmessage, but why is that an error?

I don’t understand the instruction or the hint either. In the instructions, tool_name is defined in the for loop. In the exercise, there is no definition for tool_call_id in the for loop. Do I need to write a new for loop outside of the new_msg syntax?

Hi B_rad,

Note that you are looping over msg.tool_calls. You will need the loop variable to access the id of the current tool call.

The issue with the final_text variable looks like a bug in the provided code (not in your code). If the LLM does not call a tool and it does not return any text, the final_text variable gets assigned None and the grader produces a ‘Failed test case’. I get this ‘Failed test case’ when I set msg.tool_calls to None in the provided code. I will make an issue of this on the course github.

I hope this helps.

Helps a bit, but I can’t really test if my solution works with that bug in the way.

Thanks for raising the issue.

This bug should not often have an effect as it depends on the call to the LLM which usually succeeds. So if you get the error message again and again you may have to check your code. If you cannot find a solution, you can send me your code with a direct message. Otherwise, I would suggest just to continue finishing the lab and see if any issue comes up when you submit.

Hi @B_rad, can you send me your code via DM? I don’t quite understand the cause of this error

Thanks @a-zarta, I have DM’d you

Posting here for visibility in case other learners face similar issues.

The main issue here was that messages was being hardcoded when creating the chat with the LLM. This resulted in the messages the LLM received not getting updated in each iteration, so the LLM pretty much always lost all the info from the previous step and decided to keep going which made the final_text variable to never be assigned.

We’ll make some changes to handle this more gracefully in the future but in the meantime please avoid hardcoding stuff and try to use the provided variables whenever possible.

3 Likes