๐Debugging Missionยทbeginnerยทโฑ๏ธ 25โ40mยทโญ 150 XP
M-001Build an API Response Router
Description
Nebula Corp is building an AI-powered support system. When a customer message comes in, it needs to: (1) call the LLM API to classify the message intent, (2) parse the structured response, and (3) route to the correct handler. The current implementation has broken parsing, missing error handling, and routes everything to the wrong handler. Fix the router so it correctly classifies, parses, and routes messages.
Symptoms
wrong-outputbuildClassificationPrompt returns a vague prompt with no JSON format instruction, so the LLM returns free-form text
errorparseClassification crashes with SyntaxError because LLM responses often include markdown code fences around JSON
wrong-outputrouteToHandler receives the full classification object but tries to use it as a string key, returning undefined
crashrouteToHandler has no fallback handler, so unknown intents cause a crash when calling undefined as a function
Expected Behavior (5)
Prompt requests JSON output
The prompt must instruct the model to respond with JSON containing an 'intent' field
Input:buildClassificationPrompt('I need a refund')
Expected:CONTAINS_ALL:JSON,intent
Parser handles markdown fences
The parser must strip markdown code fences before parsing JSON
Input:parseClassification('```json\n{"intent":"billing","confidence":0.9}\n```')
Expected:{"intent":"billing","confidence":0.9}
Parser handles clean JSON
The parser must also handle JSON without code fences
Input:parseClassification('{"intent":"technical","confidence":0.85}')
Expected:{"intent":"technical","confidence":0.85}
Router extracts intent field
The router must read the intent property from the classification object
Input:routeToHandler({intent:'billing',confidence:0.9})
Expected:TYPEOF:function
Router has fallback for unknown intents
Unknown intents should fall back to the general handler instead of returning undefined
Input:routeToHandler({intent:'unknown',confidence:0.3})
Expected:TYPEOF:function
Related Lessons
Click Run / Check to validate your solution