Challenge - 5 Problems
LangChain Tools 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 tool execution?
Consider this LangChain tool that adds two numbers. What will be the output when the tool is called with inputs 3 and 5?
LangChain
from langchain.tools import Tool def add_numbers(a: int, b: int) -> int: return a + b add_tool = Tool( name="Adder", func=add_numbers, description="Adds two numbers" ) result = add_tool.func(3, 5)
Attempts:
2 left
💡 Hint
Think about what the add_numbers function returns when given 3 and 5.
✗ Incorrect
The add_numbers function returns the sum of its two inputs. Calling add_tool.func(3, 5) returns 8.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a LangChain tool with a synchronous function?
You want to create a LangChain tool that synchronously reverses a string. Which code snippet correctly defines this tool?
Attempts:
2 left
💡 Hint
Remember that synchronous functions do not use async keywords.
✗ Incorrect
Option A defines a synchronous lambda function that returns the reversed string slice. Options A and C incorrectly use async syntax. Option A tries to call list.reverse() on a string, which is invalid.
🔧 Debug
advanced2:00remaining
What error does this LangChain tool code raise?
This tool is intended to fetch current weather but has a bug. What error will it raise when called?
LangChain
from langchain.tools import Tool import requests def get_weather(city: str) -> str: response = requests.get(f"https://api.weather.com/{city}") return response.json()['temp'] weather_tool = Tool(name="Weather", func=get_weather, description="Gets weather") result = weather_tool.func('London')
Attempts:
2 left
💡 Hint
Check what keys the JSON response might have and if 'temp' exists.
✗ Incorrect
The code assumes the JSON response has a 'temp' key, but if the API returns a different structure or error, accessing 'temp' raises KeyError.
❓ state_output
advanced2:00remaining
What is the value of 'counter' after running this LangChain tool multiple times?
This tool increments a counter each time it is called. What is the value of 'counter' after calling tool.func() three times?
LangChain
from langchain.tools import Tool counter = 0 def increment_counter() -> int: global counter counter += 1 return counter increment_tool = Tool(name="Incrementer", func=increment_counter, description="Increments counter") increment_tool.func() increment_tool.func() increment_tool.func()
Attempts:
2 left
💡 Hint
Think about how the global variable 'counter' changes with each call.
✗ Incorrect
Each call to increment_counter increases the global counter by 1. After three calls, counter is 3.
🧠 Conceptual
expert2:00remaining
Which option best describes the role of a Tool in LangChain agents?
In LangChain, what is the primary purpose of creating a Tool for an agent?
Attempts:
2 left
💡 Hint
Think about how agents use tools to extend their capabilities.
✗ Incorrect
Tools in LangChain are functions with descriptions that agents can call to perform tasks like calculations, API calls, or data retrieval during their reasoning.