migrating-langchain-to-pydantic-ai

Migrate LangChain and LangGraph applications to Pydantic AI while preserving observed behavioral contracts.

19.6k|2.6k|Updated Jun 21, 2024
One-click install
npx skills add https://github.com/pydantic/pydantic-ai --skill migrating-langchain-to-pydantic-ai
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: migrating-langchain-to-pydantic-ai
Source: https://github.com/pydantic/pydantic-ai/tree/main/pydantic_ai_slim/pydantic_ai/.agents/skills/migrating-langchain-to-pydantic-ai
Command: npx skills add https://github.com/pydantic/pydantic-ai --skill migrating-langchain-to-pydantic-ai

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Migrating LangChain or LangGraph applications to Pydantic AI is risky because similar-looking APIs differ in semantics: message history formats, structured output transports, checkpointing, interrupts, streaming, and retry behavior rarely map one-to-one. This Skill guides a behavior-preserving migration so contracts are verified rather than assumed.

Core Features & Use Cases

  • Contract-first migration workflow: Trace a representative request through prompts, tools, state, persistence, and streaming, then migrate the smallest behaviorally complete vertical slice behind the existing public boundary.
  • Concept mapping and semantic-gap analysis: Reference guides map LangChain/LangGraph primitives (agents, LCEL, middleware, checkpointers, interrupts) to Pydantic AI equivalents and flag non-equivalent behaviors like run_stream() versus run() or tool retry semantics.
  • Verification and cutover discipline: Classify every observed contract as verified-equivalent, verified-adapter, intentional-change, or unverified, with parity tests, Logfire-based trace comparison, and safe shadow/canary cutover steps.
  • Use Case: Given a LangGraph agent with a checkpointer and human-in-the-loop approval, use this Skill to split state into dependencies, message history, workflow state, and memory, then rebuild approval with deferred tools and prove resume semantics with crash-injection tests.

Quick Start

Ask the AI to use the migrating-langchain-to-pydantic-ai skill to migrate the LangChain agent in your repository to Pydantic AI while preserving its existing public contracts.

Frequently Asked Questions about migrating-langchain-to-pydantic-ai

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I migrate a LangChain agent to Pydantic AI?

Trace one representative request through prompts, tools, state, and streaming, then rebuild the smallest behaviorally complete slice as a pydantic_ai.Agent with typed dependencies and tools. Preserve public request and response shapes with a small adapter and prove parity with characterization tests.

How do LangGraph checkpointers map to Pydantic AI?

Pydantic AI message_history only provides conversation continuity, not checkpoint replay, forks, or pending writes. Split state into dependencies, messages, workflow state, and long-term memory, then use application persistence or a durable execution integration such as DBOS, Temporal, or Prefect.

Can Pydantic AI reuse existing LangChain tools during migration?

Yes, tool_from_langchain and LangChainToolset wrap existing LangChain tools as a transitional bridge. The wrapper keeps LangChain validation and dependencies and does not preserve flags like return_direct, so add parity tests and plan removal.

Does run_stream in Pydantic AI behave like run?

No. run_stream() commits the first matching streamed output immediately, so co-emitted tools and retries can produce a different terminal result than run(). Use run(event_stream_handler=...), run_stream_events(), or iter() when complete tool execution must finish before the final result.

When should I not use this LangChain migration skill?

Do not use it for migrations centered on create_deep_agent or Deep Agents harness features such as planning, skills, filesystem, subagents, or sandbox contracts. Those are harness migrations that require different scope decisions.

How do I verify a LangChain to Pydantic AI migration is complete?

Classify every observed contract as verified-equivalent, verified-adapter, intentional-change, external-owner, not-applicable, unverified, or blocked, each linked to an executable test. Use Logfire traces as supporting evidence only, and remove LangChain dependencies only after no retained path needs them.