重磅發布!LangChain 1.0 Alpha 來了,Agent 終于統一了! 原創
LangChain 自推出以來迅速成為開發者構建 LLM 驅動應用的重要框架之一,支持多種鏈(chains)、代理(agents)、工具調用、向量檢索、模型接口等功能。其生態不斷壯大,但也因模式多樣、抽象紛繁導致開發者面對不同 “agent 模式” 時感到困惑。為提升一致性、可維護性與企業級穩定性,LangChain 在 2025 年 9 月推出了 v1.0.0 Alpha,與 LangGraph 一起邁入 1.x 版本演進。
為什么要推出 v1.0?
- 企業信心:1.0 版本象征著更加穩定、信賴,降低企業采用風險。
- 抽象收斂:過去存在多種 agent 模式(ReAct、Plan-and-Solve、React-Retry 等),v1.0 把主流用例統一遷移到一個成型的 agent 抽象上,復雜或非常規場景則推薦使用更底層、靈活的 LangGraph。
- 標準輸出格式:不同 LLM 提供商返回結構迥異,新增?
?content_blocks?? 屬性,用以統一 reasoning、工具調用、引用、媒體類型等新 API 特性
核心新特性概覽
統一代理抽象:create_agent
過去要實現“調用工具 → 整理結果 → 輸出結構化 JSON”,要手寫 chain,現在一行搞定:
from langchain.agents import create_agent
from langchain_core.messages import HumanMessage
from pydantic import BaseModel
class WeatherInfo(BaseModel):
city: str
temperature_c: float
condition: str
def fake_weather(city: str) -> str:
returnf"25,Sunny"if city == "Singapore"else"18,Rainy"
agent = create_agent(
model="openai:gpt-4o-mini",
tools=[fake_weather],
response_format=WeatherInfo
)
res = agent.invoke({"messages": [
HumanMessage("What's the weather like in Singapore?")
]})
print(res["structured_response"])
# WeatherInfo(city='Singapore', temperature_c=25.0, condition='Sunny')設計解析
- 所有舊版 agent(ReAct, Plan-and-Execute 等)均重構為?
?create_agent?? 的預設配置。 - 底層統一使用?
?LangGraph?? 作為執行引擎,暴露標準接口:??invoke(input: dict) -> dict??。 - 輸入輸出結構標準化:
a.輸入:??{"messages": List[BaseMessage]}??
b.輸出:??{"messages": List[BaseMessage], "structured_response": Optional[BaseModel]}??
工程價值
- 降低學習成本:開發者只需掌握一個接口。
- 提升可測試性:輸入輸出結構固定,便于 mock 和斷言。
- 支持熱插拔:更換模型或工具集,不影響調用層。
標準化內容模型:content_blocks
所有 ??AIMessage??? 新增 ??.content_blocks: List[ContentBlock]?? 屬性。
類型定義(簡化)
class TextBlock(ContentBlock):
text: str
class ToolCallBlock(ContentBlock):
name: str
args: dict
class CitationBlock(ContentBlock):
sources: List[Source]
class ImageBlock(ContentBlock):
mime_type: str
data: bytes設計解析
- 所有 LLM 輸出(文本、工具調用、引用、圖像等)均封裝為類型化塊。
- 各提供商差異被抽象層屏蔽:
a.OpenAI??function_call?? →??ToolCallBlock??
b.Anthropic??<thinking>?? →??TextBlock?? +??reasoning=True??
c.Claude XML 工具 →??ToolCallBlock??
- 支持懶加載:如?
?ImageBlock.data?? 僅在訪問時解碼。
工程價值
- 模型切換成本趨近于零。
- 輸出可被程序精確消費,無需正則/字符串匹配。
- 為多模態、流式輸出、結構化引用提供擴展基礎。
Runtime:LangGraph 成為默認引擎
??create_agent??? 返回的 agent 本質是一個編譯后的 ??LangGraph?? 實例。
核心能力
- 狀態持久化:對話歷史、工具調用結果、中間變量存儲于?
?GraphState??。 - 流程控制:
a.條件邊(conditional edges):根據狀態決定下一節點。
b.循環控制:自動重試、最大步數限制。
c.異常恢復:工具失敗自動進入 error 節點。
- 可觀測性:內置 tracing,支持 LangSmith 集成。
示例
自定義重試邏輯:
from langgraph.graph import StateGraph
def tool_node(state):
try:
result = tool.invoke(state["input"])
return {"result": result, "error": None}
except Exception as e:
return {"result": None, "error": str(e)}
def should_retry(state):
if state["error"] and state["retry_count"] < 3:
return"tool_node"
else:
return"end_node"
graph = StateGraph(AgentState)
graph.add_node("tool_node", tool_node)
graph.add_conditional_edges("tool_node", should_retry)工程價值
- 企業級韌性:自動重試、熔斷、降級。
- 復雜流程支持:審批流、多階段任務、人工干預點。
- 調試友好:狀態可序列化、可回放、可單元測試。
升級路徑與兼容策略
包結構重構
舊模塊 | 新位置 | 說明 |
? | ? | 僅保留核心抽象 |
? | ? | 索引、社區工具等遷出 |
? | ? | 舊版 agent 執行器 |
漸進式遷移建議
- 新項目:直接使用?
?create_agent?? +??content_blocks??。 - 存量項目:
- 安裝?
?langchain-legacy?? 保持兼容。 - 逐步替換?
?initialize_agent(..., AgentType.REACT...)?? →??create_react_agent(...)??(兼容層)。 - 最終遷移至?
?create_agent(...)??。
- 注意 Breaking Changes:
- Python ≥ 3.10
- ?
?BaseMessage.text()?? →.text(屬性) - 默認返回?
?AIMessage??,非 ??str??
本文轉載自????AI 博物院???? 作者:longyunfeigu

















