agentsclimarketplace

Langchain model inference

Skill jeremylongshore/claude-code-plugins-plus-skills/plugins/saas-packs/langchain-py-pack/skills/langchain-model-inference

'Invoke Claude, GPT-4o, and Gemini through LangChain 1.0 without tripping onFrom its SKILL.md

Install
npx -y skills add jeremylongshore/claude-code-plugins-plus-skills --skill langchain-model-inference

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

What its file declares

Copied from the file, not written here

The file declares its own license as MIT. That is the author’s claim about this one file, and it is not the same thing as the license GitHub reports for the repository, which is listed with the other numbers below.

SKILL.md

10.5 KB, ~2.5k tokens by cl100k_base, as published. Nobody here has run it

LangChain Model Inference (Python)

Overview

AIMessage.content is a str on simple OpenAI calls and a list[dict] on Claude the instant any tool_use, thinking, or image block enters the response. Code that does message.content.lower() crashes with AttributeError: 'list' object has no attribute 'lower' — the #1 first-production-call LangChain 1.0 bug on Anthropic. And that is one of four separate "content shape" pitfalls in this skill:

  • P02 — AIMessage.content list-vs-string divergence
  • P03 — with_structured_output(method="function_calling") silently drops Optional[list[X]] fields on ~40% of real schemas
  • P05 — temperature=0 is not deterministic on Anthropic even though it is on OpenAI
  • P58 — Claude expects the system message at position 0; middleware that reorders messages makes it silently ignored

This skill walks through ChatAnthropic, ChatOpenAI, and ChatGoogleGenerativeAI initialization; model routing; token counting that is actually correct during streaming; content-block iteration; and a decision tree for with_structured_output methods that holds up on real schemas. Pin: langchain-core 1.0.x, langchain-anthropic 1.0.x, langchain-openai 1.0.x, langchain-google-genai 1.0.x. Pain-catalog anchors: P01, P02, P03, P04, P05, P53, P54, P58, P63, P64, P65.

Prerequisites

  • Python 3.10+
  • langchain-core >= 1.0, < 2.0
  • At least one provider package: pip install langchain-anthropic langchain-openai
  • Provider API key(s): ANTHROPIC_API_KEY, OPENAI_API_KEY, GOOGLE_API_KEY

Instructions

Step 1 — Initialize a chat model with explicit, version-safe defaults

from langchain_anthropic import ChatAnthropic
from langchain_openai import ChatOpenAI

claude = ChatAnthropic(
    model="claude-sonnet-4-6",
    temperature=0,
    max_tokens=4096,
    timeout=30,       # seconds. Default is None — hangs forever on provider stall.
    max_retries=2,    # Retries, not attempts. See P30 in pain catalog.
)

gpt4o = ChatOpenAI(
    model="gpt-4o",
    temperature=0,
    timeout=30,
    max_retries=2,
)

Explicit timeout and max_retries are not optional in production — the defaults are wrong for every workload we have measured. max_retries=6 (the default on ChatOpenAI) means a single logical call can bill as 7 requests on flaky networks.

Step 2 — Iterate AIMessage.content as typed blocks, not strings

from langchain_core.messages import AIMessage

def extract_text(msg: AIMessage) -> str:
    """Safe on both provider shapes. Works for streaming deltas too.

    Handles both dict blocks (provider-native) and typed block objects
    (LangChain 1.0 wrappers) — which Gemini, OpenAI tools, and future
    SDK versions may return.
    """
    if isinstance(msg.content, str):
        return msg.content
    parts = []
    for block in msg.content:
        # Block may be a dict (provider-native) or a typed object (1.0 wrapper)
        block_type = block.get("type") if isinstance(block, dict) else getattr(block, "type", None)
        if block_type == "text":
            parts.append(block["text"] if isinstance(block, dict) else block.text)
    return "".join(parts)

AIMessage.text() (1.0+) does this for you in most cases — prefer it. Roll your own only when you need to filter by block type (tool_use, image, thinking). See Content Blocks for the full block-type reference and streaming-delta shape.

Step 3 — Route across providers with a factory, not a conditional

from langchain_core.language_models import BaseChatModel

# Version-safe defaults applied to every model the factory builds.
# Callers can override via **kwargs.
_SAFE_DEFAULTS = {"timeout": 30, "max_retries": 2}

def chat_model(provider: str, **kwargs) -> BaseChatModel:
    defaults = {**_SAFE_DEFAULTS, **kwargs}  # caller's kwargs win
    if provider == "anthropic":
        return ChatAnthropic(model="claude-sonnet-4-6", **defaults)
    if provider == "openai":
        return ChatOpenAI(model="gpt-4o", **defaults)
    if provider == "gemini":
        from langchain_google_genai import ChatGoogleGenerativeAI
        return ChatGoogleGenerativeAI(model="gemini-2.5-pro", **defaults)
    raise ValueError(f"Unknown provider: {provider!r}")

A factory centralizes the version-safe defaults from Step 1 (timeout=30, max_retries=2) and the structured-output method pick from Step 5. Chains depend on the BaseChatModel protocol, not the concrete class. Callers override with chat_model("openai", timeout=60) when they need it.

Step 4 — Count tokens correctly during streaming

ChatAnthropic.stream() does not populate response_metadata["token_usage"] until the stream closes (P01). If your cost dashboard reads on_llm_end, it lags by the stream duration. Use astream_events(version="v2"):

