๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ
ๅฐ้พ™่™พๅฐ้พ™่™พAI
๐Ÿค–

LangChain

Avoid common LangChain mistakes โ€” LCEL gotchas, memory persistence, RAG chunking, and output parser traps.

ไธ‹่ฝฝ2.1k
ๆ˜Ÿๆ ‡2
็‰ˆๆœฌ1.0.0
AI ๆ™บ่ƒฝไฝ“
ๅฎ‰ๅ…จ้€š่ฟ‡

ๆŠ€่ƒฝ่ฏดๆ˜Ž


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 | parser
  • RunnablePassthrough() forwards input unchanged โ€” use in parallel branches
  • RunnableParallel runs branches concurrently โ€” {"a": chain1, "b": chain2}
  • .invoke() for single, .batch() for multiple, .stream() for tokens
  • Input must match expected keys โ€” {"question": x} not just x if prompt expects {question}

Memory Gotchas

  • Memory doesn't auto-persist between sessions โ€” save/load explicitly
  • ConversationBufferMemory grows unbounded โ€” use ConversationSummaryMemory for long chats
  • Memory key must match prompt variable โ€” memory_key="chat_history" needs {chat_history} in prompt
  • return_messages=True for chat models โ€” False returns 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
  • RecursiveCharacterTextSplitter preserves structure โ€” splits on paragraphs, then sentences
  • Embedding dimension must match vector store โ€” mixing models causes silent failures

Output Parsers

  • PydanticOutputParser needs format instructions in prompt โ€” call .get_format_instructions()
  • Parser failures aren't always loud โ€” malformed JSON may partially parse
  • OutputFixingParser retries with LLM โ€” wraps another parser, fixes errors
  • with_structured_output() on chat models โ€” cleaner than manual parsing for supported models

Retrieval

  • similarity_search returns documents โ€” .page_content for text
  • k parameter controls results count โ€” more isn't always better, noise increases
  • Metadata filtering before similarity โ€” filter={"source": "docs"} in most vector stores
  • max_marginal_relevance_search for 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=10 default may be too low

Common Mistakes

  • Prompt template variables case-sensitive โ€” {Question} โ‰  {question}
  • Chat models need message format โ€” ChatPromptTemplate, not PromptTemplate
  • Callbacks not propagating โ€” pass config={"callbacks": [...]} through chain
  • Rate limits crash silently sometimes โ€” wrap in retry logic
  • Token count exceeds context โ€” use trim_messages or summarization for long histories

ๅฆ‚ไฝ•ไฝฟ็”จใ€ŒLangChainใ€๏ผŸ

  1. ๆ‰“ๅผ€ๅฐ้พ™่™พAI๏ผˆWeb ๆˆ– iOS App๏ผ‰
  2. ็‚นๅ‡ปไธŠๆ–นใ€Œ็ซ‹ๅณไฝฟ็”จใ€ๆŒ‰้’ฎ๏ผŒๆˆ–ๅœจๅฏน่ฏๆก†ไธญ่พ“ๅ…ฅไปปๅŠกๆ่ฟฐ
  3. ๅฐ้พ™่™พAI ไผš่‡ชๅŠจๅŒน้…ๅนถ่ฐƒ็”จใ€ŒLangChainใ€ๆŠ€่ƒฝๅฎŒๆˆไปปๅŠก
  4. ็ป“ๆžœๅณๆ—ถๅ‘ˆ็Žฐ๏ผŒๆ”ฏๆŒ็ปง็ปญๅฏน่ฏไผ˜ๅŒ–

็›ธๅ…ณๆŠ€่ƒฝ