LangChain 1.x patterns for chains, tools, memory, and structured outputs
LangChain 1.x patterns for chains, tools, memory, and structured outputs
Build production LangChain applications using LCEL, tools, memory, and structured outputs.
ChatGoogleGenerativeAI).prompt | llm | parser. Use ChatPromptTemplate for templated prompts.PydanticOutputParser for type-safe responses.@tool, bind with llm.bind_tools(), invoke and handle tool calls.RunnableWithMessageHistory with InMemoryChatMessageHistory for session-scoped context.PyPDFLoader, WebBaseLoader, CSVLoader from langchain_community.| Pattern | Example |
|||
| Sequential | chain1 \| chain2 \| chain3 |
| Parallel | RunnableParallel(a=chain1, b=chain2) |
| Conditional | RunnableBranch((condition, chain1), chain2) |
| Fallback | chain.with_fallbacks([backup]) |
| Retry | chain.with_retry(stop_after_attempt=3) |
ainvoke, astream) for I/O| Anti-Pattern | Fix |
|--|--|
| Sync in async context | Use ainvoke not invoke |
| No error handling | Add .with_fallbacks() |
| Hardcoded prompts | Use ChatPromptTemplate |
| No type hints | Use Pydantic models |
Use the docs-langchain MCP server to search for the latest patterns and API references directly:
# Search for specific patterns
response = await client.chat.completions.create(
messages=[{"role": "user", "content": "How do I use RunnableWithMessageHistory?"}],
tools=[{
"type": "mcp",
"name": "docs-langchain",
"command": "npx", # Managed by MCP config
"args": []
}]
)
--project-dir)This skill should be used when strict adherence to the defined process is required.
Category:other