langgoap.goapify_tool#
- goapify_tool(tool, *, preconditions=None, effects=None, cost=1.0, resources=None, duration=None, max_retries=0, effect_validator=None, result_key=None, qos=None, require_human_approval=False, human_input_key=None, can_rerun=True, read_only=False, utility=None, metadata=None)[source]#
Wrap a LangChain
BaseToolinto a LangGOAPActionSpec.The resulting action’s
executecallable invokestoolwith the state dict filtered to match the tool’s declared input schema (if any) and returns the declaredeffectsdict. A tool failure is an action failure: tools created withhandle_tool_error/handle_validation_errorare invoked through a copy with those options off, so aToolExceptionraises (and the observer blacklists and replans) instead of the error text being applied as a successful result. Passing nopreconditions/effectsyields an action with empty pre/eff — legal but rarely useful, which is whycreate_goap_agent()emits a warning in that case.- Parameters:
tool (BaseTool) – Any LangChain
BaseToolinstance (e.g. produced by the@tooldecorator).preconditions (dict[str, Any] | None) – World-state conditions required before the tool runs. Keys must be hashable scalars.
effects (dict[str, Any] | None) – World-state changes produced by executing the tool. These boolean/scalar flags are what A* reasons over.
cost (float) – Static action cost for A* search. Defaults to 1.0.
resources (dict[str, float] | None) – Per-run resource usage for CSP optimization (e.g.
{"cost_usd": 0.02, "tokens": 500}).duration (timedelta | None) – Estimated wall-clock duration for temporal scheduling.
max_retries (int) – Number of retries before the executor blacklists the action.
effect_validator (Callable[[dict[str, Any], dict[str, Any]], bool] | None) – Optional postcondition checker.
result_key (str | None) –
When set, the tool’s raw return value is stored in
world_state[result_key]after execution so that downstream actions can read it. Planning is unaffected — A* continues to reason over the boolean flags ineffects. Typical usage:search = goapify_tool( search_tool, effects={"search_done": True}, result_key="search_results", ) # After execution: world_state["search_results"] == <raw output>
result_keymust not overlap with any key ineffects— a collision would silently overwrite the planning flag with raw tool output, corrupting the world state A* reasons over.qos (ActionQos | None) – Optional
ActionQosretry / idempotency policy applied by the executor.require_human_approval (bool | type) –
Trueto gate the tool behind a LangGraphinterrupt()approve/deny prompt, or a PydanticBaseModelsubclass to collect a typed form on resume.human_input_key (str | None) – World-state key that receives the approved form (defaults to the action name).
can_rerun (bool) –
Falsefor actions that must not run twice within one run (the planner will not re-plan through a spent instance).read_only (bool) –
Truefor actions that do not mutate the world; a planning hint consumed by strategies and validators.utility (float | CostFunction | None) – Optional utility score (static or callable) consumed by
UtilityStrategy.metadata (dict[str, Any] | None) – Free-form, JSON-serialisable metadata attached to the action. Merged over
{"description": tool.description, "args": tool.args}(your keys win), so the natural-language goal interpreter sees the tool’s docstring and arguments.
- Return type:
:param Every remaining
ActionSpecfield has the same meaning and: :param default as onActionSpecitself: :param so a tool wrapped here is: :param a first-class action.:- Returns:
A frozen
ActionSpecready to feed intoGoapGraph.- Raises:
TypeError – If
toolis not aBaseToolinstance.ValueError – If
result_keymatches a key already declared ineffects. This would silently overwrite the planning flag with raw tool output at execution time.
- Parameters:
tool (BaseTool)
preconditions (dict[str, Any] | None)
effects (dict[str, Any] | None)
cost (float)
resources (dict[str, float] | None)
duration (timedelta | None)
max_retries (int)
effect_validator (Callable[[dict[str, Any], dict[str, Any]], bool] | None)
result_key (str | None)
qos (ActionQos | None)
require_human_approval (bool | type)
human_input_key (str | None)
can_rerun (bool)
read_only (bool)
utility (float | CostFunction | None)
metadata (dict[str, Any] | None)
- Return type: