Bird
0
0

Given this code snippet using streaming:

medium📝 component behavior Q4 of 15
LangChain - Production Deployment
Given this code snippet using streaming:
from langchain.callbacks.base import BaseCallbackHandler

class PrintHandler(BaseCallbackHandler):
    def on_llm_new_token(self, token: str, **kwargs):
        print(token, end='')

handler = PrintHandler()
llm = OpenAI(streaming=True, callbacks=[handler])
llm('Hello')

What will be the output behavior?
ATokens print one by one immediately as they arrive.
BNothing prints until the full response is ready.
CAn error occurs because on_llm_new_token is missing.
DTokens print only after the entire response finishes.
Step-by-Step Solution
Solution:
  1. Step 1: Understand on_llm_new_token callback

    This method is called for each new token during streaming.
  2. Step 2: Analyze print behavior

    Printing tokens with end='' outputs tokens immediately without newline.
  3. Final Answer:

    Tokens print one by one immediately as they arrive. -> Option A
  4. Quick Check:

    Streaming with on_llm_new_token prints tokens live [OK]
Quick Trick: on_llm_new_token prints tokens as they stream in [OK]
Common Mistakes:
MISTAKES
  • Thinking output waits for full response
  • Assuming missing callback causes error
  • Confusing token printing timing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes