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/ainvokeand returns the JSON-nativeGoapStateChannelsform (planandexecution_historyentries are dicts); uselanggoap.rehydrate_state()to get typed objects. Pass the goal asagent.goap_goal(orgoal.to_dict()when a checkpointer is attached, because LangGraph persists the raw input).NL-at-invocation-time limitation: the returned
CompiledStateGraphonly exposes LangGraph’sinvoke({"goal": GoalSpec, "world_state": dict})andainvokeinterfaces. It does not haveinvoke_nl(), because that method lives on theGoapGraphbuilder, not on the compiled graph. For single-shot NL execution passgoal=request_stringdirectly (NL interpretation happens once at construction). For repeated NL-driven invocations against the same tool set, construct aGoapGraphdirectly and useinvoke_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,llmmust be provided.llm (BaseChatModel | None) – Chat model used to interpret a string goal. Ignored when
goalis already aGoalSpec.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 ofresearch_topicavailable to a downstreamwrite_article(brief)tool viaworld_state["brief"]. Planning is unaffected; A* still reasons over the boolean flags ineffects. A key here must not collide with any key declared for the same tool ineffects(seegoapify_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;NallowsNextra planner-level replans before blacklisting (i.e.N + 1total 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) -> boolinvoked after execution. ReturningFalsesignals the action did not produce its declared effects (soft failure → replan).qos (dict[str, ActionQos] | None) – Optional mapping of tool name →
ActionQosretry / idempotency policy.require_human_approval (dict[str, bool | type] | None) – Optional mapping of tool name →
True(approve/deny gate) or a PydanticBaseModelsubclass (typed form) that pauses the run withinterrupt()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 →
Falsefor tools that must not run twice in one run.read_only (dict[str, bool] | None) – Optional mapping of tool name →
Truefor 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 raisesTypeError.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
CompiledStateGraphready for.invoke()or.ainvoke(). It carries two attributes for convenience:goap_goal(the resolvedGoalSpec) andgoap_actions(the wrappedActionSpeclist, e.g. forlanggoap.rehydrate_state()).- Raises:
ValueError – If
goalis a string butllmisNone.TypeError – If
**compile_kwargscontains a name thatGoapGraph.compile()does not accept.
- Return type:
CompiledStateGraph