Bird
0
0

Given this LangChain code snippet, what will be printed when the chain runs?

medium📝 component behavior Q13 of 15
LangChain - LangSmith Observability
Given this LangChain code snippet, what will be printed when the chain runs?
from langchain.callbacks.base import BaseCallbackHandler

class PrintCallback(BaseCallbackHandler):
    def on_llm_start(self, serialized, prompts, **kwargs):
        print(f"LLM started with prompt: {prompts[0]}")

chain = LLMChain(llm=llm, prompt=prompt, callbacks=[PrintCallback()])
chain.run("Hello")
ALLM started with prompt: Hello
BLLM started with prompt: ["Hello"]
CNo output printed
DError: on_llm_start method missing required arguments
Step-by-Step Solution
Solution:
  1. Step 1: Understand the on_llm_start callback parameter

    The 'prompts' argument is a list of prompt strings, so prompts[0] is the first prompt string.
  2. Step 2: Analyze the print statement output

    The print outputs the string with prompts[0], which is the string "Hello" passed to run, but wrapped in a list originally.
  3. Final Answer:

    LLM started with prompt: Hello -> Option A
  4. Quick Check:

    Print prompt string = "Hello" [OK]
Quick Trick: Callbacks receive prompts as list; print first item for prompt text [OK]
Common Mistakes:
MISTAKES
  • Thinking prompts is a string, not a list
  • Expecting no output from callback
  • Confusing method parameters causing errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes