Index of every page on this site: llms.txt
All skills
jeremylongshore avatar

/langchain-sdk-patterns

@0d57af8
Run it yourself

Compose LangChain 1.0 Python runnables with the production defaults the docs do not warn about: parallel batching, narrow fallbacks, and brace-safe prompts. Use when building an LCEL chain with RunnableSequence / RunnableParallel, adding resilience via .with_fallbacks(), tuning throughput with .batch() or .abatch(), or wrapping user input in a prompt template. Trigger with "langchain runnable", "with_fallbacks", "langchain batch", "runnable sequence", "lcel", "runnableparallel", "chain composition".

Use this Skill: https://skilld.dev/gh/jeremylongshore/tons-of-skills-marketplace/langchain-sdk-patterns

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesrunnable-composition-matrix.md

≈1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Runnable Composition Matrix

LangChain 1.0 exposes five composition primitives. Picking the wrong one is rarely a crash — it is usually a quiet performance or maintainability loss. This is the decision table.

The five primitives

Primitive Input Output Runs When to use
RunnableSequence (a | b) Same as a.input Same as b.output Sequentially Linear pipeline: prompt → llm → parser
RunnableParallel({"x": a, "y": b}) Forwarded to both {"x": a.output, "y": b.output} Concurrently Fan-out enrichment: summary + entities + sentiment from one doc
RunnableBranch((cond, chain), ..., default) Any First matching branch's output Sequential dispatch Per-input routing: route to a cheap or expensive model based on input size
RunnablePassthrough() Any Same as input Identity Keep the original value alongside a transform in a RunnableParallel
RunnableLambda(fn) fn's arg fn's return Wraps arbitrary Python Escape hatch for ad-hoc transforms — avoid for anything you want traced well

Shape conventions

Every chain declares an input schema and an output schema. Keep them explicit:

from typing import TypedDict

class TriageIn(TypedDict):
    ticket_text: str
    customer_tier: str

class TriageOut(TypedDict):
    priority: str
    suggested_reply: str

RunnableSequence propagates these types through |. When you drop a RunnableLambda in the middle, annotate its signature — otherwise the chain's inferred schema collapses to Any.

Fan-out with RunnableParallel

from langchain_core.runnables import RunnableParallel, RunnablePassthrough

enrich = RunnableParallel(
    summary=summary_chain,         # Runnable[str, str]
    entities=entity_chain,          # Runnable[str, list[str]]
    original=RunnablePassthrough(), # keep the input
)

# Output: {"summary": "...", "entities": [...], "original": "..."}

All three sub-chains run concurrently. If one is 5x slower than the others, your wall-clock is the slow one — not the sum. This is the single biggest latency win in an LCEL pipeline after correct .batch(max_concurrency=...).

Per-input routing with RunnableBranch

from langchain_core.runnables import RunnableBranch

router = RunnableBranch(
    (lambda x: len(x["text"]) < 500, fast_chain),  # cheap model
    (lambda x: x["tier"] == "enterprise", premium_chain),
    default_chain,  # mandatory — unmatched inputs go here
)

The default branch is required. Without it you get a silent fallthrough — exactly the kind of error P56 covers for LangGraph conditional edges, and the runnable version has the same footgun.

When RunnableLambda is the wrong answer

RunnableLambda(fn) wraps any Python callable but loses LangSmith tracing fidelity — the input/output show up as opaque blobs. For data shaping that must be observable, prefer:

  • RunnablePassthrough.assign(key=chain) — merges a computed field into the dict
  • itemgetter("key") | runnable — pulls a field and runs a typed chain on it
  • A concrete BaseRunnable subclass when the lambda has >3 lines of logic

Input/output shape conventions table

Pattern Typical input Typical output
prompt | llm | StrOutputParser() dict[str, Any] str
prompt | llm.with_structured_output(T) dict[str, Any] T (Pydantic)
RunnableParallel(a=ch1, b=ch2) dict[str, Any] {"a": ..., "b": ...}
RunnablePassthrough.assign(x=chain) dict[str, Any] input dict with "x" added
RunnableBranch((cond, ch), default) anything cond accepts ch.output or default.output

Composition rule of thumb

  1. Linear flow → | (builds a RunnableSequence)
  2. Independent sub-tasks on the same input → RunnableParallel
  3. Input-dependent dispatch → RunnableBranch
  4. Merge computed fields → RunnablePassthrough.assign
  5. One-off Python glue → RunnableLambda (but consider a subclass)

When in doubt, prefer composition primitives over lambdas: they trace better, they serialize for inspection, and their schemas propagate.

Source: SKILL.md on GitHub

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    This skill provides production-grade patterns for LangChain 1.0, focusing on batching, resilience, and prompt security. It is educational and contains no security issues.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

Signed by skilld at 0d57af8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 2 months ago
What it can do
Reads files Edits files Runs commands
version
2.7.0
author
Jeremy Longshore <jeremy@intentsolutions.io>
compatibility
Designed for Claude Code
All 5 allowed tools
ReadWriteEditBash(python:*)Bash(pip:*)
Other metadata
tags
[
  "saas",
  "langchain",
  "langgraph",
  "python",
  "langchain-1.0",
  "lcel",
  "runnables"
]

README badge

README badge for jeremylongshore/tons-of-skills-marketplace/langchain-sdk-patterns