langgoap.create_goap_agent

langgoap.create_goap_agent#

create_goap_agent(tools, goal, *, llm=None, preconditions=None, effects=None, resources=None, costs=None, result_keys=None, max_retries=None, durations=None, effect_validators=None, qos=None, require_human_approval=None, human_input_key=None, can_rerun=None, read_only=None, utility=None, metadata=None, strategy=None, tracer=None, history=None, sensors=None, guards=None, resolvers=None, record_expansions=False, transition_model=None, rng=None, stuck_handlers=None, max_stuck_iterations=3, termination_policies=None, executor='sequential', history_state='full', **compile_kwargs)[source]#

Create a compiled GOAP agent from a list of LangChain tools and a goal.

The returned graph exposes LangGraph’s invoke / ainvoke and returns the JSON-native GoapStateChannels form (plan and execution_history entries are dicts); use langgoap.rehydrate_state() to get typed objects. Pass the goal as agent.goap_goal (or goal.to_dict() when a checkpointer is attached, because LangGraph persists the raw input).

NL-at-invocation-time limitation: the returned CompiledStateGraph only exposes LangGraph’s invoke({"goal": GoalSpec, "world_state": dict}) and ainvoke interfaces. It does not have invoke_nl(), because that method lives on the GoapGraph builder, not on the compiled graph. For single-shot NL execution pass goal=request_string directly (NL interpretation happens once at construction). For repeated NL-driven invocations against the same tool set, construct a GoapGraph directly and use invoke_nl().

Parameters:
  • tools (list[BaseTool]) – The LangChain tools the agent may call.

  • goal (str | GoalSpec) – A GoalSpec, or a natural-language string. If a string, llm must be provided.

  • llm (BaseChatModel | None) – Chat model used to interpret a string goal. Ignored when goal is already a GoalSpec.

  • preconditions (dict[str, dict[str, Any]] | None) – Optional mapping of tool name → preconditions dict. Missing tools get empty preconditions.

  • effects (dict[str, dict[str, Any]] | None) – Optional mapping of tool name → effects dict. Missing tools get empty effects.

  • resources (dict[str, dict[str, float]] | None) – Optional mapping of tool name → resources dict.

  • costs (dict[str, float] | None) – Optional mapping of tool name → action cost override.

  • result_keys (dict[str, str] | None) – Optional mapping of tool name → world-state key that should receive the tool’s raw return value at execution time. Use this to wire one tool’s output into the next tool’s input — e.g. {"research_topic": "brief"} makes the return value of research_topic available to a downstream write_article(brief) tool via world_state["brief"]. Planning is unaffected; A* still reasons over the boolean flags in effects. A key here must not collide with any key declared for the same tool in effects (see goapify_tool()).

  • max_retries (dict[str, int] | None) – Optional mapping of tool name → planner-level retry budget. 0 (default) blacklists the action on its first failure; N allows N extra planner-level replans before blacklisting (i.e. N + 1 total failures). This is the knob that lets a transient failure trigger replanning without giving up entirely.

  • durations (dict[str, timedelta] | None) – Optional mapping of tool name → datetime.timedelta. Used by the CSP scheduler when computing parallel execution windows. Tools with no entry default to instantaneous actions.

  • effect_validators (dict[str, EffectValidator] | None) – Optional mapping of tool name → callable (pre_state, post_state) -> bool invoked after execution. Returning False signals the action did not produce its declared effects (soft failure → replan).

  • qos (dict[str, ActionQos] | None) – Optional mapping of tool name → ActionQos retry / idempotency policy.

  • require_human_approval (dict[str, bool | type] | None) – Optional mapping of tool name → True (approve/deny gate) or a Pydantic BaseModel subclass (typed form) that pauses the run with interrupt() before the tool executes.

  • human_input_key (dict[str, str] | None) – Optional mapping of tool name → world-state key receiving the approved form.

  • can_rerun (dict[str, bool] | None) – Optional mapping of tool name → False for tools that must not run twice in one run.

  • read_only (dict[str, bool] | None) – Optional mapping of tool name → True for tools that do not mutate the world.

  • utility (dict[str, float | CostFunction] | None) – Optional mapping of tool name → utility score for UtilityStrategy.

  • metadata (dict[str, dict[str, Any]] | None) – Optional mapping of tool name → free-form metadata dict.

  • strategy (PlanningStrategy | None)

  • tracer (PlanningTracer | None)

  • history (StoreExecutionHistory | None)

  • sensors (list[Sensor | AsyncSensor] | None)

  • guards (list[ActionGuard | AsyncActionGuard] | None)

  • resolvers (list[ConditionResolver | AsyncConditionResolver] | None)

  • record_expansions (bool)

  • transition_model (TransitionModel | None)

  • rng (random.Random | None)

  • stuck_handlers (list[StuckHandler] | None)

  • max_stuck_iterations (int)

  • termination_policies (list[TerminationPolicy] | None)

  • executor (Literal['sequential', 'parallel'])

  • history_state (Literal['full', 'delta', 'planning'])

  • compile_kwargs (Any)

Return type:

CompiledStateGraph

:param : :param record_expansions: :param transition_model: :param rng: :param stuck_handlers: :param : :param max_stuck_iterations: Forwarded verbatim to

GoapGraph; see its docstring.

Parameters:
  • termination_policies (list[TerminationPolicy] | None) – Forwarded verbatim to GoapGraph; see its docstring.

  • executor (Literal['sequential', 'parallel']) – Forwarded verbatim to GoapGraph; see its docstring.

  • history_state (Literal['full', 'delta', 'planning']) – Forwarded verbatim to GoapGraph; see its docstring.

  • **compile_kwargs (Any) – Forwarded to GoapGraph.compile() (checkpointer, store, interrupt_before, interrupt_after). Any other name raises TypeError.

  • tools (list[BaseTool])

  • goal (str | GoalSpec)

  • llm (BaseChatModel | None)

  • preconditions (dict[str, dict[str, Any]] | None)

  • effects (dict[str, dict[str, Any]] | None)

  • resources (dict[str, dict[str, float]] | None)

  • costs (dict[str, float] | None)

  • result_keys (dict[str, str] | None)

  • max_retries (dict[str, int] | None)

  • durations (dict[str, timedelta] | None)

  • effect_validators (dict[str, EffectValidator] | None)

  • qos (dict[str, ActionQos] | None)

  • require_human_approval (dict[str, bool | type] | None)

  • human_input_key (dict[str, str] | None)

  • can_rerun (dict[str, bool] | None)

  • read_only (dict[str, bool] | None)

  • utility (dict[str, float | CostFunction] | None)

  • metadata (dict[str, dict[str, Any]] | None)

  • strategy (PlanningStrategy | None)

  • tracer (PlanningTracer | None)

  • history (StoreExecutionHistory | None)

  • sensors (list[Sensor | AsyncSensor] | None)

  • guards (list[ActionGuard | AsyncActionGuard] | None)

  • resolvers (list[ConditionResolver | AsyncConditionResolver] | None)

  • record_expansions (bool)

  • transition_model (TransitionModel | None)

  • rng (random.Random | None)

  • stuck_handlers (list[StuckHandler] | None)

  • max_stuck_iterations (int)

Returns:

A compiled CompiledStateGraph ready for .invoke() or .ainvoke(). It carries two attributes for convenience: goap_goal (the resolved GoalSpec) and goap_actions (the wrapped ActionSpec list, e.g. for langgoap.rehydrate_state()).

Raises:
  • ValueError – If goal is a string but llm is None.

  • TypeError – If **compile_kwargs contains a name that GoapGraph.compile() does not accept.

Return type:

CompiledStateGraph