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.

referencesprompt-template-escaping.md

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

Prompt Template Escaping

ChatPromptTemplate.from_messages defaults to template_format="f-string". The f-string parser treats every { in every string as a variable marker — so the moment a user pastes JSON, code, or a Markdown block with {...}, the chain raises KeyError at invoke time (pain-catalog P57).

The failure mode

from langchain_core.prompts import ChatPromptTemplate

template = ChatPromptTemplate.from_messages([
    ("system", "Reply with a one-line summary"),
    ("human", "Error: {user_message}"),
])

template.invoke({"user_message": "Server returned {\"error\": \"oops\"}"})
# KeyError: '"error"'

The template parser reads {"error" in the rendered string as a variable named "error" — which does not exist in the input dict.

Fix 1: Switch to Jinja2

Jinja2 uses {{ var }} for substitution and treats bare { as literal:

template = ChatPromptTemplate.from_messages(
    [
        ("system", "Reply with a one-line summary"),
        ("human", "Error: {{ user_message }}"),
    ],
    template_format="jinja2",
)

template.invoke({"user_message": 'Server returned {"error": "oops"}'})
# OK — renders correctly

Use jinja2 whenever a template variable can contain user-provided free text. Support tickets, transcripts, code blocks, JSON payloads, logs, chat history — anything not authored by you.

Fix 2: Escape braces in f-string mode

If you need to keep f-string format (for example because the rest of the codebase uses it), escape literal braces by doubling them:

template = ChatPromptTemplate.from_messages([
    ("system", "Output JSON matching the schema {{'name': str, 'age': int}}"),
    ("human", "{user_message}"),
])

Only the {var} sites remain as variables; {{ and }} render as literal { and }. This works for instructions you write but not for user input at runtime — if the user's text contains {, it is still parsed.

Fix 3: MessagesPlaceholder for chat history

A chat history is a list[BaseMessage], not a string. Never render it with f-string substitution. Use MessagesPlaceholder:

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

template = ChatPromptTemplate.from_messages([
    ("system", "You are a helpful assistant."),
    MessagesPlaceholder("history"),
    ("human", "{{ question }}"),
], template_format="jinja2")

template.invoke({
    "history": [prior_human, prior_ai],   # list[BaseMessage]
    "question": "What did I just ask?",
})

MessagesPlaceholder("history") inserts the message list at that position without running either template engine over its contents.

When f-string is correct

For trusted, template-author-written instructions, f-string is fine and faster. Rule of thumb: if every {var} corresponds to a dict key whose value is a fixed-shape ID, number, or short identifier that cannot contain {, f-string is the right choice. If any value is free text from a user, an LLM output, a log, or a document, switch to jinja2.

Jinja2 side effects

Jinja2 supports control flow:

tmpl = """
{% if tier == "enterprise" %}
You have access to advanced features.
{% else %}
Standard tier response.
{% endif %}

User question: {{ question }}
"""

Handy for conditional system prompts. Keep logic simple — complex branching belongs in a RunnableBranch, not in a template.

Testing prompt templates

Always unit-test templates against adversarial inputs:

def test_template_survives_json_input():
    rendered = template.invoke({
        "user_message": '{"nested": {"json": true}}',
    })
    assert "nested" in str(rendered)

def test_template_survives_code_block():
    rendered = template.invoke({
        "user_message": "def f(x): return {x: 1}",
    })
    assert "return" in str(rendered)

If these pass, your template is brace-safe. If they throw KeyError, flip to jinja2 or escape the braces.

Migration checklist

  1. Grep for ChatPromptTemplate.from_messages
  2. Identify which templates have variables that can contain user input
  3. Add template_format="jinja2" and change {var} to {{ var }} in those
  4. Leave trusted internal templates on f-string for speed
  5. Add an adversarial-input unit test per template

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