Bird
0
0

Why does this code raise a NameError: name 'cb' is not defined?

medium📝 Debug Q7 of 15
LangChain - LangSmith Observability
Why does this code raise a NameError: name 'cb' is not defined?
chain.run("Hello")
print(cb.total_cost)
Atotal_cost is not accessible outside the run method
BThe callback 'cb' was never created or assigned
CYou must call chain.run inside a callback context
Dcb is a reserved keyword and cannot be used
Step-by-Step Solution
Solution:
  1. Step 1: Check variable definition

    The variable cb is used but never assigned or created in the code.
  2. Step 2: Understand NameError cause

    Using an undefined variable causes NameError in Python.
  3. Final Answer:

    The callback 'cb' was never created or assigned -> Option B
  4. Quick Check:

    Undefined variable causes NameError = B [OK]
Quick Trick: Define callback before using it [OK]
Common Mistakes:
MISTAKES
  • Using callback variable without assignment
  • Assuming run creates callback automatically
  • Misunderstanding Python variable scope

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes