Bird
0
0

What is the main issue with this code snippet for streaming in LangChain?

medium📝 Debug Q14 of 15
LangChain - Production Deployment
What is the main issue with this code snippet for streaming in LangChain?
llm = OpenAI(streaming=True, callbacks=PrintTokens())
llm('Test')
AThe PrintTokens class is missing required methods.
Bstreaming=True is not a valid parameter for OpenAI.
CCallbacks must be passed as a list, not a single instance.
DThe llm call should be awaited with async syntax.
Step-by-Step Solution
Solution:
  1. Step 1: Check callback parameter type

    LangChain expects callbacks as a list, even if only one handler is used.
  2. Step 2: Identify error cause

    Passing callbacks=PrintTokens() (not in a list) causes a type error or unexpected behavior.
  3. Final Answer:

    Callbacks must be passed as a list, not a single instance. -> Option C
  4. Quick Check:

    Callbacks = list of handlers [OK]
Quick Trick: Always wrap callbacks in a list, even if one [OK]
Common Mistakes:
MISTAKES
  • Passing a single callback object directly
  • Assuming streaming=True is invalid
  • Forgetting to implement callback methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes