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

/langchain-model-inference

@0d57af8
Run it yourself

Invoke Claude, GPT-4o, and Gemini through LangChain 1.0 without tripping on the content-block, token-accounting, and structured-output quirks that silently break production code. Use when initializing chat models, routing across providers, iterating AIMessage content, or choosing a structured-output method. Trigger with "langchain model inference", "ChatAnthropic", "ChatOpenAI", "with_structured_output", "AIMessage content blocks", "langchain routing".

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

Nothing lands on disk. Nothing to clean up.

Fork this Skill

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

referencesone-pager.md

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

langchain-model-inference — One-Pager

Invoke Claude, GPT-4o, and Gemini through LangChain 1.0 without tripping on content blocks, token-accounting, or structured-output quirks.

The Problem

AIMessage.content is a str on simple OpenAI calls and a list[dict] the instant a tool_use, thinking, or image block enters the response — so message.content.lower() crashes with AttributeError on your first production Claude call. Token counts via on_llm_end lag stream duration (5-30s on long completions). with_structured_output(method="function_calling") silently drops Optional[list[X]] fields on ~40% of real schemas. Default max_retries=6 on ChatOpenAI bills a single logical call as up to seven requests.

The Solution

This skill walks through ChatAnthropic, ChatOpenAI, and ChatGoogleGenerativeAI initialization with version-safe defaults (explicit timeout, bounded retries); a content-block extractor that handles both str and list[dict] shapes and streaming deltas; a factory-pattern router that centralizes provider defaults; correct token counting via astream_events(version="v2"); and a with_structured_output method decision tree tied to each provider's native schema support. Pinned to LangChain 1.0.x with four deep references.

Who / What / When

Aspect Details
Who Python engineers initializing chat models, routing across providers, or extracting structured data with LangChain 1.0
What Version-safe model init, content-block-safe extractor, factory routing, streaming token meter, structured-output method decision tree, 4 references (content-blocks, token-accounting, structured-output-methods, provider-quirks)
When After langchain-install-auth, before any real chain work — this is where content-shape and token-accounting bugs will surface first

Key Features

  1. Content-block safe extractor — Single function handles both str (OpenAI simple) and list[dict] (Claude always, OpenAI with tools) shapes of AIMessage.content; also works on astream_events chunks
  2. Streaming token meter via astream_events(version="v2") — Reports input/output/cache tokens incrementally from on_chat_model_stream events, not at stream end; dashboards stay live
  3. with_structured_output method decision tree — Per-provider / per-model recommendation matrix (json_schema for Claude 3.5+/GPT-4o+/Gemini 2.5; function_calling for legacy; json_mode + Pydantic retry for unknown), with fallbacks for Optional[list[X]] and extra-field issues

Quick Start

See SKILL.md for full instructions.

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

    The skill provides safe implementation patterns for LangChain 1.0, specifically addressing multi-provider support (Anthropic, OpenAI, Gemini). It contains educational documentation and robust code snippets for handling message content, token usage, and structured output without any detected security risks.

  • 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 6 allowed tools
ReadWriteEditBash(python:*)Bash(pip:*)Grep
Other metadata
tags
[
  "saas",
  "langchain",
  "langgraph",
  "python",
  "langchain-1.0",
  "model-inference"
]

README badge

README badge for jeremylongshore/tons-of-skills-marketplace/langchain-model-inference