Bird
0
0

You want to use StrOutputParser to handle output from a language model that returns a JSON string. You want to keep the raw JSON string without parsing it into a dictionary. Which approach is best?

hard📝 Application Q15 of 15
LangChain - Output Parsers
You want to use StrOutputParser to handle output from a language model that returns a JSON string. You want to keep the raw JSON string without parsing it into a dictionary. Which approach is best?
AUse a JSON parser to convert the string to a dictionary
BManually split the string by commas to extract keys
CUse StrOutputParser to parse the output text as is
DUse StrOutputParser but call json.loads() on the result
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    You want to keep the JSON string exactly as returned, without converting it to a dictionary.
  2. Step 2: Choose the correct parser

    StrOutputParser returns the text unchanged, so it fits the goal. Using a JSON parser or manual splitting changes the data format.
  3. Final Answer:

    Use StrOutputParser to parse the output text as is -> Option C
  4. Quick Check:

    Keep raw text output = B [OK]
Quick Trick: StrOutputParser keeps output raw, no conversion needed [OK]
Common Mistakes:
  • Parsing JSON when raw string is needed
  • Trying manual string splitting
  • Calling json.loads() unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes