Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open In ColabOpen on GitHub

How to access the RunnableConfig from a tool

Prerequisites

If you have atool that callschat models,retrievers, or otherrunnables, you may want to access internal events from those runnables or configure them with additional properties. This guide shows you how to manually pass parameters properly so that you can do this using theastream_events() method.

Tools arerunnables, and you can treat them the same way as any other runnable at the interface level - you can callinvoke(),batch(), andstream() on them as normal. However, when writing custom tools, you may want to invoke other runnables like chat models or retrievers. In order to properly trace and configure those sub-invocations, you'll need to manually access and pass in the tool's currentRunnableConfig object. This guide show you some examples of how to do that.

Compatibility

This guide requireslangchain-core>=0.2.16.

Inferring by parameter type

To access reference the active config object from your custom tool, you'll need to add a parameter to your tool's signature typed asRunnableConfig. When you invoke your tool, LangChain will inspect your tool's signature, look for a parameter typed asRunnableConfig, and if it exists, populate that parameter with the correct value.

Note: The actual name of the parameter doesn't matter, only the typing.

To illustrate this, define a custom tool that takes a two parameters - one typed as a string, the other typed asRunnableConfig:

%pip install-qU langchain_core
from langchain_core.runnablesimport RunnableConfig
from langchain_core.toolsimport tool


@tool
asyncdefreverse_tool(text:str, special_config_param: RunnableConfig)->str:
"""A test tool that combines input text with a configurable parameter."""
return(text+ special_config_param["configurable"]["additional_field"])[::-1]
API Reference:RunnableConfig |tool

Then, if we invoke the tool with aconfig containing aconfigurable field, we can see thatadditional_field is passed through correctly:

await reverse_tool.ainvoke(
{"text":"abc"}, config={"configurable":{"additional_field":"123"}}
)
'321cba'

Next steps

You've now seen how to configure and stream events from within a tool. Next, check out the following guides for more on using tools:

You can also check out some more specific uses of tool calling:


[8]ページ先頭

©2009-2025 Movatter.jp