async for event in claude.astream_events({"input": "..."}, version="v2"):
    if event["event"] == "on_chat_model_stream":
        chunk = event["data"]["chunk"]
        if hasattr(chunk, "usage_metadata") and chunk.usage_metadata:
            meter.record(chunk.usage_metadata["input_tokens"],
                         chunk.usage_metadata["output_tokens"])

See Token Accounting for per-provider differences (Anthropic reports input/output/cache separately; OpenAI aggregates; Gemini reports completion-only on stream start).

Step 5 — Pick the right with_structured_output method

ProviderModel classRecommended methodWhy
AnthropicClaude 3.5+, 4.xjson_schemaProvider-enforced, supports $ref and unions
OpenAIGPT-4o, GPT-4-turbojson_schemaStrict schema, additionalProperties: false enforced
OpenAIGPT-3.5, legacyfunction_callingPre-json_schema fallback
GeminiGemini 2.5 Pro/Flashjson_schemaNative structured output in 1.0+
AnyOlder or unknownjson_mode + Pydantic validate + retryJSON-parseable only, no schema enforcement (P54)
from pydantic import BaseModel, ConfigDict

class Plan(BaseModel):
    model_config = ConfigDict(extra="ignore")  # P53 — models add helpful extra fields
    steps: list[str]
    estimated_minutes: int

structured = claude.with_structured_output(Plan, method="json_schema")
plan = structured.invoke("Plan a 3-step deploy")

Avoid Optional[list[X]] fields — they silently return None on some providers (P03). See Structured Output Methods for a concrete comparison matrix and fallback pattern.

Output

  • Chat models initialized with explicit timeouts (30s) and max_retries=2
  • Content-safe extractor that handles both str and list[dict] shapes
  • Factory-based routing with a single BaseChatModel return type
  • Streaming token counter that reports incrementally, not at stream end
  • with_structured_output chosen per provider capability, with Pydantic validation

Error Handling

ErrorCauseFix
AttributeError: 'list' object has no attribute 'lower'Treating Claude AIMessage.content as str (P02)Use msg.text() or the Step 2 extractor
ValidationError: extra fields not permittedPydantic v2 strict default; model added fields (P53)Set model_config = ConfigDict(extra="ignore")
ValidationError: Field required on Optional[list[X]]method="function_calling" drops ambiguous unions (P03)Switch to method="json_schema"
anthropic.BadRequestError: tool_choice requires toolsForcing tool without binding any (P63)Call .bind_tools([tool]) before .with_config(tool_choice=...)
google.api_core.exceptions.InvalidArgument: finish_reason=SAFETYGemini default safety thresholds (P65)Override safety_settings per model init or switch provider
Streaming response response_metadata["token_usage"] == {}Stream end not yet reached (P01)Use astream_events(version="v2")
ImportError: cannot import name 'ChatOpenAI' from 'langchain.chat_models'Legacy 0.2 import path (P38)from langchain_openai import ChatOpenAI

Examples

Routing: cheap draft, expensive final

A common pattern — draft with gpt-4o-mini, finalize with claude-sonnet-4-6. The factory in Step 3 makes this trivial; combined with with_structured_output the finalize step returns a typed object.

See Provider Quirks for the full draft-then-finalize example including the token budget calculation.

Extracting tool calls from a single-shot response

A classification task that should return one tool call with a typed argument. Use bind_tools([...], tool_choice={"type": "tool", "name": "Classify"}) for a single forced call — but never loop on a forced choice (P63).

See Structured Output Methods for the worked example and the decision tree for tool vs structured-output for extraction.

Multi-modal: screenshot plus prompt

Images are passed as content blocks, but the block shape differs between providers (P64). LangChain 1.0 abstracts this into a universal image content block.

See Content Blocks for the universal shape and per-provider adapter examples.

Resources

What ships with it: 5 files

18.4 KB alongside SKILL.md

Gives 0 of the 12 instructions most context ai engineering skills give in ~2.5k tokens

Counted across 1,328 of the 2,349 authors here whose files we hold, read 2026-09-06

  • Dispatch a fresh subagent for each taskin 76 of 1328, across 59 files
  • Perform spec compliance review before code quality reviewin 44 of 1328, across 34 files
  • Dispatch a final code reviewer after all tasksin 38 of 1328, across 26 files
  • Answer subagent questions before allowing implementationin 36 of 1328, across 26 files
  • Use the least powerful model capable of the taskin 33 of 1328, across 26 files
  • Create a TodoWrite list for all tasksin 32 of 1328, across 22 files
  • Perform a task review after each implementationin 31 of 1328, across 24 files
  • Extract all tasks and context from the planin 29 of 1328, across 20 files
  • Provide full task text to subagentsin 28 of 1328, across 20 files
  • Use git worktrees for isolated workspacesin 25 of 1328, across 20 files
  • Specify the model explicitly when dispatching a subagentin 23 of 1328, across 18 files
  • Execute all tasks from the plan without stoppingin 21 of 1328, across 16 files

Said here and by no other author read

  • Initialize chat models with timeout 30 and max_retries 2
  • Use AIMessage.text() to extract content
  • Iterate AIMessage.content as typed blocks when filtering
  • Route providers using a factory returning BaseChatModel
  • Use astream_events version v2 for streaming token counting
  • Use json_schema method for with_structured_output

Grouped from the skills themselves: near-identical wordings counted once, and counted by distinct author, so one author publishing three of these counts once. Length counted with cl100k_base; the agent that loads this file may tokenize it differently.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.