Skip to content

Commit 7bf2cb2

Browse files
authored
add ToolRuntime migration section to tools documentation (#1737)
1 parent fd1e40c commit 7bf2cb2

1 file changed

Lines changed: 37 additions & 6 deletions

File tree

src/oss/langchain/tools.mdx

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ The following parameter names are reserved and cannot be used as tool arguments.
167167
| `runtime` | Reserved for `ToolRuntime` parameter (accessing state, context, store) |
168168

169169
To access runtime information, use the @[`ToolRuntime`] parameter instead of naming your own arguments `config` or `runtime`.
170+
171+
If you use `InjectedState`, `InjectedStore`, `get_runtime()`, or `InjectedToolCallId`, see [Migrate from older injection patterns](#migrate-from-older-injection-patterns).
170172
:::
171173

172174
## Access context
@@ -621,6 +623,40 @@ Requires `deepagents>=1.9.0` (or `@langchain/langgraph>=1.2.8`).
621623
</Note>
622624
:::
623625

626+
:::python
627+
<Accordion title="Migrate from older injection patterns" id="migrate-from-older-injection-patterns">
628+
629+
Older examples used `InjectedState`, `InjectedStore`, `get_runtime()`, or `InjectedToolCallId`. Use @[`ToolRuntime`] instead for one explicit interface to state, context, store, and execution metadata.
630+
631+
#### Previous pattern
632+
633+
```python
634+
from langchain.tools import tool, InjectedState
635+
636+
@tool
637+
def summarize(state: InjectedState) -> str:
638+
"""Summarize the conversation."""
639+
messages = state["messages"]
640+
return f"Conversation length: {len(messages)} messages."
641+
```
642+
643+
#### Recommended pattern
644+
645+
```python
646+
from langchain.tools import tool, ToolRuntime
647+
648+
@tool
649+
def summarize(runtime: ToolRuntime) -> str:
650+
"""Summarize the conversation."""
651+
messages = runtime.state["messages"]
652+
return f"Conversation length: {len(messages)} messages."
653+
```
654+
655+
For agent-level migrations (for example `create_react_agent` and custom state), see the [LangChain v1 migration guide](/oss/migrate/langchain-v1).
656+
657+
</Accordion>
658+
:::
659+
624660
## Tool execution
625661

626662
In LangChain, tools are used by agents (for example via @[`create_agent`]) and tool error handling is configured through [middleware](/oss/langchain/middleware).
@@ -727,10 +763,9 @@ Handle tool errors using LangChain agent [middleware](/oss/langchain/middleware)
727763

728764
### State injection
729765

730-
Tools can access the current graph state through @[`ToolRuntime`]:
766+
Tools access graph state through @[`ToolRuntime`]. See [Access context](#access-context) for state, context, store, and streaming APIs.
731767

732768
:::python
733-
734769
```python
735770
from langchain.tools import tool, ToolRuntime
736771

@@ -740,12 +775,8 @@ def get_message_count(runtime: ToolRuntime) -> str:
740775
messages = runtime.state["messages"]
741776
return f"There are {len(messages)} messages."
742777
```
743-
744778
:::
745779

746-
For more details on accessing state, context, and long-term memory from tools, see [Access context](#access-context).
747-
748-
749780
## Dynamic tool selection
750781

751782
With dynamic tools, the set of tools available to the agent is modified at runtime rather than defined all upfront. Not every tool is appropriate for every situation. Too many tools may overwhelm the model (overload context) and increase errors; too few limit capabilities. Dynamic tool selection enables adapting the available toolset based on authentication state, user permissions, feature flags, or conversation stage.

0 commit comments

Comments
 (0)