3.7 KiB
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
Toolobjects - Required explicit
name,func, anddescriptionparameters
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
- Simpler API: Less boilerplate code
- Better Tool Definition: Functions with docstrings vs Tool objects
- Built on LangGraph: Better durability and streaming support
- Cleaner Code: More intuitive and Pythonic
- Better Performance: Optimized execution
Migration Checklist
- Replace
create_openai_tools_agentwithcreate_agent - Remove
AgentExecutorusage - Convert
Toolobjects 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