langgoap.goapify_tool

Contents

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 BaseTool into a LangGOAP ActionSpec.

The resulting action’s execute callable invokes tool with the state dict filtered to match the tool’s declared input schema (if any) and returns the declared effects dict. A tool failure is an action failure: tools created with handle_tool_error / handle_validation_error are invoked through a copy with those options off, so a ToolException raises (and the observer blacklists and replans) instead of the error text being applied as a successful result. Passing no preconditions/effects yields an action with empty pre/eff — legal but rarely useful, which is why create_goap_agent() emits a warning in that case.

Parameters:
  • tool (BaseTool) – Any LangChain BaseTool instance (e.g. produced by the @tool decorator).

  • 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 in effects. Typical usage:

    search = goapify_tool(
        search_tool,
        effects={"search_done": True},
        result_key="search_results",
    )
    # After execution: world_state["search_results"] == <raw output>
    

    result_key must not overlap with any key in effects — a collision would silently overwrite the planning flag with raw tool output, corrupting the world state A* reasons over.

  • qos (ActionQos | None) – Optional ActionQos retry / idempotency policy applied by the executor.

  • require_human_approval (bool | type) – True to gate the tool behind a LangGraph interrupt() approve/deny prompt, or a Pydantic BaseModel subclass 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) – False for actions that must not run twice within one run (the planner will not re-plan through a spent instance).

  • read_only (bool) – True for 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:

ActionSpec

:param Every remaining ActionSpec field has the same meaning and: :param default as on ActionSpec itself: :param so a tool wrapped here is: :param a first-class action.:

Returns:

A frozen ActionSpec ready to feed into GoapGraph.

Raises:
  • TypeError – If tool is not a BaseTool instance.

  • ValueError – If result_key matches a key already declared in effects. 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:

ActionSpec