METADATA 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. Metadata-Version: 2.4
  2. Name: langgraph-prebuilt
  3. Version: 1.0.5
  4. Summary: Library with high-level APIs for creating and executing LangGraph agents and tools.
  5. Project-URL: Source, https://github.com/langchain-ai/langgraph/tree/main/libs/prebuilt
  6. Project-URL: Twitter, https://x.com/LangChainAI
  7. Project-URL: Slack, https://www.langchain.com/join-community
  8. Project-URL: Reddit, https://www.reddit.com/r/LangChain/
  9. License-Expression: MIT
  10. License-File: LICENSE
  11. Classifier: Development Status :: 5 - Production/Stable
  12. Classifier: Programming Language :: Python
  13. Classifier: Programming Language :: Python :: 3
  14. Classifier: Programming Language :: Python :: 3 :: Only
  15. Classifier: Programming Language :: Python :: 3.10
  16. Classifier: Programming Language :: Python :: 3.11
  17. Classifier: Programming Language :: Python :: 3.12
  18. Classifier: Programming Language :: Python :: 3.13
  19. Classifier: Programming Language :: Python :: Implementation :: CPython
  20. Classifier: Programming Language :: Python :: Implementation :: PyPy
  21. Requires-Python: >=3.10
  22. Requires-Dist: langchain-core>=1.0.0
  23. Requires-Dist: langgraph-checkpoint<4.0.0,>=2.1.0
  24. Description-Content-Type: text/markdown
  25. # LangGraph Prebuilt
  26. This library defines high-level APIs for creating and executing LangGraph agents and tools.
  27. > [!IMPORTANT]
  28. > This library is meant to be bundled with `langgraph`, don't install it directly
  29. ## Agents
  30. `langgraph-prebuilt` provides an [implementation](https://langchain-ai.github.io/langgraph/reference/prebuilt/#langgraph.prebuilt.chat_agent_executor.create_react_agent) of a tool-calling [ReAct-style](https://langchain-ai.github.io/langgraph/concepts/agentic_concepts/#react-implementation) agent - `create_react_agent`:
  31. ```bash
  32. pip install langchain-anthropic
  33. ```
  34. ```python
  35. from langchain_anthropic import ChatAnthropic
  36. from langgraph.prebuilt import create_react_agent
  37. # Define the tools for the agent to use
  38. def search(query: str):
  39. """Call to surf the web."""
  40. # This is a placeholder, but don't tell the LLM that...
  41. if "sf" in query.lower() or "san francisco" in query.lower():
  42. return "It's 60 degrees and foggy."
  43. return "It's 90 degrees and sunny."
  44. tools = [search]
  45. model = ChatAnthropic(model="claude-3-7-sonnet-latest")
  46. app = create_react_agent(model, tools)
  47. # run the agent
  48. app.invoke(
  49. {"messages": [{"role": "user", "content": "what is the weather in sf"}]},
  50. )
  51. ```
  52. ## Tools
  53. ### ToolNode
  54. `langgraph-prebuilt` provides an [implementation](https://langchain-ai.github.io/langgraph/reference/prebuilt/#langgraph.prebuilt.tool_node.ToolNode) of a node that executes tool calls - `ToolNode`:
  55. ```python
  56. from langgraph.prebuilt import ToolNode
  57. from langchain_core.messages import AIMessage
  58. def search(query: str):
  59. """Call to surf the web."""
  60. # This is a placeholder, but don't tell the LLM that...
  61. if "sf" in query.lower() or "san francisco" in query.lower():
  62. return "It's 60 degrees and foggy."
  63. return "It's 90 degrees and sunny."
  64. tool_node = ToolNode([search])
  65. tool_calls = [{"name": "search", "args": {"query": "what is the weather in sf"}, "id": "1"}]
  66. ai_message = AIMessage(content="", tool_calls=tool_calls)
  67. # execute tool call
  68. tool_node.invoke({"messages": [ai_message]})
  69. ```
  70. ### ValidationNode
  71. `langgraph-prebuilt` provides an [implementation](https://langchain-ai.github.io/langgraph/reference/prebuilt/#langgraph.prebuilt.tool_validator.ValidationNode) of a node that validates tool calls against a pydantic schema - `ValidationNode`:
  72. ```python
  73. from pydantic import BaseModel, field_validator
  74. from langgraph.prebuilt import ValidationNode
  75. from langchain_core.messages import AIMessage
  76. class SelectNumber(BaseModel):
  77. a: int
  78. @field_validator("a")
  79. def a_must_be_meaningful(cls, v):
  80. if v != 37:
  81. raise ValueError("Only 37 is allowed")
  82. return v
  83. validation_node = ValidationNode([SelectNumber])
  84. validation_node.invoke({
  85. "messages": [AIMessage("", tool_calls=[{"name": "SelectNumber", "args": {"a": 42}, "id": "1"}])]
  86. })
  87. ```
  88. ## Agent Inbox
  89. The library contains schemas for using the [Agent Inbox](https://github.com/langchain-ai/agent-inbox) with LangGraph agents. Learn more about how to use Agent Inbox [here](https://github.com/langchain-ai/agent-inbox#interrupts).
  90. ```python
  91. from langgraph.types import interrupt
  92. from langgraph.prebuilt.interrupt import HumanInterrupt, HumanResponse
  93. def my_graph_function():
  94. # Extract the last tool call from the `messages` field in the state
  95. tool_call = state["messages"][-1].tool_calls[0]
  96. # Create an interrupt
  97. request: HumanInterrupt = {
  98. "action_request": {
  99. "action": tool_call['name'],
  100. "args": tool_call['args']
  101. },
  102. "config": {
  103. "allow_ignore": True,
  104. "allow_respond": True,
  105. "allow_edit": False,
  106. "allow_accept": False
  107. },
  108. "description": _generate_email_markdown(state) # Generate a detailed markdown description.
  109. }
  110. # Send the interrupt request inside a list, and extract the first response
  111. response = interrupt([request])[0]
  112. if response['type'] == "response":
  113. # Do something with the response
  114. ...
  115. ```