Files
2025-11-05 01:03:10 +01:00

3.7 KiB

LangChain v1.0 Migration Notes

What Changed

The codebase has been updated to use LangChain v1.0 API, which introduced breaking changes from v0.x.

Key Changes Made

1. Agent Creation API

Before (v0.x):

from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.tools import Tool

# Create tools as Tool objects
tools = [
    Tool(name="navigate", func=navigate_wrapper, description="..."),
    # ... more tools
]

# Create prompt with MessagesPlaceholder
prompt = ChatPromptTemplate.from_messages([...])

# Create agent and executor
agent = create_openai_tools_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, ...)

# Execute
result = await agent_executor.ainvoke({"input": task})

After (v1.0):

from langchain.agents import create_agent

# Create tools as simple functions with docstrings
def navigate(url: str) -> str:
    """Navigate to a URL. Input should be a valid URL."""
    return asyncio.run(self.browser.navigate(url))

tools = [navigate, click, type_text, ...]  # Just functions!

# Create agent directly
agent = create_agent(
    model="gpt-4o-mini",
    tools=tools,
    system_prompt="Your instructions here"
)

# Execute with messages format
result = await agent.ainvoke(
    {"messages": [{"role": "user", "content": task}]}
)

2. Tool Definition

Before:

  • Tools were defined as Tool objects
  • Required explicit name, func, and description parameters

After:

  • Tools are simple Python functions
  • Function docstring becomes the tool description
  • Function name becomes the tool name
  • Much simpler and cleaner!

3. Response Format

Before:

result = await agent_executor.ainvoke({"input": task})
output = result.get("output")

After:

result = await agent.ainvoke({"messages": [{"role": "user", "content": task}]})
output = result["messages"][-1].content

4. Pydantic Models

Also updated Pydantic models to use v2.0 syntax:

Before:

class Settings(BaseSettings):
    field: str

    class Config:
        env_file = ".env"

After:

from pydantic import ConfigDict

class Settings(BaseSettings):
    model_config = ConfigDict(env_file=".env")

    field: str

Benefits of v1.0

  1. Simpler API: Less boilerplate code
  2. Better Tool Definition: Functions with docstrings vs Tool objects
  3. Built on LangGraph: Better durability and streaming support
  4. Cleaner Code: More intuitive and Pythonic
  5. Better Performance: Optimized execution

Migration Checklist

  • Replace create_openai_tools_agent with create_agent
  • Remove AgentExecutor usage
  • Convert Tool objects to simple functions
  • Update invocation format to use messages
  • Update response parsing
  • Fix Pydantic v2.0 deprecations
  • Test all functionality

Running the Application

The application now works with LangChain v1.0:

# Install dependencies
uv pip install -e .
playwright install chromium

# Set up environment
cp .env.example .env
# Edit .env and add OPENAI_API_KEY

# Run
python main.py

Documentation

Issues Fixed

  • ImportError: cannot import name 'AgentExecutor' - Fixed by using new API
  • Pydantic deprecation warnings - Fixed by using ConfigDict
  • All imports working correctly
  • Application runs successfully