You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
asyncdefclear_agent(state: CustomAgentState):
""" Post‑model hook: 1. keep the original “safe‑name” overwrite 2. emit the final prompt to the logger 3. ***NEW*** – if we are about to route to END **while** `state.from_agent.not_replied_yet` is still True, inject a SystemMessage that reminds the model to call `reply_to_agent` and loop back to the agent node (`goto="agent"`). """# ─────────── ORIGINAL LOGIC ───────────this_agent=state.get("this_agent", {}) or {}
raw_name=this_agent.get("agent_name", "")
raw_id=this_agent.get("agent_id", "")
safe_name=f"{''.join(cforcinraw_nameifc.isalnum())}_{raw_id.replace('-', '')}"ifraw_nameandraw_idelse"UnnamedAgent"messages=state.get("messages", [])
ifmessagesandisinstance(messages[-1], AIMessage):
messages[-1].name=safe_nameprompt=awaitprompt_for_agent(state)
logger.info(f"[Dario Agent] Final Prompt Used:\n{prompt}")
# ─────────── NEW REPLY‑ENFORCEMENT ───────────from_agent=state.get("from_agent") or {}
must_reply=from_agent.get("not_replied_yet", False)
# “About to finish” ⇢ last LLM turn has **no** tool callslast_msg=messages[-1] ifmessageselseNonefinishing=isinstance(last_msg, AIMessage) andnotgetattr(last_msg, "tool_calls", None)
ifmust_replyandfinishing:
reminder=SystemMessage(
content=(
"⚠️ You are the **receiver** in an agent→agent thread and ""must call the `reply_to_agent` tool **before ending**."
)
)
# Tell LangGraph to loop back to the agent nodereturnCommand(
update={"messages": [reminder]},
goto="agent"# ← documented pattern:contentReference[oaicite:0]{index=0}
)
# If everything is fine, just return an empty dict (no state mutation)return {}
I’m implementing agent-to-agent messaging using a from_agent field in my LangGraph state, which includes a not_replied_yet: bool flag. If another agent has messaged the current agent, this flag is set to True until the reply_to_agent tool is used. I want to enforce that the graph cannot terminate while not_replied_yet is still True. Right now, I’m using the post_model_hook to check that flag and raise a ValueError if it's still True, which effectively blocks completion. Is this the recommended pattern for enforcing final-state invariants like this in LangGraph, or is there a more idiomatic way to prevent termination until certain tools/actions have been executed?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I’m implementing agent-to-agent messaging using a from_agent field in my LangGraph state, which includes a not_replied_yet: bool flag. If another agent has messaged the current agent, this flag is set to True until the reply_to_agent tool is used. I want to enforce that the graph cannot terminate while not_replied_yet is still True. Right now, I’m using the post_model_hook to check that flag and raise a ValueError if it's still True, which effectively blocks completion. Is this the recommended pattern for enforcing final-state invariants like this in LangGraph, or is there a more idiomatic way to prevent termination until certain tools/actions have been executed?
Beta Was this translation helpful? Give feedback.
All reactions