Challenge - 5 Problems
Structured Output Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this LangChain structured output example?
Consider a LangChain agent that returns a structured JSON output with keys 'name' and 'age'. What will be the output if the agent is given input 'Tell me about Alice, 30 years old'?
LangChain
from langchain.schema import BaseOutputParser class SimpleParser(BaseOutputParser): def parse(self, text: str) -> dict: # Simulate parsing structured output return {"name": "Alice", "age": 30} parser = SimpleParser() result = parser.parse('Tell me about Alice, 30 years old') print(result)
Attempts:
2 left
💡 Hint
Think about what the parse method returns as a dictionary.
✗ Incorrect
The parse method returns a dictionary with keys 'name' and 'age' extracted from the input text. So the output is a dictionary with those values.
❓ state_output
intermediate1:30remaining
What happens if structured output is not used in LangChain?
If a LangChain agent returns plain text instead of structured JSON, what is a likely problem when downstream code tries to use the output?
Attempts:
2 left
💡 Hint
Think about how code expects data to be formatted to use it easily.
✗ Incorrect
Without structured output, the downstream code cannot reliably parse or extract specific information, causing failures or incorrect behavior.
📝 Syntax
advanced2:30remaining
Which option correctly defines a LangChain output parser for structured JSON?
Choose the code snippet that correctly implements a LangChain output parser returning a dictionary from JSON string.
Attempts:
2 left
💡 Hint
Which method safely converts JSON string to dictionary?
✗ Incorrect
Option C uses json.loads which safely parses JSON string to a dictionary. Others either use unsafe eval, wrong type conversion, or string splitting.
🔧 Debug
advanced2:30remaining
Why does this LangChain parser raise a ValueError?
Given this parser code, why does it raise ValueError when parsing input '{"name": "Bob"}'?
class MyParser(BaseOutputParser):
def parse(self, text: str) -> dict:
import json
data = json.loads(text)
return {"name": data["name"], "age": data["age"]}
Attempts:
2 left
💡 Hint
Check if all keys accessed exist in the input JSON.
✗ Incorrect
The input JSON does not have 'age' key, so accessing data['age'] raises KeyError, which can be wrapped or shown as ValueError in some contexts.
🧠 Conceptual
expert3:00remaining
Why is structured output critical in multi-step LangChain workflows?
In a multi-step LangChain workflow where outputs of one step feed into the next, why is structured output important?
Attempts:
2 left
💡 Hint
Think about how data flows and is used between steps.
✗ Incorrect
Structured output guarantees each step receives data in a known format, preventing errors and simplifying processing in complex workflows.