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

/langchain-core-workflow

@0d57af8
Run it yourself

Compose LangChain 1.0 chains with RunnableParallel, RunnableBranch, RunnablePassthrough.assign, and RunnableLambda — correct input/output shapes, debug probes, and typed composition that catches dict-shape bugs before invocation. Use when wiring multi-step chains, parallel retrievals, conditional routing, or threading state through a chain for RAG, classification, or extraction pipelines. Trigger with "runnable parallel", "runnable branch", "langchain rag composition", "passthrough assign", "langchain lcel", "runnable lambda", "debug probe".

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

Nothing lands on disk. Nothing to clean up.

Fork this Skill

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

referencespassthrough-assign-patterns.md

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

Passthrough Assign Patterns

RunnablePassthrough.assign(field=ch) merges a computed field into the input dict without losing any existing keys. It is the primary pattern for threading state through a chain — the input dict grows as it flows, every earlier key stays reachable by later stages.

This reference covers staged context assembly for RAG, the itemgetter variant for typed sub-chains, and anti-patterns around key shadowing.

The core shape

RunnablePassthrough.assign(key=runnable)
# Input:  {"a": 1, "b": 2}
# Output: {"a": 1, "b": 2, "key": runnable.invoke({"a": 1, "b": 2})}

The runnable receives the full input dict and its output is assigned to the new key. This is different from RunnableParallel, which replaces the dict with one keyed only by the parallel branches.

Staged context assembly for RAG

The canonical RAG pipeline stages context in three assigns:

from langchain_core.runnables import RunnablePassthrough

def format_docs(docs: list) -> str:
    return "\n\n".join(f"[{i+1}] {d.page_content}" for i, d in enumerate(docs))

def extract_citations(docs: list) -> list[str]:
    return [d.metadata.get("url", "") for d in docs]

rag_context = (
    RunnablePassthrough.assign(docs=retriever)
    | RunnablePassthrough.assign(
        context=lambda x: format_docs(x["docs"]),
        citations=lambda x: extract_citations(x["docs"]),
    )
)

rag_context.invoke({"question": "..."})
# {"question": "...", "docs": [...], "context": "...", "citations": [...]}

Note that .assign accepts multiple kwargs at once — both computed fields run after the previous stage and both see the full input including docs. Use this when two computed fields derive from the same upstream value.

itemgetter for pulling a field into a typed sub-chain

When a sub-chain expects a concrete input type (string, list), not the whole dict, use itemgetter to extract the field. This is the pattern to pass a dict's question to a retriever that takes a string:

from operator import itemgetter
from langchain_core.runnables import RunnablePassthrough

question_only = itemgetter("question") | retriever  # Runnable[dict, list[Document]]

chain = RunnablePassthrough.assign(docs=question_only)

Without itemgetter, you would pass the full dict to retriever.invoke, which would try to embed the dict and crash (P06 exactly — retriever expects str, sees dict). This is the cleanest shape-matching primitive in LCEL for pulling typed fields into typed sub-chains.

Preserving input alongside transforms in RunnableParallel

RunnablePassthrough() (the bare call, no .assign) is the identity runnable — input passes through unchanged. Inside a RunnableParallel, this keeps the original value alongside computed values:

from langchain_core.runnables import RunnableParallel, RunnablePassthrough

enriched = RunnableParallel(
    summary=summary_chain,
    entities=entity_chain,
    original=RunnablePassthrough(),  # keep the input around
)
# Output: {"summary": "...", "entities": [...], "original": "<input>"}

Use this when a downstream stage needs both a transform and the raw input. The common case is a prompt that quotes the user's original question alongside an LLM-generated classification.

Lambda assigns — short, readable, no-LLM

.assign(field=lambda x: ...) wraps the lambda in a RunnableLambda automatically. This is the clean way to add computed fields from pure-Python logic:

from datetime import datetime

annotated = RunnablePassthrough.assign(
    timestamp=lambda x: datetime.utcnow().isoformat(),
    word_count=lambda x: len(x["question"].split()),
    is_urgent=lambda x: "urgent" in x["question"].lower(),
)

Keep the lambdas short (1 line). If the logic is >3 lines, extract to a named function and pass the function — it traces better and tests are easier.

Anti-pattern: shadowing input keys

# BAD — overwrites the original "question" with a paraphrase
bad = RunnablePassthrough.assign(
    question=paraphrase_chain,   # shadowed!
)

Downstream stages that expected the original question now see the paraphrased version. This is a silent correctness bug — the chain runs fine, but your citations no longer match what the user asked.

Rule: name computed fields distinctly from input keys. If you genuinely need to replace a field, do it via a full RunnableLambda that signals intent, not via a sneaky .assign that looks like augmentation.

Anti-pattern: chained .assign with circular dependencies

# BAD — "context" is computed from "docs", but "docs" is computed from "context"
broken = (
    RunnablePassthrough.assign(docs=lambda x: retrieve(x["context"]))
    | RunnablePassthrough.assign(context=lambda x: format_docs(x["docs"]))
)

At the first .assign, x["context"] does not exist — KeyError. Order matters. Make the dependency graph explicit: compute upstream first, merge into dict, then compute downstream.

Combining with RunnableBranch for computed routes

A clean pattern: use .assign to compute a category, then RunnableBranch to route on it:

routed = (
    RunnablePassthrough.assign(category=classifier)
    | RunnableBranch(
        (lambda x: x["category"] == "refund", refund_chain),
        (lambda x: x["category"] == "shipping", shipping_chain),
        general_chain,
    )
)

The branch runnables all receive the dict with category included — they can use it for further conditional logic, or ignore it.

Shape cheat sheet

Before After .assign(k=ch)
{"a": 1} {"a": 1, "k": ch.invoke({"a": 1})}
{"a": 1, "k": "old"} {"a": 1, "k": ch.invoke({"a": 1, "k": "old"})} (shadowed!)
"not a dict" TypeError — .assign requires dict input

Resources

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 educational patterns and best practices for building LangChain 1.0 pipelines. It focuses on improving observability via debug probes and enhancing reliability through typed composition, with no malicious activity detected.

  • 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 4 allowed tools
ReadWriteEditBash(python:*)
Other metadata
tags
[
  "saas",
  "langchain",
  "langgraph",
  "python",
  "langchain-1.0",
  "lcel",
  "runnables",
  "rag"
]

README badge

README badge for jeremylongshore/tons-of-skills-marketplace/langchain-core-workflow