# langchain > Design principle: progressive disclosure and concision. Keep core workflows and navigation here; load detailed samples and long docs from references and runnable scripts only when needed. - Author: cincly - Repository: rzte/skills - Version: 20251222100700 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-07 - Source: https://github.com/rzte/skills - Web: https://mule.run/skillshub/@@rzte/skills~langchain:20251222100700 --- --- name: langchain-python description: Comprehensive workflows for building and debugging Python agents with LangChain and LangGraph: model selection/init, tool definition/execution, middleware injection, short/long-term memory with checkpoints, streaming outputs, strictly structured responses (Pydantic/TypedDict/Dataclass/JSON Schema), RAG and SQL Agent integration, HITL approvals and PII guardrails, evaluation and LangSmith observability. Trigger whenever implementing any of these scenarios in Python or when running the bundled examples/scripts. --- # LangChain + LangGraph (Python) Skill Design principle: progressive disclosure and concision. Keep core workflows and navigation here; load detailed samples and long docs from references and runnable scripts only when needed. ## Directory and resource navigation - API parameters and methods (read on demand): [references/api_list.json](skills/langchain/references/api_list.json) - Runnable example scripts (copy or run directly): - Basic agent: [resources/basic_agent.py](skills/langchain/resources/basic_agent.py) - Dynamic prompt + middleware: [resources/middleware_dynamic_prompt.py](skills/langchain/resources/middleware_dynamic_prompt.py) - Tool error handling: [resources/tool_error_handling.py](skills/langchain/resources/tool_error_handling.py) - Short-term memory (InMemorySaver): [resources/memory_short_term.py](skills/langchain/resources/memory_short_term.py) - Long-term memory (InMemoryStore): [resources/memory_long_term.py](skills/langchain/resources/memory_long_term.py) - Streaming updates (messages and progress): [resources/streaming_examples.py](skills/langchain/resources/streaming_examples.py) - Strict structured output: [resources/structured_output.py](skills/langchain/resources/structured_output.py) - RAG agent: [resources/rag_agent.py](skills/langchain/resources/rag_agent.py) - SQL Agent: [resources/sql_agent.py](skills/langchain/resources/sql_agent.py) - HITL example: [resources/hitl_example.py](skills/langchain/resources/hitl_example.py) - PII guardrails: [resources/guardrails_pii.py](skills/langchain/resources/guardrails_pii.py) - Ops/Troubleshooting scripts (run as needed): - HITL resume: [scripts/hitl_resume.py](skills/langchain/scripts/hitl_resume.py) - PII middleware launcher: [scripts/pii_middleware.py](skills/langchain/scripts/pii_middleware.py) - Message trimming: [scripts/message_trim.py](skills/langchain/scripts/message_trim.py) - Unified tool error wrapper: [scripts/tool_error_wrapper.py](skills/langchain/scripts/tool_error_wrapper.py) ## Setup and environment - Language: Python 3.10+ - Recommended deps: langchain, langgraph, langchain-openai, pydantic, dataclasses, typing-extensions, psycopg (for PostgresSaver), numpy (some examples), tiktoken (optional) - Virtualenv: prefer the project venv interpreter [venv/bin/python](venv/bin/python) when running examples and scripts - Install (using Huawei Cloud PyPI mirror): - Create venv: ``` python3 -m venv venv ``` - Install deps: ``` venv/bin/pip install -i https://repo.huaweicloud.com/repository/pypi/simple/ langchain langgraph langchain-openai pydantic typing-extensions psycopg ``` - Secrets: configure model provider keys (e.g., OPENAI_API_KEY) via environment variables or secure config ## Core workflow (high-level guide) Read detailed API only when needed: [references/api_list.json](skills/langchain/references/api_list.json). Apply the following steps: 1. Model and messages - Initialize chat models with explicit parameters: temperature, max_tokens, timeout, max_retries - Normalize message types: System/Human/AI/Tool; capture tool calls via AIMessage 2. Tools and runtime - Define tool input with `@tool` and `args_schema`; avoid reserved arg names like "config" and "runtime" - The tool runtime may carry context, state stores, and stream writers 3. Middleware - Inject prompts/telemetry before and after requests: dynamic_prompt, wrap_model_call, wrap_tool_call - Enforce single-responsibility: separate prompt enhancement, call monitoring, error wrapping 4. Memory and checkpoints - Short-term memory: InMemorySaver; use a stable, unique thread_id - Long-term store: InMemoryStore put/get/search; tools may read/write the store 5. Streaming outputs - Use agent.stream (updates/messages/custom) to emit tokens and progress - Use get_stream_writer only when necessary and design consumer handling for custom updates 6. Strict structured outputs - Define response schemas via Pydantic/TypedDict/Dataclass/JSON Schema - Coordinate ProviderStrategy and ToolStrategy; avoid pre-binding models that limit structure 7. RAG and SQL Agent - Vector retrieval tools, two-stage retrieval chains, agent-style RAG - Integrate SQLDatabaseToolkit for query-validate-execute loop 8. Guardrails and approvals (HITL/PII) - PII strategies: redact/mask/hash/block; default non-blocking, enable HITL for sensitive ops - HumanInTheLoopMiddleware requires a checkpointer and consistent thread_id 9. Observability and evaluation - Enable LangSmith tracing and connect UI/Studio - Use AgentEvals, GenericFakeChatModel, and InMemorySaver for unit testing ## Scenario quick navigation - Minimal basic agent: run [resources/basic_agent.py](skills/langchain/resources/basic_agent.py) - Dynamic prompt + middleware: run [resources/middleware_dynamic_prompt.py](skills/langchain/resources/middleware_dynamic_prompt.py) - Tool exceptions + unified error output: run [resources/tool_error_handling.py](skills/langchain/resources/tool_error_handling.py) or [scripts/tool_error_wrapper.py](skills/langchain/scripts/tool_error_wrapper.py) - Memory (short/long): run [resources/memory_short_term.py](skills/langchain/resources/memory_short_term.py) and [resources/memory_long_term.py](skills/langchain/resources/memory_long_term.py) - Streaming: run [resources/streaming_examples.py](skills/langchain/resources/streaming_examples.py) - Strict structured responses: run [resources/structured_output.py](skills/langchain/resources/structured_output.py) - RAG and SQL: run [resources/rag_agent.py](skills/langchain/resources/rag_agent.py), [resources/sql_agent.py](skills/langchain/resources/sql_agent.py) - HITL and PII: run [resources/hitl_example.py](skills/langchain/resources/hitl_example.py), [resources/guardrails_pii.py](skills/langchain/resources/guardrails_pii.py) Example commands: ``` venv/bin/python skills/langchain/resources/basic_agent.py venv/bin/python skills/langchain/scripts/message_trim.py ``` ## Common pitfalls and quick troubleshooting - Missing/invalid model key: check env vars/credentials; set retries, timeouts, and rate limits - Model identifier mismatch: verify model name vs driver (e.g., langchain-openai) - Structured output limitations with pre-bound models: avoid pre-binding in strict mode - Tool signature: do not use reserved names "config"/"runtime" - HITL prerequisites: configured checkpointer and stable thread_id - PII strategy selection: choose redact/mask/hash/block per scenario and validate UX - Stream writing: obtain writer only when needed; implement consumer handling - Versions and dependencies: pin major versions; assess compatibility before upgrades Related scripts: - HITL resume: [scripts/hitl_resume.py](skills/langchain/scripts/hitl_resume.py) - PII middleware: [scripts/pii_middleware.py](skills/langchain/scripts/pii_middleware.py) - Message trimming: [scripts/message_trim.py](skills/langchain/scripts/message_trim.py) - Tool error wrapper: [scripts/tool_error_wrapper.py](skills/langchain/scripts/tool_error_wrapper.py) ## Best practices - Explicit parameters: temperature, max_tokens, timeout, max_retries - Separation of concerns: clear middleware responsibilities, avoid coupling - Testability: use InMemorySaver and GenericFakeChatModel for stateful unit tests - Safety and compliance: default non-blocking PII; route sensitive operations via HITL - Schema-first inputs: use Pydantic/TypedDict/Dataclass for tool inputs - Performance and resilience: avoid deep ReAct loops; implement tool timeouts and degrade strategies ## Progressive disclosure Load only what is needed. For details beyond this page, read [references/api_list.json](skills/langchain/references/api_list.json) or run the relevant example/script.