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

162 lines
3.7 KiB
Markdown

# 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):**
```python
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):**
```python
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:**
```python
result = await agent_executor.ainvoke({"input": task})
output = result.get("output")
```
**After:**
```python
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:**
```python
class Settings(BaseSettings):
field: str
class Config:
env_file = ".env"
```
**After:**
```python
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
- [x] Replace `create_openai_tools_agent` with `create_agent`
- [x] Remove `AgentExecutor` usage
- [x] Convert `Tool` objects to simple functions
- [x] Update invocation format to use messages
- [x] Update response parsing
- [x] Fix Pydantic v2.0 deprecations
- [x] Test all functionality
## Running the Application
The application now works with LangChain v1.0:
```bash
# 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
- [LangChain v1.0 Overview](https://docs.langchain.com/oss/python/langchain/overview)
- [Migration Guide](https://docs.langchain.com/oss/python/migrate/langchain-v1)
- [Release Notes](https://docs.langchain.com/oss/python/releases/langchain-v1)
## 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