0
0
LangChainframework~20 mins

Custom document loaders in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Loader Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this custom loader's load method?

Consider this custom document loader class using LangChain. What will loader.load() return?

LangChain
from langchain.document_loaders import BaseLoader

class MyLoader(BaseLoader):
    def __init__(self, data):
        self.data = data

    def load(self):
        return [{'page_content': d, 'metadata': {'source': 'custom'}} for d in self.data]

loader = MyLoader(['doc1', 'doc2'])
result = loader.load()
ANone
B[{'page_content': 'doc1', 'metadata': {'source': 'custom'}}, {'page_content': 'doc2', 'metadata': {'source': 'custom'}}]
C[{'content': 'doc1'}, {'content': 'doc2'}]
D['doc1', 'doc2']
Attempts:
2 left
💡 Hint

Look at what the load method returns: a list of dictionaries with specific keys.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a custom loader inheriting from BaseLoader?

Choose the correct syntax for a custom loader class that inherits from BaseLoader and implements load method.

A
class CustomLoader(BaseLoader):
    def load(self):
        return []
B
class CustomLoader:
    def load(self):
        return []
C
class CustomLoader(BaseLoader):
    def load():
        return []
D
class CustomLoader(BaseLoader):
    def load(self, data):
        return data
Attempts:
2 left
💡 Hint

Remember that methods inside classes need self as the first parameter.

🔧 Debug
advanced
2:00remaining
What error does this custom loader code raise?

Given this custom loader code, what error will it raise when calling loader.load()?

LangChain
from langchain.document_loaders import BaseLoader

class BrokenLoader(BaseLoader):
    def load(self):
        return [{'page_content': d} for d in self.data]

loader = BrokenLoader()
result = loader.load()
AReturns empty list []
BTypeError: load() missing 1 required positional argument: 'self'
CSyntaxError: invalid syntax
DAttributeError: 'BrokenLoader' object has no attribute 'data'
Attempts:
2 left
💡 Hint

Check if self.data is defined before use.

state_output
advanced
2:00remaining
What is the value of loader.data after this code runs?

After running this code, what is the value of loader.data?

LangChain
from langchain.document_loaders import BaseLoader

class MyLoader(BaseLoader):
    def __init__(self, data):
        self.data = data

    def load(self):
        return [{'page_content': d} for d in self.data]

loader = MyLoader(['a', 'b'])
loader.data.append('c')
A['a', 'b', 'c']
B['a', 'b']
C['c']
DRaises AttributeError
Attempts:
2 left
💡 Hint

Remember that loader.data is a list passed in and can be modified.

🧠 Conceptual
expert
2:00remaining
Why implement a custom document loader in LangChain?

Which reason best explains why you would create a custom document loader in LangChain?

ATo replace the LangChain core with a different framework
BTo speed up the LangChain library's internal processing
CTo load documents from a unique or unsupported data source not covered by built-in loaders
DTo automatically generate documents without user input
Attempts:
2 left
💡 Hint

Think about when built-in loaders are not enough.