๐ค
LangChain
Avoid common LangChain mistakes โ LCEL gotchas, memory persistence, RAG chunking, and output parser traps.
ๅฎๅ
จ้่ฟ
ๆ่ฝ่ฏดๆ
name: LangChain description: Avoid common LangChain mistakes โ LCEL gotchas, memory persistence, RAG chunking, and output parser traps. metadata: {"clawdbot":{"emoji":"๐ฆ","requires":{"bins":["python3"]},"os":["linux","darwin","win32"]}}
LCEL Basics
|pipes output to next โprompt | llm | parserRunnablePassthrough()forwards input unchanged โ use in parallel branchesRunnableParallelruns branches concurrently โ{"a": chain1, "b": chain2}.invoke()for single,.batch()for multiple,.stream()for tokens- Input must match expected keys โ
{"question": x}not justxif prompt expects{question}
Memory Gotchas
- Memory doesn't auto-persist between sessions โ save/load explicitly
ConversationBufferMemorygrows unbounded โ useConversationSummaryMemoryfor long chats- Memory key must match prompt variable โ
memory_key="chat_history"needs{chat_history}in prompt return_messages=Truefor chat models โFalsereturns string for completion models
RAG Chunking
- Chunk size affects retrieval quality โ too small loses context, too large dilutes relevance
- Chunk overlap prevents cutting mid-sentence โ 10-20% overlap typical
RecursiveCharacterTextSplitterpreserves structure โ splits on paragraphs, then sentences- Embedding dimension must match vector store โ mixing models causes silent failures
Output Parsers
PydanticOutputParserneeds format instructions in prompt โ call.get_format_instructions()- Parser failures aren't always loud โ malformed JSON may partially parse
OutputFixingParserretries with LLM โ wraps another parser, fixes errorswith_structured_output()on chat models โ cleaner than manual parsing for supported models
Retrieval
similarity_searchreturns documents โ.page_contentfor textkparameter controls results count โ more isn't always better, noise increases- Metadata filtering before similarity โ
filter={"source": "docs"}in most vector stores max_marginal_relevance_searchfor diversity โ avoids redundant similar chunks
Agents
- Agents decide tool order dynamically โ chains are fixed sequence
- Tool descriptions matter โ agent uses them to decide when to call
handle_parsing_errors=Trueโ prevents crash on malformed agent output- Max iterations prevents infinite loops โ
max_iterations=10default may be too low
Common Mistakes
- Prompt template variables case-sensitive โ
{Question}โ{question} - Chat models need message format โ
ChatPromptTemplate, notPromptTemplate - Callbacks not propagating โ pass
config={"callbacks": [...]}through chain - Rate limits crash silently sometimes โ wrap in retry logic
- Token count exceeds context โ use
trim_messagesor summarization for long histories
ๅฆไฝไฝฟ็จใLangChainใ๏ผ
- ๆๅผๅฐ้พ่พAI๏ผWeb ๆ iOS App๏ผ
- ็นๅปไธๆนใ็ซๅณไฝฟ็จใๆ้ฎ๏ผๆๅจๅฏน่ฏๆกไธญ่พๅ ฅไปปๅกๆ่ฟฐ
- ๅฐ้พ่พAI ไผ่ชๅจๅน้ ๅนถ่ฐ็จใLangChainใๆ่ฝๅฎๆไปปๅก
- ็ปๆๅณๆถๅ็ฐ๏ผๆฏๆ็ปง็ปญๅฏน่ฏไผๅ