Hint: Focus on why accuracy matters for agent decisions [OK]
Common Mistakes:
Thinking speed of tool matters more than result accuracy
Ignoring the importance of result correctness
Confusing tool code size with result handling
2. Which of the following is the correct way to check if a tool's execution result is empty in Python before using it?
easy
A. if result is None:
B. if result != None:
C. if result = None:
D. if result == None:
Solution
Step 1: Identify the correct syntax for None comparison in Python
In Python, to check if a variable is None, use 'is None' instead of '==' because None is a singleton.
Step 2: Eliminate incorrect options
if result == None: uses '==', which works but is not recommended. if result = None: uses '=' which is assignment, causing syntax error. if result != None: checks for not None, which is opposite.
Final Answer:
if result is None: -> Option A
Quick Check:
Use 'is None' to check None in Python [OK]
Hint: Use 'is None' to check for None, not '==' or '=' [OK]
Common Mistakes:
Using '=' instead of '==' or 'is' causing syntax errors
tool_result.get('status') returns 'success', so the if condition is True.
Step 2: Calculate length of data list
tool_result['data'] is [1, 2, 3], which has length 3, so print(3) is executed.
Final Answer:
3 -> Option D
Quick Check:
Status is 'success', print length 3 [OK]
Hint: Check condition first, then count list length [OK]
Common Mistakes:
Assuming else branch runs
Confusing get() with direct key access
Expecting KeyError when key exists
4. What is the error in the following code snippet that handles a tool's result?
result = tool.run()
if result != None:
print(result['value'])
else:
print('No result')
medium
A. Using '!=' instead of 'is not' to check None
B. Missing try-except block for key access
C. Using print instead of return
D. No error, code is correct
Solution
Step 1: Analyze None check
Using 'result != None' works but 'result is not None' is preferred; this is not a critical error.
Step 2: Check key access safety
Accessing result['value'] without checking if 'value' exists can cause KeyError if missing; no try-except or key check is present.
Final Answer:
Missing try-except block for key access -> Option B
Quick Check:
Always handle missing keys safely [OK]
Hint: Always check keys or catch exceptions when accessing dict values [OK]
Common Mistakes:
Ignoring possible missing keys causing runtime errors
Thinking '!=' None is always wrong
Confusing print and return usage
5. An AI agent uses a tool that returns a dictionary with keys 'status' and 'output'. Sometimes 'output' can be an empty string or None. Which is the best way to handle the tool's result to safely get meaningful output or fallback to 'No data'?
hard
A. if result.get('status') == 'success' and result.get('output'):
use_output = result['output']
else:
use_output = 'No data'
B. if result['status'] == 'success' and result['output'] != '':
use_output = result['output']
else:
use_output = 'No data'
C. if result.get('status') == 'success' and result['output'] is not None:
use_output = result['output']
else:
use_output = 'No data'
D. if result['status'] == 'success' and result['output']:
use_output = result['output']
else:
use_output = 'No data'
Solution
Step 1: Use safe key access with get()
Using result.get('status') avoids KeyError if 'status' is missing, making code safer.
Step 2: Check output truthiness to handle empty string or None
Checking 'and result.get('output')' ensures output is not None or empty string, both falsy values, so fallback triggers correctly.
Final Answer:
Option A -> Option A
Quick Check:
Safe get() and truthy check handle missing or empty output [OK]
Hint: Use get() and check truthiness for safe, clean handling [OK]
Common Mistakes:
Using direct key access risking KeyError
Checking only for None but missing empty string case